Wordpress settings menu not displaying correctly - php

I've create a settings menu with tabs and some content. I get no PHP errors but when I load the page my settings are looking like this:
So as you can see my content is outside of the tab in the left corner. I'm absolutely not sure whats wrong here. This is my code:
Here I register the new menu page:
add_action( 'admin_menu', 'register_settings_submenu' );
function register_settings_submenu() {
add_submenu_page( 'options-general.php', 'Additional Settings', 'Additional Settings', 'manage_options', 'settings-submenu', 'settings_page' );
}
This is the regarding function for the add_action above:
function settings_page() {
global $settings_active_tab;
$settings_active_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : 'general'; ?>
<h2 class="nav-tab-wrapper">
<?php
do_action( 'payments_tab' )
?>
</h2>
<?php
do_action( 'payments_content' );
}
This is the function for the tab:
add_action( 'payments_tab', 'payments_tab', 1 );
function payments_tab() {
global $settings_active_tab; ?>
<a class="nav-tab <?php echo $settings_active_tab === 'payments' || '' ? 'nav-tab-active' : ''; ?>"
href="<?php echo admin_url( 'admin.php?page=settings-submenu&tab=payments' ); ?>"><?php _e( 'Zahlungen ', 'woocommerce' ); ?> </a>
<?php
}
This is the function for the content:
add_action( 'payments_content', 'payments_content' );
function payments_content() {
global $settings_active_tab;
if ( '' || 'payments' !== $settings_active_tab ) {
return;
} ?>
<div class="wrap">
<h2>Zahlungen</h2>
<form method="post" action="options.php">
<table class="form-table">
<tbody>
<?php
settings_fields( "payments" );
do_settings_sections( "payment-options" );
?>
</tbody>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php }
This function gets the content for this tab and init it:
add_action( "admin_init", "display_settings_pages_content" );
function display_settings_pages_content() {
payment_settings_element();
}
This is the function payment_settings_element:
function payment_settings_element() {
$settings = array(
'field1' => array(
'name' => __( 'Field 1', 'settings' ),
'id' => 'wc_tab_field1',
'type' => 'text',
'desc_tip' => __( 'Im a short desc', 'settings' ),
'desc' => __( '%', 'settings' )
),
'field2' => array(
'name' => __( 'Field 2', 'settings' ),
'id' => 'wc_tab_field2',
'type' => 'text',
'desc_tip' => __( 'Another short desc', 'settings' ),
'desc' => __( '$', 'settings' )
)
);
?>
<?php
foreach ( $settings as $key => $setting ) {
add_settings_field( $key, $setting['name'], createInput( $setting['name'], $setting['id'], $setting['type'], $setting['desc'] ), 'payment-options', 'payments' );
register_setting( 'payments', $key );
} ?>
<?php }
And finally the functions which creates a new settings element:
function createInput( $name, $id, $type, $desc ) {
?>
<tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo $id; ?>"><?php echo $name; ?></label>
</th>
<td class="forminp forminp-text">
<input type="<?php echo $type; ?>" name="<?php echo $id; ?>" id="<?php echo $id; ?>"
value="<?php echo get_option( $id ); ?>">
<span class="description"><?php echo $desc; ?></span>
</td>
</tr>
<?php
}
I've absolutely no clue why the inputs are outside of the tab content which has just the table structure but no entries. What do I wrong?

You used the wrong hook here:
add_action( "admin_init", "display_settings_pages_content" );
Try this:
add_action( "payments_content", "display_settings_pages_content" );

Related

Custom fields code and displaying in the Additional Information table in Woocommerce

I have adapted the code in the Display selected variation custom fields value in WooCommerce product additional information tab post in order to be able to have custom dimension fields in both the product shipping tab area and the product variations tab area of product info. Here is my adapted code:
// Add custom fields to product shipping tab
add_action( 'woocommerce_product_options_dimensions', 'add_product_options_other_dimensions');
function add_product_options_other_dimensions(){
global $product_object;
$product_id = method_exists( $product_object, 'get_id' ) ? $product_object->get_id() : $product_object->id;
echo '</div><div class="options_group">'; // New option group
woocommerce_wp_text_input( array(
'id' => 'real_length',
'class' => 'short',
'label' => __( 'Actual Length', 'woocommerce' ),
'placeholder' => 'L',
'desc_tip' => 'true',
'description' => __( 'Product actual length (in inches).', 'woocommerce' ),
) );
woocommerce_wp_text_input( array(
'id' => 'real_width',
'class' => 'short',
'label' => __( 'Actual Width', 'woocommerce' ),
'placeholder' => 'W',
'desc_tip' => 'true',
'description' => __( 'Product actual width (in inches).', 'woocommerce' ),
) );
woocommerce_wp_text_input( array(
'id' => 'real_height',
'class' => 'short',
'label' => __( 'Actual Height', 'woocommerce' ),
'placeholder' => 'H',
'desc_tip' => 'true',
'description' => __( 'Product actual height (in inches).', 'woocommerce' ),
) );
}
// Save the custom fields values as meta data
add_action( 'woocommerce_process_product_meta', 'save_product_options_other_dimensions' );
function save_product_options_other_dimensions( $post_id ){
if( isset( $_POST['real_length'] ) )
update_post_meta( $post_id, 'real_length', esc_attr( $_POST['real_length'] ) );
if( isset( $_POST['real_width'] ) )
update_post_meta( $post_id, 'real_width', esc_attr( $_POST['real_width'] ) );
if( isset( $_POST['real_height'] ) )
update_post_meta( $post_id, 'real_height', esc_attr( $_POST['real_height'] ) );
}
// Add custom fields to product variation settings
add_action( 'woocommerce_product_after_variable_attributes','add_variation_options_other_dimensions', 10, 3 );
function add_variation_options_other_dimensions( $loop, $variation_data, $variation ){
$variation_real_length = get_post_meta($variation->ID,"real_length", true );
if( ! $variation_real_length ) $variation_real_length = "";
$variation_real_width = get_post_meta($variation->ID,"real_width", true );
if( ! $variation_real_width ) $variation_real_width = "";
$variation_real_height = get_post_meta($variation->ID,"real_height", true );
if( ! $variation_real_height ) $variation_real_height = "";
echo '<p class="form-field dimensions_field">';
woocommerce_wp_text_input( array(
'id' => 'real_length' . '_' . $loop,
'class' => 'short',
'label' => __( 'Actual Length', 'woocommerce' ),
'placeholder' => 'L',
'desc_tip' => 'true',
'description' => __( 'Product actual length (in inches).', 'woocommerce' ),
'value' => $variation_real_length
) );
woocommerce_wp_text_input( array(
'id' => 'real_width' . '_' . $loop,
'class' => 'short',
'label' => __( 'Actual Width', 'woocommerce' ),
'placeholder' => 'W',
'desc_tip' => 'true',
'description' => __( 'Product actual width (in inches).', 'woocommerce' ),
'value' => $variation_real_width
) );
woocommerce_wp_text_input( array(
'id' => '_circuit' . '_' . $loop,
'class' => 'short',
'label' => __( 'Actual Height', 'woocommerce' ),
'placeholder' => 'H',
'desc_tip' => 'true',
'description' => __( 'Product actual height (in inches).', 'woocommerce' ),
'value' => $variation_real_height
) );
echo '</p>';
}
// Save product variation custom fields values
add_action( 'woocommerce_save_product_variation','save_variation_options_other_dimensions', 10 ,2 );
function save_variation_options_other_dimensions( $variation_id, $loop ){
$the_actual_lenght = $_POST["actual_length_$loop"];
if( isset($the_actual_lenght) )
update_post_meta( $variation_id, 'the_actual_lenght', esc_attr($the_actual_lenght) );
$the_actual_width = $_POST["actual_thickness_$loop"];
if( isset($the_actual_width) )
update_post_meta( $variation_id, 'the_actual_width', esc_attr($the_actual_width) );
$the_actual_height = $_POST["actual_height_$loop"];
if( isset($the_actual_height) )
update_post_meta( $variation_id, 'the_actual_height', esc_attr($the_actual_height) );
}
I have two problems though. For one, the code seems to be a little flawed in that it does not seem to save the custom length/width/height fields under the product variation area. I have checked and the flaw is in the original post's code too.
Secondly, the answer on that post does not detail how to code this so that the inputted information can show on the front end within the Additional Product Information table of Woocommerce product listings.
My question is: how do I alter this code to get it to save the product variation fields and to get the custom field values to display on the Woocommerce Additional Information Tab?
I have been working on this solution for days now and any help would be greatly appreciated.
I ended up paying someone to solve this for me but I figured that I would post the answer here. If anyone out there is looking for a solution to add a second set of dimensions to simple product shipping area and variations area and then have that display in the additional information tab, then here is the code solution:
/**
* Plugin Name: Custom Product Fields
*/
defined('ABSPATH') || exit;
/**
* Add and show simple and variable products.
* Class NFCPF
* #package NFCPF
*/
if (!class_exists('NFCPF')) {
class NFCPF
{
/**
* Hook for call.
* NFCPF constructor.
*/
public function __construct()
{
// Add simple product fields
add_action('woocommerce_product_options_shipping', [$this, 'np_show_product_fields'], 101, 3);
add_action('woocommerce_process_product_meta', [$this, 'np_save_product_fields'], 101, 1);
// Add variable product fields.
add_action('woocommerce_product_after_variable_attributes', [$this, 'np_show_product_fields'], 101, 3);
add_action('woocommerce_save_product_variation', [$this, 'np_save_product_fields'], 101, 1);
}
/**
* Show product fields in variation and simple product.
* #param array $loop
* #param array $variation_data
* #param array $variation
*/
public function np_show_product_fields($loop, $variation_data = [], $variation = [])
{
$postId = (isset($variation->ID)) ? $variation->ID : get_the_ID();
$this->np_custom_product_fields($postId);
}
/**
* Save the simple product fields.
* #param int $postId
*/
public function np_save_product_fields($postId)
{
if (isset($postId) && $postId > 0) {
$np_product_fields = $this->np_product_fields_arr();
foreach ($np_product_fields as $key => $custom_field) {
$custom_field = (isset($custom_field['id'])) ? $custom_field['id'] : '';
$np_updated_product = (isset($_POST[$custom_field][$postId])) ? $_POST[$custom_field][$postId] : '';
update_post_meta($postId, $custom_field, $np_updated_product);
}
}
}
/**
* Product Fields Array
* #return array
*/
public function np_product_fields_arr()
{
$np_product_fields = [
[
'type' => 'input_field',
'id' => 'real_length',
'class' => 'short',
'label' => 'Actual Length',
'placeholder' => 'L',
'description' => __('Product actual length (in inches).', 'woocommerce')
],
[
'type' => 'input_field',
'id' => 'real_width',
'class' => 'short',
'label' => 'Actual Width',
'placeholder' => 'W',
'description' => __('Product actual width (in inches).', 'woocommerce')
],
[
'type' => 'input_field',
'id' => 'real_height',
'class' => 'short',
'label' => 'Actual Height',
'placeholder' => 'H',
'description' => __('Product actual height (in inches).', 'woocommerce')
]
];
return $np_product_fields;
}
/**
* Show Product Fields
* #param int $postId
*/
public function np_custom_product_fields($postId)
{
$np_product_fields = $this->np_product_fields_arr();
foreach ($np_product_fields as $key => $custom_field) {
$np_field_type = (isset($custom_field['type'])) ? $custom_field['type'] : '';
$np_action_function_name = 'np_product_' . $np_field_type;
if (method_exists($this, $np_action_function_name)) {
$this->$np_action_function_name($custom_field, $postId);
}
}
}
/**
* Dynamic input field show on product detail page
* #param array $custom_field
* #param int $postId
*/
public function np_product_input_field($custom_field, $postId)
{
$custom_input_field = [
'id' => $custom_field['id'] . '[' . $postId . ']',
'label' => $custom_field['label'],
'class' => $custom_field['class'],
'placeholder' => $custom_field['placeholder'],
'value' => get_post_meta($postId, $custom_field['id'], true)
];
if (isset($custom_field['description'])) {
$custom_input_field['desc_tip'] = true;
$custom_input_field['description'] = $custom_field['description'];
}
woocommerce_wp_text_input($custom_input_field);
}
}
new NFCPF();
}
/**
* Add data to json encoded variation form.
*
* #param array $data - this is the variation's json data
* #param object $product
* #param object $variation
* #return array
*/
function kia_available_variation($data, $product, $variation)
{
$kia_data['real_length'] = $variation->get_meta('real_length', true);
$kia_data['real_width'] = $variation->get_meta('real_width', true);
$kia_data['real_height'] = $variation->get_meta('real_height', true);
return array_merge($data, $kia_data);
}
add_filter('woocommerce_available_variation', 'kia_available_variation', 10, 3);
/**
* Add scripts to variable products.
*/
function kia_on_found_template_for_variable_add_to_cart()
{
add_action('wp_print_footer_scripts', 'kia_variable_footer_scripts', 99);
}
add_action('woocommerce_variable_add_to_cart', 'kia_on_found_template_for_variable_add_to_cart', 30);
function kia_variable_footer_scripts()
{ ?>
<script type="text/template" id="tmpl-variation-template-extra-data">
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--attribute_real_length extra-data">
<th class="woocommerce-product-attributes-item__label"><?php esc_html_e('Length (in)', 'my-text-domain'); ?></th>
<td class="woocommerce-product-attributes-item__value">{{{ data.variation.real_length }}}</td>
</tr>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--attribute_real_width extra-data">
<th class="woocommerce-product-attributes-item__label"><?php esc_html_e('Width (in)', 'my-text-domain'); ?></th>
<td class="woocommerce-product-attributes-item__value">{{{ data.variation.real_width }}}</td>
</tr>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--attribute_real_height extra-data">
<th class="woocommerce-product-attributes-item__label"><?php esc_html_e('Height (in)', 'my-text-domain'); ?></th>
<td class="woocommerce-product-attributes-item__value">{{{ data.variation.real_height }}}</td>
</tr>
</script>
<script type="text/javascript">
jQuery(document).ready(function ($) {
$('form.cart')
.on('found_variation', function (event, variation) {
template = wp.template('variation-template-extra-data');
$template_html = template({
variation: variation
});
// Remove any existing rows.
$('#tab-additional_information').find('.woocommerce-product-attributes tr.extra-data').remove();
// Add new rows.
$('#tab-additional_information').find('.woocommerce-product-attributes tbody').append($template_html);
})
.on('reset_data', function (event, variation) {
$('#tab-additional_information').find('.woocommerce-product-attributes tr.extra-data').remove();
});
});
</script>
<?php
}
// Show custom field to simple product
function np_woocommerce_product_additional_information($product)
{
if ($product->is_type('simple')) {
$product_id = $product->get_id();
$real_length = get_post_meta($product_id, 'real_length', true);
$real_width = get_post_meta($product_id, 'real_width', true);
$real_height = get_post_meta($product_id, 'real_height', true);
ob_start();
?>
<table class="woocommerce-product-attributes shop_attributes">
<tbody>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--attribute_real_length extra-data">
<th class="woocommerce-product-attributes-item__label"><?php esc_html_e('Length (in)', 'np-domain'); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo $real_length; ?></td>
</tr>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--attribute_real_width extra-data">
<th class="woocommerce-product-attributes-item__label"><?php esc_html_e('Width (in)', 'np-domain'); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo $real_width; ?></td>
</tr>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--attribute_real_height extra-data">
<th class="woocommerce-product-attributes-item__label"><?php esc_html_e('Height (in)', 'np-domain'); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo $real_height; ?></td>
</tr>
</tbody>
</table>
<?php
echo ob_get_clean();
}
}
add_action('woocommerce_product_additional_information', 'np_woocommerce_product_additional_information', 99, 1);
This solution is particularly useful for anyone looking to have both "shipping dimensions" (which are provided by default by Woocommerce) and "product dimensions" which would be displayed to the customer.
In the meantime you have answered your question yourself (+1) but for the first part of your question:
"For one, the code seems to be a little flawed in that it does not seem to save the custom length/width/height fields under the product variation area. I have checked and the flaw is in the original post's code too."
This may be useful for you or other users
To add custom fields to the product shipping tab you can use:
// Add custom fields to product shipping tab
function action_woocommerce_product_options_dimensions() {
// New option group
echo '</div><div class="options_group">';
// Fields
woocommerce_wp_text_input( array(
'id' => 'real_length',
'class' => 'short',
'label' => __( 'Actual Length', 'woocommerce' ),
'placeholder' => 'L',
'desc_tip' => 'true',
'description' => __( 'Product actual length (in inches).', 'woocommerce' ),
) );
woocommerce_wp_text_input( array(
'id' => 'real_width',
'class' => 'short',
'label' => __( 'Actual Width', 'woocommerce' ),
'placeholder' => 'W',
'desc_tip' => 'true',
'description' => __( 'Product actual width (in inches).', 'woocommerce' ),
) );
woocommerce_wp_text_input( array(
'id' => 'real_height',
'class' => 'short',
'label' => __( 'Actual Height', 'woocommerce' ),
'placeholder' => 'H',
'desc_tip' => 'true',
'description' => __( 'Product actual height (in inches).', 'woocommerce' ),
) );
}
add_action( 'woocommerce_product_options_dimensions', 'action_woocommerce_product_options_dimensions' );
However, if you want to display it on 1 line then use this instead of the above code!
function action_woocommerce_product_options_dimensions() {
global $product_object;
?>
<p class="form-field actual_dimensions_field dimensions_field">
<?php /* translators: WooCommerce dimension unit*/ ?>
<label for="product_real_length"><?php printf( __( 'Actual dimensions (%s)', 'woocommerce' ), get_option( 'woocommerce_dimension_unit' ) ); ?></label>
<span class="wrap">
<input id="product_real_length" placeholder="<?php esc_attr_e( 'Actual length', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="product_real_length" value="<?php echo esc_attr( wc_format_localized_decimal( $product_object->get_meta( 'product_real_length' ) ) ); ?>" />
<input id="product_real_width" placeholder="<?php esc_attr_e( 'Actual width', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="product_real_width" value="<?php echo esc_attr( wc_format_localized_decimal( $product_object->get_meta( 'product_real_width' ) ) ); ?>" />
<input id="product_real_height" placeholder="<?php esc_attr_e( 'Actual height', 'woocommerce' ); ?>" class="input-text wc_input_decimal last" size="6" type="text" name="product_real_height" value="<?php echo esc_attr( wc_format_localized_decimal( $product_object->get_meta( 'product_real_height' ) ) ); ?>" />
</span>
<?php echo wc_help_tip( __( 'Your text', 'woocommerce' ) ); ?>
</p>
<?php
}
add_action( 'woocommerce_product_options_dimensions', 'action_woocommerce_product_options_dimensions' );
To save fields you can use the woocommerce_admin_process_product_object hook, opposite the outdated woocommerce_process_product_meta hook:
// Save the custom fields values as meta data
function action_woocommerce_admin_process_product_object( $product ) {
// When isset, save
if ( isset( $_POST['product_real_length'] ) ) {
$product->update_meta_data( 'product_real_length', esc_attr( $_POST['product_real_length'] ) );
}
if ( isset( $_POST['product_real_width'] ) ) {
$product->update_meta_data( 'product_real_width', esc_attr( $_POST['product_real_width'] ) );
}
if ( isset( $_POST['product_real_height'] ) ) {
$product->update_meta_data( 'product_real_height', esc_attr( $_POST['product_real_height'] ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
So far adding and saving the fields to the product shipping tab.
To add and save the fields to the variations tab area, you can use the answer below. But, here are some remarks:
The woocommerce_product_after_variable_attributes hook is replaced by woocommerce_variation_options_dimensions as it is better positioned for your question
woocommerce_admin_process_variation_object replaces the outdated woocommerce_save_product_variation
Optional: to align the fields to the left change form-row-last to form-row-first
// Add fields
function action_woocommerce_variation_options_dimensions( $loop, $variation_data, $variation ) {
global $post;
// Parent product
$product_object = wc_get_product( $post->ID );
// Get meta
$parent_length = wc_format_localized_decimal( $product_object->get_meta( 'product_real_length' ) );
$parent_width = wc_format_localized_decimal( $product_object->get_meta( 'product_real_width' ) );
$parent_height = wc_format_localized_decimal( $product_object->get_meta( 'product_real_height' ) );
$real_length = get_post_meta( $variation->ID, 'variable_real_length', true );
$real_width = get_post_meta( $variation->ID, 'variable_real_width', true );
$real_height = get_post_meta( $variation->ID, 'variable_real_height', true );
?>
<p class="form-field form-row actual_dimensions_field dimensions_field hide_if_variation_virtual form-row-last">
<label for="product_real_length">
<?php
printf(
/* translators: %s: dimension unit */
esc_html__( 'Your text (%s)', 'woocommerce' ),
esc_html( get_option( 'woocommerce_dimension_unit' ) )
);
?>
</label>
<?php echo wc_help_tip( __( 'Your text', 'woocommerce' ) ); ?>
<span class="wrap">
<input id="product_real_length" placeholder="<?php echo $parent_length ? esc_attr( $parent_length ) : esc_attr__( 'Actual length', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="variable_real_length[<?php echo esc_attr( $loop ); ?>]" value="<?php echo esc_attr( wc_format_localized_decimal( $real_length ) ); ?>" />
<input id="product_real_width" placeholder="<?php echo $parent_width ? esc_attr( $parent_width ) : esc_attr__( 'Actual width', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="variable_real_width[<?php echo esc_attr( $loop ); ?>]" value="<?php echo esc_attr( wc_format_localized_decimal( $real_width ) ); ?>" />
<input id="product_real_height" placeholder="<?php echo $parent_height ? esc_attr( $parent_height ) : esc_attr__( 'Actual height', 'woocommerce' ); ?>" class="input-text wc_input_decimal last" size="6" type="text" name="variable_real_height[<?php echo esc_attr( $loop ); ?>]" value="<?php echo esc_attr( wc_format_localized_decimal( $real_height ) ); ?>" />
</span>
</p>
<?php
}
add_action( 'woocommerce_variation_options_dimensions', 'action_woocommerce_variation_options_dimensions', 10, 3 );
// Save
function action_woocommerce_admin_process_variation_object( $variation, $i ) {
// When isset, save
if ( isset( $_POST['variable_real_length'][$i] ) ) {
$variation->update_meta_data( 'variable_real_length', esc_attr( $_POST['variable_real_length'][$i] ) );
}
if ( isset( $_POST['variable_real_width'][$i] ) ) {
$variation->update_meta_data( 'variable_real_width', esc_attr( $_POST['variable_real_width'][$i] ) );
}
if ( isset( $_POST['variable_real_height'][$i] ) ) {
$variation->update_meta_data( 'variable_real_height', esc_attr( $_POST['variable_real_height'][$i] ) );
}
}
add_action( 'woocommerce_admin_process_variation_object', 'action_woocommerce_admin_process_variation_object', 10, 2 );
Result:

How to reference a custom product type value in HTML/PHP (WordPress & Woocommerce)

I have a custom button that instead of the 'add to cart' button. It should open a link to the brand: id='_brands_link' and in the button, and after "Bestel bij" there should be the brand name id='_brands_name' (see the created fields in the bone product):
// name
function add_brands_name_field() {
global $product_object;
?>
<div class='options_group show_if_brands'>
<?php
woocommerce_wp_text_input(
array(
'id' => '_brands_name',
'label' => __( 'Brand name', 'vicodemedia' ),
'value' => $product_object->get_meta( '_brands_name', true ),
'default' => '',
'placeholder' => 'Enter brand name',
'data_type' => 'text',
)
);
?>
</div>
<?php
}
// link
function add_brands_link_field() {
global $product_object;
?>
<div class='options_group show_if_brands'>
<?php
woocommerce_wp_text_input(
array(
'id' => '_brands_link',
'label' => __( 'Product link', 'vicodemedia' ),
'value' => $product_object->get_meta( '_brands_link', true ),
'default' => '',
'placeholder' => 'Enter product link',
'data_type' => 'text',
)
);
?>
</div>
<?php
}
add_action( 'woocommerce_product_options_general_product_data', 'add_brands_link_field');
add_action( 'woocommerce_product_options_general_product_data', 'add_brands_name_field');
I have a function of this buttons now:
if (! function_exists( 'woocommerce_brands_add_to_cart' ) ) {
function brands_add_to_cart() {
//wc_get_template( 'single-product/add-to-cart/brand.php' );
do_action( 'woocommerce_before_add_to_cart_form' );
?>
<form class="cart" action="https://www.google.com">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<button style="padding: 18px 40px 18px 40px; height: 55px;" type="submit" name="add-to-cart" value="" class="single_add_to_cart_button button alt">
Bestel bij Google <?php ?>
</button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php
}
add_action('woocommerce_brands_add_to_cart', 'brands_add_to_cart');
}
So in action instead of https://www.google.com there should be _brands_link. And instead of "Google", which is in the button, there should be _brands_name
Here's all the code for my custom product type, just in case:
// BEGIN CODE
class WC_Product_brands extends WC_Product_Simple {
// Return the product type
public function get_type() {
return 'brands';
}
}
// add the product type to the dropdown
function add_type_to_dropdown( $types ) {
$types['brands'] = __( 'Brand product', 'vicodemedia' );
return $types;
}
add_filter( 'product_type_selector', 'add_type_to_dropdown');
// add the product type as a taxonomy
function install_taxonomy() {
// If there is no brands product type taxonomy, add it.
if ( ! get_term_by( 'slug', 'brands', 'product_type' ) ) {
wp_insert_term( 'brands', 'product_type' );
}
}
register_activation_hook( __FILE__, 'install_taxonomy');
// name
function add_brands_name_field() {
global $product_object;
?>
<div class='options_group show_if_brands'>
<?php
woocommerce_wp_text_input(
array(
'id' => '_brands_name',
'label' => __( 'Brand name', 'vicodemedia' ),
'value' => $product_object->get_meta( '_brands_name', true ),
'default' => '',
'placeholder' => 'Enter brand name',
'data_type' => 'text',
)
);
?>
</div>
<?php
}
// link
function add_brands_link_field() {
global $product_object;
?>
<div class='options_group show_if_brands'>
<?php
woocommerce_wp_text_input(
array(
'id' => '_brands_link',
'label' => __( 'Product link', 'vicodemedia' ),
'value' => $product_object->get_meta( '_brands_link', true ),
'default' => '',
'placeholder' => 'Enter product link',
'data_type' => 'text',
)
);
?>
</div>
<?php
}
add_action( 'woocommerce_product_options_general_product_data', 'add_brands_link_field');
add_action( 'woocommerce_product_options_general_product_data', 'add_brands_name_field');
// Generl Tab not showing up
add_action( 'woocommerce_product_options_general_product_data', function(){
echo '<div class="options_group show_if_brands clear"></div>';
} );
// add show_if_advanced calass to options_group
function enable_product_js() {
global $post, $product_object;
if ( ! $post ) { return; }
if ( 'product' != $post->post_type ) :
return;
endif;
$is_brands = $product_object && 'brands' === $product_object->get_type() ? true : false;
?>
<script type='text/javascript'>
jQuery(document).ready(function () {
//for link tab
jQuery('#general_product_data .pricing').addClass('show_if_brands');
<?php if ( $is_brands ) { ?>
jQuery('#general_product_data .pricing').show();
<?php } ?>
});
</script>
<style>
.woocommerce_options_panel .options_group {
border-top: 0px;
border-bottom: 0px;
}
</style>
<?php
}
add_action( 'admin_footer', 'enable_product_js');
// save data on submission
function save_brands_link( $post_id ) {
$link = isset( $_POST['_brands_link'] ) ? sanitize_text_field( $_POST['_brands_link'] ) : '';
update_post_meta( $post_id, '_brands_link', $link );
}
add_action( 'woocommerce_process_product_meta_brands', 'save_brands_link');
// save data on submission
function save_brands_name( $post_id ) {
$name = isset( $_POST['_brands_name'] ) ? sanitize_text_field( $_POST['_brands_name'] ) : '';
update_post_meta( $post_id, '_brands_name', $name );
}
add_action( 'woocommerce_process_product_meta_brands', 'save_brands_name');
/*
//button
function custom_product_button() {
?>
<a href="<?php the_field('_brands_link'); ?>" class="button alt">Bestel bij
<?php the_field('_brands_name'); ?></a>
<?php
}
add_action( 'woocommerce_single_product_summary', 'custom_product_button', 30 );
*/
if (! function_exists( 'woocommerce_brands_add_to_cart' ) ) {
function brands_add_to_cart() {
//wc_get_template( 'single-product/add-to-cart/brand.php' );
do_action( 'woocommerce_before_add_to_cart_form' );
?>
<form class="cart" action="https://www.google.com">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<button style="padding: 18px 40px 18px 40px; height: 55px;" type="submit" name="add-to-cart" value="" class="single_add_to_cart_button button alt">
Bestel bij Google <?php ?>
</button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php
}
add_action('woocommerce_brands_add_to_cart', 'brands_add_to_cart');
}
// END CODE
CONCLUSION:
I need to understand how to correctly reference the objects of the classes I created for _brands_link and _brands_name. I do not understand how this should be done, so that it would be displayed in HTML as text (_brands_name) and as a link in the form action = "" (_brands_link):
function brands_add_to_cart() {
do_action( 'woocommerce_before_add_to_cart_form' );
?>
<form class="cart" action="_brands_link">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<button style="padding: 18px 40px 18px 40px; height: 55px;" type="submit" name="add-to-cart" value="" class="single_add_to_cart_button button alt">
Bestel bij _brands_name
</button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php
}
Since are save them as post meta data, you could get back as meta too
if (! function_exists( 'woocommerce_brands_add_to_cart' ) ) {
function brands_add_to_cart() {
//wc_get_template( 'single-product/add-to-cart/brand.php' );
do_action( 'woocommerce_before_add_to_cart_form' );
?>
<form class="cart" action="<?php echo get_post_meta(get_the_ID(), '_brands_link', true); ?>">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<button style="padding: 18px 40px 18px 40px; height: 55px;" type="submit" name="add-to-cart" value="" class="single_add_to_cart_button button alt">
Bestel bij <?php echo get_post_meta(get_the_ID(), '_brands_name', true); ?>
</button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php
}
add_action('woocommerce_brands_add_to_cart', 'brands_add_to_cart');
}

Woo-commerce Registration form add otp as custom field

I want to add otp when user register on woo-commerce registration page, i have added custom field for phone number but i am not able to get any sources for otp in word-press i have my own API please if someone can share any resource
for finding it
It will be very helpful ...
function iconic_get_account_fields() {
return apply_filters( 'iconic_account_fields', array(
'user_url' => array(
'type' => 'text',
'label' => __( 'Phone' ),
'placeholder' => __( 'E.g. 9861234567', 'iconic' ),
'required' => true,
),
) );
}
function iconic_print_user_frontend_fields() {
$fields = iconic_get_account_fields();
foreach ( $fields as $key => $field_args ) {
woocommerce_form_field( $key, $field_args );
}
}
add_action( 'woocommerce_register_form', 'iconic_print_user_frontend_fields', 1 );
add_action( 'woocommerce_edit_account_form', 'iconic_print_user_frontend_fields', 10 ); // my account
function iconic_print_user_admin_fields() {
$fields = iconic_get_account_fields();
?>
<h2><?php _e( 'Additional Information', 'iconic' ); ?></h2>
<table class="form-table" id="iconic-additional-information">
<tbody>
<?php foreach ( $fields as $key => $field_args ) { ?>
<tr>
<th>
<label for="<?php echo $key; ?>"><?php echo $field_args['label']; ?></label>
</th>
<td>
<?php $field_args['label'] = false; ?>
<?php woocommerce_form_field( $key, $field_args ); ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
}
add_action( 'show_user_profile', 'iconic_print_user_admin_fields', 30 ); // admin: edit profile
add_action( 'edit_user_profile', 'iconic_print_user_admin_fields', 30 );
.

Display profile fields in an extra tab in BuddyPress (Wordpress) using bp-custom.php

I created an extra tab on the user’s profile page and I would like to display the profile fields in it.
This is how I created the extra tab in the first place, I created a new bp-custom.php with this inside
function profile_new_nav_item() {
global $bp;
bp_core_new_nav_item(
array(
'name' => 'About',
'slug' => 'about',
'default_subnav_slug' => 'extra_sub_tab', // We add this submenu item below
'screen_function' => 'view_manage_tab_main'
)
);
}
add_action( 'bp_setup_nav', 'profile_new_nav_item', 10 );
function view_manage_tab_main() {
add_action( 'bp_template_content', 'bp_template_content_main_function' );
bp_core_load_template( 'template_content' );
}
function bp_template_content_main_function() {
if ( ! is_user_logged_in() ) {
wp_login_form( array( 'echo' => true ) );
}
}
function profile_new_subnav_item() {
global $bp;
bp_core_new_subnav_item( array(
'name' => 'Extra Sub Tab',
'slug' => 'extra_sub_tab',
'parent_url' => $bp->loggedin_user->domain . $bp->bp_nav[ 'about' ][ 'slug' ] . '/',
'parent_slug' => $bp->bp_nav[ 'about' ][ 'slug' ],
'position' => 10,
'screen_function' => 'view_manage_sub_tab_main'
) );
}
add_action( 'bp_setup_nav', 'profile_new_subnav_item', 10 );
function view_manage_sub_tab_main() {
add_action( 'bp_template_content', 'bp_template_content_sub_function' );
bp_core_load_template( 'template_content' );
}
function bp_template_content_sub_function() {
if ( is_user_logged_in() ) {
echo 'you are here';
} else {
wp_login_form( array( 'echo' => true ) );
}
}
See where it says echo 'you are here'; in the last function, I would like to display the profile fields there. I have the code below but I can’t figure out how to embed it there.
If I echo bp_the_profile_field() I get the following error
Fatal error: Call to a member function the_profile_field() on null
The code below works perfectly on another page, it fetches all the available fields.
<div class="bp-widget <?php bp_the_profile_group_slug(); ?>">
<h4><?php bp_the_profile_group_name(); ?></h4>
<table class="profile-fields">
<?php while ( bp_profile_fields() ) : bp_the_profile_field(); ?>
<?php if ( bp_field_has_data() ) : ?>
<tr<?php bp_field_css_class(); ?>>
<td class="label"><?php bp_the_profile_field_name(); ?></td>
<td class="data"><?php bp_the_profile_field_value(); ?></td>
</tr>
<?php endif; ?>
<?php
do_action( 'bp_profile_field_item' ); ?>
<?php endwhile; ?>
</table></div>
Replace this:
echo 'you are here';
With this:
bp_get_template_part( 'members/single/profile/profile-loop' );

Wordpress Theme Options Page - Radio Buttons not updating correctly

I am currently working on the Theme Options Page of my Wordpress Theme.
As a base I'm using the "SAMPLE WORDPRESS THEME OPTIONS PAGE" by Ian Steward:
The Situation:
I've implemented two sets of radio-buttons like so:
The Problem:
They work as planned, the chosen options are applied.
Only problem: after saving the chosen radio buttons aren't selected anymore.
For the image above: If I would change both radio buttons and click save only the very first radio button is selected, after the page has loaded again.
The Code:
From the theme-options.php:
// Normal/Fixed Navigation Options
$fixed_navigation_options = array(
'Normal' => array(
'value' => ' ',
'label' => __( 'Normal', 'arrowcatch' )
),
'Fixed' => array(
'value' => 'is-fixed',
'label' => __( 'Fixed', 'arrowcatch' )
)
);
// Full-width/Contained Navigation Options
$contained_navigation_options = array(
'Full-width' => array(
'value' => ' ',
'label' => __( 'Full-width', 'arrowcatch' )
),
'Contained' => array(
'value' => 'is-contained',
'label' => __( 'Contained', 'arrowcatch' )
)
);
function arrowcatch_theme_options_page() {
global $select_options, $fixed_navigation_options, $contained_navigation_options;
if ( ! isset( $_REQUEST['settings-updated'] ) )
$_REQUEST['settings-updated'] = false; ?>
<div class="wrap">
<?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
<div class="updated fade">
<p><strong>Options saved!</strong></p>
</div>
<?php endif; ?>
<!-- Normal/Fixed Navigation -->
<tr valign="top"><th scope="row"><?php _e( 'Navigation Normal/Fixed', 'arrowcatch' ); ?></th>
<td>
<fieldset><legend class="screen-reader-text"><span><?php _e( 'Navigation Normal/Fixed', 'arrowcatch' ); ?></span></legend>
<?php
if ( ! isset( $checked ) )
$checked = '';
foreach ( $fixed_navigation_options as $option ) {
$fixed_navigation_setting = $options['fixed_navigation'];
if ( '' != $fixed_navigation_setting ) {
if ( $options['fixed_navigation'] == $option['value'] ) {
$checked = "checked=\"checked\"";
} else {
$checked = '';
}
}
?>
<label class="description"><input type="radio" name="arrowcatch_theme_options[fixed_navigation]" value="<?php esc_attr_e( $option['value'] ); ?>" <?php echo $checked; ?> /> <?php echo $option['label']; ?></label><br />
<?php
}
?>
</fieldset>
</td>
</tr>
<!-- Full-width/Contained Navigation -->
<tr valign="top"><th scope="row"><?php _e( 'Navigation Full-width/Contained', 'arrowcatch' ); ?></th>
<td>
<fieldset><legend class="screen-reader-text"><span><?php _e( 'Navigation Full-width/Contained', 'arrowcatch' ); ?></span></legend>
<?php
if ( ! isset( $checked ) )
$checked = '';
foreach ( $contained_navigation_options as $option ) {
$contained_navigation_setting = $options['contained_navigation'];
if ( '' != $contained_navigation_setting ) {
if ( $options['fixed_navigation'] == $option['value'] ) {
$checked = "checked=\"checked\"";
} else {
$checked = '';
}
}
?>
<label class="description"><input type="radio" name="arrowcatch_theme_options[contained_navigation]" value="<?php esc_attr_e( $option['value'] ); ?>" <?php echo $checked; ?> /> <?php echo $option['label']; ?></label><br />
<?php
}
?>
</fieldset>
</td>
</tr>
Hope that's all the code thats needed.
Not sure what's going on there.
I'm very thankfull for any push in the right direction.
Thank you!

Categories