2017-09-27 49 views
1

我想添加自定义通知到购物车,只要一个特定的产品(不是所有的产品)被添加到购物车。在Woocommerce中添加自定义通知到

我想首先检查数量,如果是1,那么我想显示通知以增加该产品的数量。我想通通过自己的东西从互联网上,我不认为我的解决方案是正确的:

add_action('wp', 'sp_custom_notice'); 

function sp_custom_notice() { 
if (is_admin() && ! defined('DOING_AJAX')) return; 

//to display notice only on cart page 
if (! is_cart()) { 
    return; 
} 

global $product; 

$product_id = method_exists($product, 'get_id') ? $product->get_id() : $product->id; 
if($product_id == 15730){ 
    //check for quantify if equal to 1 in cart 


    wc_clear_notices(); 
    wc_add_notice(__("Add one more to get 50% off on 2nd product"), 'notice'); 
} 
} 

这将是巨大的,如果有人能帮助我在此。

回答

1

如果你还在使用从旧的答案代码,你可以改变它一点得到这个工作太:

add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_2nd_at_50', 10, 1); 
function add_custom_discount_2nd_at_50($wc_cart){ 
    if (is_admin() && ! defined('DOING_AJAX')) return; 
    $discount = 0; 
    $items_prices = array(); 
    $qty_notice = 0; // <== Added HERE 

    // Set HERE your targeted variable product ID 
    $targeted_product_id = 40; 

    foreach ($wc_cart->get_cart() as $key => $cart_item) { 
     if($cart_item['product_id'] == $targeted_product_id){ 
      $qty = intval($cart_item['quantity']); 
      $qty_notice += intval($cart_item['quantity']); // <== Added HERE 
      for($i = 0; $i < $qty; $i++) 
       $items_prices[] = floatval($cart_item['data']->get_price()); 
     } 
    } 
    $count_items_prices = count($items_prices); 
    if($count_items_prices > 1) foreach($items_prices as $key => $price) 
     if($key % 2 == 1) $discount -= number_format($price/2, 2); 

    if($discount != 0){ 
     // Displaying a custom notice (optional) 
     wc_clear_notices(); 
     wc_add_notice(__("You get 50% of discount on the 2nd item"), 'notice'); 

     // The discount 
     $wc_cart->add_fee('Discount 2nd at 50%', $discount, true ); 
     # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false) 
    } 
    // <== Here we display your additional notice when there quantity is equal to one 
    elseif($qty_notice == 1){ 
     wc_clear_notices(); 
     wc_add_notice(__("Add one more to get 50% off on 2nd product"), 'notice'); 
    } 
} 

代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。

此代码在Woocommerce 3+上测试并正常工作。

注:wc_add_notice()功能绝对不需要进行呼应

+1

再次感谢您为我节省了成本,您真棒... :) @loictheaztec – abdul

0

你做得相当不错的代码需要进行一些小的改动尝试下面的代码:

add_action('woocommerce_before_cart', 'sp_custom_notice'); 
function sp_custom_notice() { 

    if (is_admin() && ! defined('DOING_AJAX')) return; 

    //to display notice only on cart page 
    if (! is_cart()) { 
    return; 
    } 

    global $woocommerce; 
    $items = $woocommerce->cart->get_cart(); 
     foreach($items as $item => $values) { 
      $_product = wc_get_product($values['data']->get_id()); 

      if($values['data']->get_id() == 190 && $values['quantity'] == 1){ 
       wc_clear_notices(); 
       echo wc_add_notice(__("Add one more to get 50% off on 2nd product"), 'notice'); 
      } 
     } 
} 
+0

感谢,但我想先检查数量。 @akshayshah – abdul

+0

现在请检查 –

+0

我不知道为什么,但它不工作。 @aksayshah – abdul

相关问题