Есть ли способ скрыть настраиваемое раскрывающееся поле на странице оформления заказа, если выбрана конкретная категория - PullRequest
0 голосов
/ 08 ноября 2019

В 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;
}

1 Ответ

0 голосов
/ 08 ноября 2019

Вы можете использовать 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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...