2017-02-07 8 views
-1

の場合、製品を削除します。誰かが助けてくれることを願っています。私は、カートの価値が50ドル以上であれば、カートに商品を追加するコード(thanks to this post)を設定しました。カードの値が<x

これは機能します。ただし、値が50ドル未満の場合、商品がカートに入っていないことを確認します。つまり、ユーザーが製品を追加した場合、追加の製品が追加されますが、製品が削除され、その値は50ドル未満です(追加された新しい製品は含まれません)... ...追加された製品を削除します。

私は上記の記事のようなコードを実装してみました:

add_action('template_redirect', 'add_product_to_cart'); 

function add_product_to_cart() { 
// Bonus products 
$product_1 = '4751'; 
$product_2 = '4752'; 
$product_3 = '24711'; 

// Get cart value in a clean format 
$cart_total = WC()->cart->get_cart_subtotal(); 
$cart_total = html_entity_decode($cart_total, ENT_QUOTES, 'UTF-8'); 
$cart_total_format = strip_tags($cart_total); 
$cart_value = preg_filter("/[^0-9]/", "", $cart_total_format); 
$sum_raw = $cart_value; 

// Set the sum level 
$level3 = '50'; 

// Check sum and apply product 
if ($sum_raw >= $level3) { 

    // Cycle through each product in the cart and check for match 
    $found = 'false'; 
     foreach (WC()->cart->cart_contents as $item) { 
      global $product; 
      $product_id = $item['variation_id']; 

      if ($product_id == $product_3) { 
       $found = 'true'; 
      } 
     } 

    // If product found we do nothing 
     if ($found == 'true') {} 
    // else we will add it 
     else { 
     //We add the product 
      WC()->cart->add_to_cart($product_3); 
     } 
} 


// Check if sum 
if ($sum_raw < $level3) { 
    foreach ($woocommerce->cart->get_cart() as $cart_item_key =>  $cart_item) { 

     if ($cart_item['variation_id'] == $product_3) { 
       //remove single product 
       $woocommerce->cart->remove_cart_item($cart_item_key); 
      } 
     } 
    } 

} 

しかし、それは、製品を削除していません。 :/誰かがそれがうまくいかない理由や、カートをチェックするためのより良い解決策があれば誰でも知っている:カートが$ 50の商品を追加したら... < $ 50になったら同じ商品を削除する...

また、新しく追加された商品を小切手から除外します(したがって、プログラムで追加されたばかりの商品は含まれていません。<)。

助けてください!

$cart_value = preg_filter("/[^0-9]/", "", $cart_total_format); 

(追加 - マイナス)::(

答えて

0

はたぶん、あなたはこの行を変更する必要が...あなたは現在に更新しようとしている

$cart_value = preg_filter("/[^0-9\-]/", "", $cart_total_format); 
+0

おかげでアクションを削除するが、これはそれを修正していないようでした:( – user2115227

0

このコードを試してみてください「を追加。 『助けを

add_action('template_redirect', 'remove_product_from_cart'); 
function remove_product_from_cart() { 
    // Run only in the Cart or Checkout Page 
    if(is_cart() || is_checkout()) { 


     /* 
     GET THE CART TOTAL 
     */ 

     // Set the product ID to remove 
     $prod_to_remove = PRODUCT-ID-TO-BE-REMOVED; 

     // Cycle through each product in the cart 
     foreach(WC()->cart->cart_contents as $prod_in_cart) { 
      // Get the Variation or Product ID 
      $prod_id = (isset($prod_in_cart['variation_id']) && $prod_in_cart['variation_id'] != 0) ? $prod_in_cart['variation_id'] : $prod_in_cart['product_id']; 
      // Check to see if IDs match 
      if($prod_to_remove == $prod_id) { 
       // Get it's unique ID within the Cart 
       $prod_unique_id = WC()->cart->generate_cart_id($prod_id); 
       // Remove it from the cart by un-setting it 
       unset(WC()->cart->cart_contents[$prod_unique_id]); 
      } 
     } 
    } 
} 
+0

おかげで、私はこれを試してみました"それは上の必要がある一方で、アクション』それは何もしていないようです。 http://tulaforlife.staging.wpengine.com/my-cart/ --- $ 50以上の商品を追加すると、$ 25のギフトカードが追加されますが、その商品を削除してギフトカードは$ 50以下です。 – user2115227

関連する問題