2017-10-12 19 views
1

私のWooCommerceストアには2種類の製品があります。Wooocommerceのチェックボックスをチェックするとチェックアウトフィールドを表示/非表示

チェックアウトページで、顧客が通常の製品を購入した場合、彼は通常の請求フィールドのみを記入します。

しかし、この顧客が2番目の製品を購入した場合、追加のフィールドが表示されるチェックボックスをチェックする必要があります。

誰でもこの問題を解決できますか?

+0

あなたは、対応する製品が追加されている場合だけで、追加のフィールドを表示し、まったくチェックボックスを必要としませんカート。他の製品が追加されたら非表示にします。 –

+0

私の平均は、私はwordpressを使用してマネージャー製品と支払いのためのwoocommerceを交互に使用しています。 CHECK OUTページでユーザーチェックボックスをオンにすると、追加フィールドを表示し、ユーザーがチェックしない場合は非表示にします。 –

+0

自分でコードを試してみてください。そして、私はあなたにそれを手伝ってくれますが、少なくとも試みています。 –

答えて

0

あなたは関数の中で、あなたの2つの製品のIDSを設定し、カスタムチェックアウトフィールドの正しいテキストを配置する必要があります。両方の製品がカートに入っている場合、条件付きでチェックボックスが表示されます。顧客がチェックボックスを選択しますと、追加のテキストフィールドが表示されます。

// Add fields to the checkout 
add_action('woocommerce_after_order_notes', 'custom_checkout_fields'); 
function custom_checkout_fields($checkout) { 

    // Set HERE below your different product IDs 
    $product_1_id = 37; // Normal product 
    $product_2_id = 67; // Specific product (second product) 
    $has_id_1 = $has_id_2 = false; 

    // Check if products are in cart 
    foreach(WC()->cart->get_cart() as $cart_item){ 
     if($cart_item['product_id'] == $product_1_id) $has_id_1 = true; 
     if($cart_item['product_id'] == $product_2_id) $has_id_2 = true; 
    } 

    // Display conditionally Custom checkout Fields (when both products are in cart) 
    if($has_id_1 && $has_id_2){ 

     echo '<div id="custom_checkout_fields">'; 

     // The Check box 
     woocommerce_form_field('my_checkbox', array(
      'type'   => 'checkbox', 
      'class'   => array('my-field-class form-row-wide'), 
      'label'   => __('Fill in this field'), 
      'placeholder' => __('Enter something'), 
      ), $checkout->get_value('my_checkbox')); 

     // The hidden field 
     woocommerce_form_field('my_text_field', array(
      'type'   => 'text', 
      'class'   => array('my-field-class form-row-wide'), 
      'label'   => __('Fill in this field'), 
      'placeholder' => __('Enter something'), 
      ), $checkout->get_value('my_text_field')); 

     echo '</div>'; 

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

     // The jQuery Script 
     ?> 
     <script> 
      jQuery(function($){ 
       // Initialising variables 
       var textField = 'p#my_text_field_field', 
        checkboxField = 'input[name^="my_checkbox"]', 
        required = '<abbr class="required" title="<?php echo $required; ?>">*</abbr>'; // Required html 

       // Initialising at start (Hidding the text field) 
       $(textField).hide(function(){ 
        $(this).removeClass("validate-required"); 
        $(this).removeClass("woocommerce-validated"); 
        $(this).removeClass("woocommerce-invalid woocommerce-invalid-required-field"); 
        if($(textField+' > label > abbr').html() != undefined) 
         $(textField+' label > .required').remove(); 
       }); 

       // When Checkbox is checked/unchecked (Live event) 
       $(checkboxField).click(function() { 
        if($(this).prop('checked') == true) 
         $(textField).show(function(){ 
          $(this).addClass("validate-required"); 
          $(this).removeClass("woocommerce-validated"); 
          $(this).removeClass("woocommerce-invalid woocommerce-invalid-required-field"); 
          if($(textField+' > label > abbr').html() == undefined) 
           $(textField+' label').append(required); 
         }); 
        else 
         $(textField).hide(function(){ 
          $(this).removeClass("validate-required"); 
          $(this).removeClass("woocommerce-validated"); 
          $(this).removeClass("woocommerce-invalid woocommerce-invalid-required-field"); 
          if($(textField+' > label > abbr').html() != undefined) 
           $(textField+' label > .required').remove(); 
         }); 
       }); 
      }); 
     </script> 
     <?php 

    } 
} 

// Update the order meta with field value 
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta'); 
function my_custom_checkout_field_update_order_meta($order_id) { 
    if (! empty($_POST['my_text_field'])) { 
     update_post_meta($order_id, __('My Field'), sanitize_text_field($_POST['my_text_field'])); 
    } 
} 

コードは、あなたのアクティブな子テーマのfunction.phpファイルに(アクティブテーマや任意のプラグインファイルに)行きます。

は、テストの結果、WooCommerce 3+


関連公式開発者向けドキュメントに作品:Customizing checkout fields using actions and filters

関連する問題