Woocommerce中特定产品的购物车批量折扣 [英] Cart bulk quantity discount for specific products in Woocommerce

查看:174
本文介绍了Woocommerce中特定产品的购物车批量折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在woocommerce中,我尝试仅针对特定产品ID进行自定义购物车商品数量折扣.这是我正在尝试做的事情:
如果客户将特定产品添加到购物车中,我想保留第一件商品的原始价格,并为每增加一个商品设置折扣的自定义价格.

In woocommerce I am trying to make a custom cart item quantity discount for a specific product ID only. This is what I am trying to do:
If a customer add to cart a specific product, I would like to keep the original price for the first item and set a discounted custom price for each additional item quantity.

例如:

  • 产品有效价格为40(我们为第一个数量项目保留此价格)
  • 对于不超过2个(附加项目数量)的数量,价格为12.

因此,如果客户添加了数量为5的产品,那么价格将为:
40 + 12 + 12 + 12 + 12 = 88

So if customer add the product with a quantity 5 items, then the price will be:
40 + 12 + 12 + 12 + 12 = 88

所以我尝试稍微更改此答案代码:
Woocommerce 3中的购物车数量渐进百分比折扣

So I tried changing this answer code a bit:
Cart item quantity progressive percentage discount in Woocommerce 3

但是无法使其正常工作.

But couldn't make it work correctly.

感谢您的帮助.

推荐答案

对于您的情况,请使用以下内容:

For your case use the following instead:

add_filter('woocommerce_add_cart_item_data', 'add_items_default_price_as_custom_data', 20, 3 );
function add_items_default_price_as_custom_data( $cart_item_data, $product_id, $variation_id ){

    ## Your settings her below ##
    $product_ids = array(37); // <=== Your specific product(s) ID(s) in the array
    $discounted_price = 12; // <=== The specific product discounted price

    // The WC_Product Object
    $product = wc_get_product($variation_id > 0 ? $variation_id : $product_id);

    if( array_intersect( $product_ids, array($product_id, $variation_id) ) && ! $product->is_on_sale() ){
        // We set the Product discounted price
        $cart_item_data['pricing']['discounted'] = $discounted_price;

        // Set the Product default base price as custom cart item data
        $cart_item_data['pricing']['active'] = $product->get_price();
    }

    return $cart_item_data;
}

// Display the product original price
add_filter('woocommerce_cart_item_price', 'display_cart_items_default_price', 20, 3 );
function display_cart_items_default_price( $product_price, $cart_item, $cart_item_key ){
    if( isset($cart_item['pricing']['active']) && $cart_item['quantity'] > 1 ) {
        $product_price  = wc_price( wc_get_price_to_display( $cart_item['data'], array( 'price' => $cart_item['pricing']['active'] ) ) );
    }
    return $product_price;
}

// Display the product name with the a custom "discount" label
add_filter( 'woocommerce_cart_item_name', 'append_custom_label_to_item_name', 20, 3 );
function append_custom_label_to_item_name( $product_name, $cart_item, $cart_item_key ){
    if( isset($cart_item['pricing']['discounted']) && $cart_item['data']->get_price() != $cart_item['pricing']['discounted'] ) {
        $discounted_price = (float) $cart_item['pricing']['discounted'];
        $default_price    = (float) $cart_item['pricing']['active'];
        $quantity         = (int)   $cart_item['quantity'];

        // Calculate new product price
        $new_price = ($default_price + ($discounted_price * ($quantity - 1))) / $quantity;

        // Get the discount percentage (if needed)
        $percent = 100 - ($new_price / $default_price * 100);

        $product_name .= ' <em>(' . __('quantity discounted', 'woocommerce') . ')</em>';
    }
    return $product_name;
}

// Set the new discounted price
add_action( 'woocommerce_before_calculate_totals', 'set_custom_discount_cart_item_price', 25, 1 );
function set_custom_discount_cart_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    foreach( $cart->get_cart() as $cart_item ){

        // For items non on sale set a discount based on quantity
        if( isset($cart_item['pricing']['discounted']) && $cart_item['quantity'] > 1 ) {
            $discounted_price = (float) $cart_item['pricing']['discounted'];
            $default_price    = (float) $cart_item['pricing']['active'];
            $quantity         = (int)   $cart_item['quantity'];

            // Calculate new product price
            $new_price = ($default_price + ($discounted_price * ($quantity - 1))) / $quantity;

            // Set cart item calculated price
            $cart_item['data']->set_price($new_price);
        }
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于Woocommerce中特定产品的购物车批量折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆