我目前正在使用 WooCommerce 开发 WordPress 电子商务网站。
我在产品编辑页面常规设置中创建了一个自定义复选框。
我还有一个代码片段,用于在单个产品页面中显示自定义文本字段。
现在,我希望当为产品选中此自定义复选框(在其设置中)时,在单个产品页面中显示此自定义文本字段。
到目前为止我的编码:
为了在 WooCommerce 产品仪表板中创建复选框,我在文件中输入了以下代码functions.php:
<?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 )) …Run Code Online (Sandbox Code Playgroud)