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

问题描述 投票:0回答:1

在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', $domain ),
            'B' => __( 'B', $domain ),
            'C' => __( 'C', $domain ),
        ),
    ),
    $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', $domain ),
        'A2' => __( 'A2', $domain ),
        'A3' => __( 'A3', $domain ),
        'A4' => __( 'A4', $domain ),
    );
    $options_b = array(
        ''  => $default_option2,
        'B1' => __( 'B1', $domain ),
        'B2' => __( 'B2', $domain ),
        'B3' => __( 'B3', $domain ),
        'B4' => __( 'B4', $domain ),
    );
    $options_c = array(
        ''  => $default_option2,
        'C1' => __( 'C1', $domain ),
        'C2' => __( 'C2', $domain ),
        'C3' => __( 'C3', $domain ),
        'C4' => __( 'C4', $domain ),
    );

    // Second Select field (Populating options two)
    woocommerce_form_field( 'populating_two', array(
        'type'          => 'select',
        'label'         => __( 'Populating options two', $domain ),
        'class'         => array( 'form-row-wide' ),
        'required'       => true,
        '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;
}

[在WooCommerce中,我试图删除产品类别“个性化”的不必要的结帐下拉服装字段,这是我的服装字段/ add_action('woocommerce_before_order_notes','...

php wordpress woocommerce
1个回答
0
投票

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

推荐问答
热门问答
最新问答