问题
I am currently working on a WordPress eCommerce website, using WooCommerce.
I have created a custom Checkbox within the Product edit pages general settings.
I have also a code snippet that is displaying a custom text field in single product pages.
Now I would like when this custom Checkbox is checked for a product (in it's settings), to display this custom text field in single product pages.
The Coding I have so far:
To create the Checkbox, within the WooCommerce Product Dashboard, I have entered the following code into the functions.php
file:
<?php
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields(){
global $woocommerce, $post;
echo '<div class="product_custom_field">';
// Custom Product Checkbox Field
woocommerce_wp_checkbox(
array(
'id' => '_custom_product_checkbox_field',
'placeholder' => 'Custom Product Checkbox Field',
'label' => __('Custom Product Checkbox Field', 'woocommerce'),
'desc_tip' => 'true'
)
);
echo '</div>';
}
// Saving Values
function woocommerce_product_custom_fields_save($post_id){
// Custom Product Text Field
$woocommerce_custom_product_checkbox_field = $_POST['_custom_product_checkbox_field'];
if (!empty($woocommerce_custom_product_checkbox_field ))
update_post_meta($post_id, '_custom_product_checkbox_field', esc_attr($woocommerce_custom_product_checkbox_field ));
}
?>
The coding I have placed, in the functions.php
file, as to Output the relevant Custom Text Box, is as follows:
function add_engrave_text_field() {
if (is_single('product-url-a')) {
echo 'Enter your chosen letters: <span id="character_count"></span>
<div><table class="variations" cellspacing="0">
<tbody>
<tr>
<td class="value"><label class="product-custom-text-label" for="custom_text">Custom Text</label></td>
<td class="value">
<label><input type="text" class="product-counter" name="engrave_text" placeholder="Enter Your Custom Letters ..." maxlength="3" /></label>
</td>
</tr>
</tbody>
</table></div>';
}
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_engrave_text_field', 0 );
What would be my next step, so that the Custom Text Box coding will only be Outputted when the Checkbox is selected?
I realise that I could put the code for the Custom Text Box into the content-single-product
, however I would ideally not want to do this as it is part of a large group of coding which works out the pricing of the Custom Lettering etc.
Additional Question:
The website is made up of 5 Product Categories. Only Products in one of these Categories allow for Custom Lettering. At present, I am having to manually apply the above coding to each individual product, as I am having to insert the individual slugs into if (is_single('product-slug-a'))
Is there a way I could change 'product-slug-a' so that I only have to enter the code once into the functions.php
file and it is called for every Product within just one of the Product Categories?
回答1:
Updated:
I have revisited a bit your code, simplifying things, removing unnecessary code. I have added the necessary code to make this "Engraving field" to be displayed on single product pages only when the checkbox setting option has been checked.
For your last question check at the end (for a product category without checkbox settings).
Here is the code:
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'product_custom_fields_add');
function product_custom_fields_add(){
global $post;
echo '<div class="product_custom_field">';
// Custom Product Checkbox Field
woocommerce_wp_checkbox( array(
'id' => '_engrave_text_option',
'desc' => __('set custom Engrave text field', 'woocommerce'),
'label' => __('Display custom Engrave text field', 'woocommerce'),
'desc_tip' => 'true'
));
echo '</div>';
}
// Save Fields
add_action('woocommerce_process_product_meta', 'product_custom_fields_save');
function product_custom_fields_save($post_id){
// Custom Product Text Field
$engrave_text_option = isset( $_POST['_engrave_text_option'] ) ? 'yes' : 'no';
update_post_meta($post_id, '_engrave_text_option', esc_attr( $engrave_text_option ));
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_engrave_text_field', 0 );
function add_engrave_text_field() {
global $post;
// If is single product page and have the "engrave text option" enabled we display the field
if ( is_product() && get_post_meta( $post->ID, '_engrave_text_option', true ) == 'yes' ) {
?>
<div>
<label class="product-custom-text-label" for="engrave_text"><?php _e( 'Engraving option:', 'woocommerce'); ?><br>
<input style="min-width:220px" type="text" class="product-counter" name="engrave_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" maxlength="3" />
</label>
</div><br>
<?php
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works.
Checkbox output and save: Adding programatically data option tab to admin product pages Metabox
You will get something like this when the checkbox option has been selected in the product settings:
Make it work for a product category or a product tag (without settings checkbox):
add_action( 'woocommerce_before_add_to_cart_button', 'add_engrave_text_field', 0 );
function add_engrave_text_field() {
global $post;
// HERE set the term Ids, slug or name (or an array of values)
$categories = array( 'clothing', 'music' );
$taxonomy = 'product_cat'; // (For product tags use 'product_tag' instead)
// If is single product page and have the "engrave text option" enabled we display the field
if ( is_product() && has_term( $categories, 'product_cat', $post->ID ) ) {
?>
<div>
<label class="product-custom-text-label" for="engrave_text"><?php _e( 'Engraving option:', 'woocommerce'); ?><br>
<input style="min-width:220px" type="text" class="product-counter" name="engrave_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" maxlength="3" />
</label>
</div><br>
<?php
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works.
来源:https://stackoverflow.com/questions/47463330/custom-checkbox-in-product-settings-that-displays-a-custom-field-when-checked