如果选择了特定类别,是否可以在结帐页面上隐藏自定义下拉字段

在WooCommerce中,我试图删除“个性化”产品类别的不必要的结帐下拉服装字段

/ 这是我的服装领域 /

add_action( 'woocommerce_before_order_notes','add_checkout_custom_fields',20,1 );
function add_checkout_custom_fields( $checkout) {
    $domain = 'woocommerce'; // The domain slug

    echo '<h2>'.__( 'Populating In Woocommerce Checkout',$domain ).'</h2>';

    // First Select field (Populating options one)
    woocommerce_form_field( 'populating_one',array(
        'type'          => 'select','label'         => __( 'Populating options one',$domain),'class'         => array( 'form-row-wide' ),'required'       => true,'options'       => array(
            ''  => __( 'Please select a value',$domain ),'A' => __( 'A','B' => __( 'B','C' => __( 'C',),$checkout->get_value( 'populating_one' ) );

    // Default option value
    $default_option2 = __( 'Please select a value',$domain );

    // Dynamic select field options for Javascript/jQuery
    $options_0 = array( '' => $default_option2 );
    $options_a = array(
        ''  => $default_option2,'A1' => __( 'A1','A2' => __( 'A2','A3' => __( 'A3','A4' => __( 'A4',);
    $options_b = array(
        ''  => $default_option2,'B1' => __( 'B1','B2' => __( 'B2','B3' => __( 'B3','B4' => __( 'B4',);
    $options_c = array(
        ''  => $default_option2,'C1' => __( 'C1','C2' => __( 'C2','C3' => __( 'C3','C4' => __( 'C4',);

    // Second Select field (Populating options two)
    woocommerce_form_field( 'populating_two','label'         => __( 'Populating options two','options'       => $options_0,$checkout->get_value( 'populating_two' ) );

    $required = esc_attr__( 'required','woocommerce' );

    // jQuery code
?>
    <script>
    jQuery(function($){
        var op0 = <?php echo json_encode($options_0); ?>,opa = <?php echo json_encode($options_a); ?>,opb = <?php echo json_encode($options_b); ?>,opc = <?php echo json_encode($options_c); ?>,select1 = 'select[name="populating_one"]',select2 = 'select[name="populating_two"]';


        function dynamicSelectOptions( opt ){
            var options = '';
            $.each( opt,function( key,value ){
                options += '<option value="'+key+'">'+value+'</option>';
            });
            $(select2).html(options);
        }

        // 1. When dom is loaded we add the select field option for "A" value
        // => Disabled (optional) — Uncomment below to enable
        // dynamicSelectOptions( opa );

        // 2. On live selection event on the first dropdown
        $(select1).change(function(){
            if( $(this).val() == 'A' )
                dynamicSelectOptions( opa );
            else if( $(this).val() == 'B' )
                dynamicSelectOptions( opb );
            else if( $(this).val() == 'C' )
                dynamicSelectOptions( opc );
            else
                dynamicSelectOptions( op0 ); // Reset to default
        });
    });
    </script>
<?php
}

/ 我的功能 /

add_filter( 'woocommerce_checkout_fields','conditionally_remove_checkout_fields',25,1 );
function conditionally_remove_checkout_fields( $fields ) {


    $categories = array('personalized');

    $found = false;


    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( has_term( $categories,'product_cat',$cart_item['product_id'] ) ) {
            $found = true;
            break;
        }
    }


    if ( $found ) {

        // hide the billing fields


        // hide the additional information section
        add_filter( 'woocommerce_checkout_custom_field','__return_false' );
    }
    return $fields;
}
kshsc 回答:如果选择了特定类别,是否可以在结帐页面上隐藏自定义下拉字段

您可以使用unset删除该字段。例如,删除两个选择字段

add_filter( 'woocommerce_checkout_fields','conditionally_remove_checkout_fields',25,1 );
function conditionally_remove_checkout_fields( $fields ) {


    $categories = array('personalized');

    $found = false;


    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( has_term( $categories,'product_cat',$cart_item['product_id'] ) ) {
            $found = true;
            break;
        }
    }


    if ( $found ) {
die('check if this true');
        // hide the billing fields
        unset($fields['populating_one']);
        unset($fields['populating_two']);

        // hide the additional information section
        add_filter( 'woocommerce_checkout_custom_field','__return_false' );
    }
    return $fields;
}
本文链接:https://www.f2er.com/3140337.html

大家都在问