相关疑难解决方法(0)

产品设置中的自定义复选框,选中时显示自定义字段

我目前正在使用 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)

php wordpress product custom-fields woocommerce

3
推荐指数
1
解决办法
7148
查看次数

标签 统计

custom-fields ×1

php ×1

product ×1

woocommerce ×1

wordpress ×1