根据 WooCommerce 中的购物车数量添加一定的免费礼物

Posted

技术标签:

【中文标题】根据 WooCommerce 中的购物车数量添加一定的免费礼物【英文标题】:Add a certain free gift depending on cart amounts in WooCommerce 【发布时间】:2021-08-25 07:47:23 【问题描述】:

我想。

假设:

少于 1500 - 没有免费礼物 介于或等于 1500 - 1999 - 添加免费产品 (1) 大于或等于 2000 - 添加另一个免费产品 (2),删除免费产品 (1)

基于Add free gifted product for a minimal cart amount in WooCommerce 答案代码,如果我添加 1 个元素,它会起作用,但如果我添加更多,它就会停止工作。

这是我的代码尝试:

// Add free gifted product for specific cart subtotal
add_action('woocommerce_before_calculate_totals', 'check_free_gifted_product');
function check_free_gifted_product($cart)

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

  // Settings
  $free_product_id   = 158;
  $targeted_subtotal = 1500;
  $targeted_subtotal_max = 2000;

  $cart_subtotal     = 0; // Initializing

  // Loop through cart items (first loop)
  foreach ($cart->get_cart() as $cart_item_key => $cart_item) 
    // When free product is is cart
    if ($free_product_id == $cart_item['product_id']) 
      $free_key = $cart_item_key;
      $free_qty = $cart_item['quantity'];
      $cart_item['data']->set_price(0); // Optionally set the price to zero
     else 
      $cart_subtotal += $cart_item['line_total'] + $cart_item['line_tax'];
    
  

  // If subtotal match and free product is not already in cart, add it
  if (!isset($free_key) && $cart_subtotal >= $targeted_subtotal && $cart_subtotal <= $targeted_subtotal_max) 
    $cart->add_to_cart($free_product_id);
  
  // If subtotal doesn't match and free product is already in cart, remove it
  elseif (isset($free_key) && $cart_subtotal < $targeted_subtotal || $cart_subtotal > $targeted_subtotal_max) 
    $cart->remove_cart_item($free_key);
  
  // Keep free product quantity to 1.
  elseif (isset($free_qty) && $free_qty > 1) 
    $cart->set_quantity($free_key, 1);
  

有什么建议吗?

【问题讨论】:

【参考方案1】:

要根据 WooCommerce 中的购物车数量添加免费礼物,您可以使用 woocommerce_before_calculate_totals 操作挂钩

如果购物车数量少于 1500。将不会添加免费产品 如果购物车数量介于或等于 1500 - 1999 之间,将添加免费产品 (1) 如果购物车数量大于或等于 2000,将添加免费产品 (2),并移除免费产品 (1)
function action_woocommerce_before_calculate_totals( $cart ) 
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Free product productIDs
    $free_product_id_1 = 819;
    $free_product_id_2 = 821;
    
    // Minimum subtotal needed for free products
    $min_subtotal_free_product_1 = 1500;
    $min_subtotal_free_product_2 = 2000;

    // Initializing
    $cart_subtotal = 0;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) 
        // When free product is is cart
        if ( $free_product_id_1 == $cart_item['product_id'] ) 
            $free_key_1 = $cart_item_key;
            $free_qty_1 = $cart_item['quantity'];
            // Optionally set the price to zero
            $cart_item['data']->set_price(0);
         elseif ( $free_product_id_2 == $cart_item['product_id'] ) 
            $free_key_2 = $cart_item_key;
            $free_qty_2 = $cart_item['quantity'];
            // Optionally set the price to zero
            $cart_item['data']->set_price(0);
         else 
            // NOT empty
            if ( ! empty ( $cart_item['line_total'] ) ) 
                $cart_subtotal += $cart_item['line_total'];
            

            // NOT empty
            if ( ! empty ( $cart_item['line_tax'] ) ) 
                $cart_subtotal += $cart_item['line_tax'];
            
        
    
    
    // If subtotal is less than first subtotal
    if ( $cart_subtotal < $min_subtotal_free_product_1 ) 
        // Free product 1 is already in cart, remove it
        if ( isset( $free_key_1 ) ) 
            $cart->remove_cart_item( $free_key_1 );
        
        
        // Free product 2 is already in cart, remove it
        if ( isset( $free_key_2 ) ) 
            $cart->remove_cart_item( $free_key_2 );
        
    
    // If subtotal is between first and second subtotal
    elseif ( $cart_subtotal >= $min_subtotal_free_product_1 && $cart_subtotal < $min_subtotal_free_product_2 ) 
        // Free product 1 is not already in cart, add it
        if ( ! isset( $free_key_1 ) ) 
            $cart->add_to_cart( $free_product_id_1 );
        
        
        // Free product 2 is in cart, remove it
        if ( isset( $free_key_2 ) ) 
            $cart->remove_cart_item( $free_key_2 );
        
    
    // If subtotal greater than or equal to second subtotal
    elseif ( $cart_subtotal > $min_subtotal_free_product_2 ) 
        // Free product 1 is already in cart, remove it
        if ( isset( $free_key_1 ) ) 
            $cart->remove_cart_item( $free_key_1 );
        
        
        // Free product 2 is not already in cart, add it
        if ( ! isset( $free_key_2 ) ) 
            $cart->add_to_cart( $free_product_id_2 );
        
       

    // Keep free product 1 quantity to 1.
    if ( isset( $free_qty_1 ) && $free_qty_1 > 1 ) 
        $cart->set_quantity( $free_key_1, 1 );
    
    
    // Keep free product 2 quantity to 1.
    if ( isset( $free_qty_2 ) && $free_qty_2 > 1 ) 
        $cart->set_quantity( $free_key_2, 1 );
    

add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );

【讨论】:

以上是关于根据 WooCommerce 中的购物车数量添加一定的免费礼物的主要内容,如果未能解决你的问题,请参考以下文章

根据运输类别和商品数量添加 Woocommerce 费用

Cloudfront + Woocommerce - 购物车未更新

Woocommerce 中的迷你购物车数量变化

在Android应用程序中向GridView添加一组按钮

php [WooCommerce Core]在登录/注册表单上方添加一条消息

php [WooCommerce Core]在登录/注册表单上方添加一条消息