如何将复选框设置为在WooCommerce产品管理中始终处于选中状态? [英] How I set a checkbox to be always checked in WooCommerce product admin?

查看:36
本文介绍了如何将复选框设置为在WooCommerce产品管理中始终处于选中状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将复选框设置为始终选中?我正在使用一个插件"WooCommerce产品费用",广告收费的产品。它有一个复选框,当选中时,它会计算购物车中添加的每种产品的费用。我想要实现的是将此复选框设置为始终选中每个产品,但我找不到正确的方法。我添加了$Checked,但没有任何反应.

这是我的代码的摘录:

    // Check Box - Fee Multiply Option
    woocommerce_wp_checkbox( array( 
        'id'=> 'product-fee-multiplier[' . $variation->ID . ']', 
        'label' => __('Multiply Fee by Quantity', 'woocommerce-product-fees' ),
        'value' => get_post_meta( $variation->ID, 'product-fee-multiplier', true ), 
        'wrapper_class' => "product-fee-multiplier" , 
        'required'  => true
    ), $checked );

    do_action( 'wcpf_add_variation_settings' );

}

public function save_variation_settings_fields( $post_id ) {
    $another_field_updated = false;
    // Text Field - Fee Name
    $product_fee_name_text_field = $_POST['product-fee-name'][ $post_id ];
        if( ! empty( $product_fee_name_text_field ) || get_post_meta( 
        $post_id, 'product-fee-name', true ) != '' ) {
            update_post_meta( $post_id, 'product-fee-name', esc_attr( $product_fee_name_text_field ) );
            $another_field_updated = true;
        }

推荐答案

使用wooCommercewoocommerce_wp_checkbox()表单域函数时,如果希望默认情况下始终选中该复选框,您将使用'cbvalue'数组参数,如下所示:

// Check Box - Fee Multiply Option
woocommerce_wp_checkbox( array( 
    'id'            => 'product-fee-multiplier[' . $variation->ID . ']', 
    'label'         => __('Multiply Fee by Quantity', 'woocommerce-product-fees' ),
    'value'         => get_post_meta( $variation->ID, 'product-fee-multiplier', true ), 
    'cbvalue'       => get_post_meta( $variation->ID, 'product-fee-multiplier', true ), 
    'wrapper_class' => "product-fee-multiplier" , 
    'required'      => true
), $checked );

do_action( 'wcpf_add_variation_settings' );

这将允许您始终选中"目标"复选框。


从WooCommerce 3开始,这里有一个简单完整的";product";帖子类型的工作示例:

// Displaying quantity setting fields on admin product pages
add_action( 'woocommerce_product_options_pricing', 'add_custom_field_product_options_pricing' );
function add_custom_field_product_options_pricing() {
    global $product_object;

    echo '</div><div class="options_group">';

    $values = $product_object->get_meta('_cutom_meta_key');

    woocommerce_wp_checkbox( array( // Checkbox.
        'id'            => '_cutom_meta_key',
        'label'         => __( 'Custom label', 'woocommerce' ),
        'value'         => empty($values) ? 'yes' : $values,
        'description'   => __( 'Enable this to make something.', 'woocommerce' ),
    ) );
}

// Save quantity setting fields values
add_action( 'woocommerce_admin_process_product_object', 'save_custom_field_product_options_pricing' );
function save_custom_field_product_options_pricing( $product ) {
    $product->update_meta_data( '_cutom_meta_key', isset($_POST['_cutom_meta_key']) ? 'yes' : 'no' );
}

代码放在活动子主题(或活动主题)的functions.php文件中。已测试并正常工作。

这篇关于如何将复选框设置为在WooCommerce产品管理中始终处于选中状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆