特定のカテゴリが選択されている場合、チェックアウトページのカスタムドロップダウンフィールドを非表示にする方法はありますか?

lars

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;
}
ギリッシュ

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ