2017-01-21 13 views
1

私は、woocommerceスマートクーポンを使用して "招待コード"を作成しようとしています。そのためには、カート/チェックアウト時に特定の商品にクーポンコードが必要になります。私はこれを行う方法を見てきましたが、私が望むものだけでなく、すべての製品に影響します。特定の商品カテゴリを持つ必須のクーポンコードの通知を表示

返された変数を別の関数に渡そうとしていますが、その方法を理解できません。

  //Check to see if user has product with a specific product category in cart  
    function check_product_in_cart() { 

     global $woocommerce; 

     //assigns a default negative value 
     // categories targeted 87, 18, 19 

     $product_in_cart = false; 

     // start of the loop that fetches the cart items 

     foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { 
      $_product = $values['data']; 
      $terms = get_the_terms($_product->id, 'product_cat'); 

      // second level loop search, in case some items have several categories 
      foreach ($terms as $term) { 
       $_categoryid = $term->term_id; 
       if (($_categoryid === 87) || ($_categoryid === 18) || ($_categoryid === 19)) { 
        //category is in cart! 
        $product_in_cart = true; 
       } 
      } 
     } 

     return $product_in_cart; 
    } 

// Force Coupon codes for Woocommerce 
add_action('woocommerce_check_cart_items', 'force_coupon_code'); 

    function force_coupon_code($product_in_cart) 
    { 
     global $woocommerce; 
     if(is_cart() || is_checkout()){ 
      $my_coupon = $woocommerce->cart->applied_coupons; 
      echo $woocommerce->cart->get_applied_coupons; 
      if(empty($my_coupon)) 
      { 
       wc_add_notice('<strong>' . $btn['label'] . '</strong> ' . __('A coupon code is mandatory for this product.', 'woocommerce'), 'error'); 
      } 
     } 
    } 

最初の部分は、製品の$ product_in_cartと第二部は、クーポンを強制的に返す必要があります。ここでは

は最新バージョンです。ここで私が使用しているコードのソースがあります。 Mandatory Coupon codeおよびChecking products in cart based on category。新しい関数argの中に変数を置くことは、like soを達成するかもしれませんが、それは決して機能しません。

答えて

1

更新(WooCommerce 3+互換)

使用しているコードは時代遅れと複雑です。これは、Wordpress has_term()の条件関数を使って、商品カテゴリがカートアイテムにあるかどうかを検出するために、1つのフックされた関数で単純に実行できます。

だからあなたのコードは、はるかにコンパクトで効率的になります。

// Force Coupon codes for Woocommerce 
add_action('woocommerce_check_cart_items', 'force_coupon_code'); 
function force_coupon_code() 
{ 
    if(is_cart() || is_checkout()){ 

     // set Here your categories IDs, slugs or names 
     $categories = array(18,19,87); 
     $has_cat = false; 

     // Iterating through cart items 
     foreach (WC()->cart->get_cart() as $cart_item) 
      // Get the product ID (WC 3+ compatibility) 
      $product_id = version_compare(WC_VERSION, '3.0', '<') ? $cart_item['data']->id : $cart_item['data']->get_id(); 
      if(has_term($categories, 'product_cat', $product_id)){ 
       // Product category found in cart items 
       $has_cat = true; 
       // Exit from loop 
       break; 
      } 

     $my_coupon = WC()->cart->get_applied_coupons(); 

     // The Notice is displayed for that product categories when no mandatory coupon has been entered 
     if(empty($my_coupon) && $has_cat) 
      wc_add_notice(__('A coupon code is mandatory for this product.', 'woocommerce'), 'error'); 
    } 
} 

このコードは、任意のプラグインファイルでもfunction.phpのアクティブな子テーマ(またはテーマ)のファイルかになります。

このコードはテスト済みであり、動作します。

+0

美しい!魅力を発揮します。私はまだ学んでいるので、もっと良いコードを除いて、 'if(空($ my_coupon)&& $ has_cat)'という行はすべて一緒に結びついているものです。理論的には、文にさらに引数を追加し、 'if(empty($ my_coupon)&& $ has_cat)&& $ color_blue && etc ... 'のようにさらに狭くすることはできますか? – jimmyjames

関連する問題