I am currently working on a WordPress eCommerce website, where the preferred shopping platform is WooCommerce.
Is there a way to create a Checkbox within the WooCommerce Product Dashboard, which Outputs a piece of coding from within the functions.php
file, when the Checkbox is selected?
The coding, within the functions.php
would create a Text Box on the Product Page.
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:
<?php
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?
Aucun commentaire:
Enregistrer un commentaire