2017-06-21 2 views
0

WooCommerceの多段階チェックアウトがあります。 1つのチェックアウトステップは保険に適用されますが、この保険はレンタルカテゴリにのみ適用されます。カートにカテゴリが含まれている場合にのみWooCommerceチェックアウトテンプレートを表示

レンタル商品がカートに入っている場合にのみ、保険手続きを表示します。 Checking if the WooCommerce Cart Contains a Product Categoryで素晴らしい記事を見つけましたが、我々はチェックアウトのステップをロードするために、当社のadd_actionスニペットを配置する際のコードは、所望の結果を与えていません。ここで

enter image description here

は、カスタムテンプレート(

add_action('woocommerce_multistep_checkout_after_shipping', 'add_my_insurance_step_with_new_field'); 
     function add_my_insurance_step_with_new_field($checkout) { 
       wc_get_template('checkout/insurance.php', array('checkout' => $checkout)); 
      } 
のための私達のadd_action呼び出しです

そして、ここではSkyVergeからコード内に配置されています

// set our flag to be false until we find a product in that category 
$cat_check = false; 

// check each cart item for our category 
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { 

    $product = $cart_item['data']; 

    // replace 'membership' with your category's slug 
    if (has_term('rentals', 'product_cat', $product->id)) { 
     $cat_check = true; 
     // break because we only need one "true" to matter here 
     break; 
    } 
} 

// if a product in the cart is in our category, do something 
if ($cat_check) { 
    // we have the category, do what we want 
    add_action('woocommerce_multistep_checkout_after_shipping', 'add_my_insurance_step_with_new_field'); 
     function add_my_insurance_step_with_new_field($checkout) { 
       wc_get_template('checkout/insurance.php', array('checkout' => $checkout)); 
      } 
} 

だから、非常に近いわからないものをC! ould間違っている。あなたの助けのための

おかげで、 -L

+0

私はこの優秀な記事を[WooCommerceの条件付きチェックアウトフィールドの作成方法](https://wordimpress.com/create-conditional-checkout-fields-woocommerce/)にも掲載しました。 –

答えて

0

これは修正をしました。 $cat_checkfalseに設定し、カート内の各製品を実行し、カートにカテゴリのスラッグ(rentals)があるかどうかを確認します。真の場合は、次のifのステートメントを実行します。

チェックアウトオーダー情報(woocommerce_multistep_checkout_before_order_info)の前にテンプレートファイルを取得して読み込みます。

function cclever_show_insurance_step_if_rentals() { 
    if (function_exists('wc_checkout_add_ons')) { 
     // set to false first 
     $cat_check = false; 

     // check each cart item for our category 
     foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { 

      $product = $cart_item['data']; 

      // replace 'rentals' with your category's slug 
      if (has_term('rentals', 'product_cat', $product->id)) { 
       $cat_check = true; 
       // we only need one "true" to leave 
       break; 
      } 
     } 

     // if a product in the cart is in our category, remove the add-ons 
     if ($cat_check) { 

      add_action('woocommerce_multistep_checkout_before_order_info', 'cclever_add_insurance_custom_step'); 
      function cclever_add_insurance_custom_step($checkout) { 
       wc_get_template('checkout/insurance.php', array('checkout' => $checkout)); 
      } 
     } 
    } 
} 
add_action('woocommerce_before_checkout_form', 'cclever_show_insurance_step_if_rentals'); 

誰かを助ける希望。 **または私のコードに何か問題がある場合は、私に知らせてください。ありがとう!

関連する問題