简体   繁体   English

购物商品折扣基于Woocommerce 3中的数量

[英]Cart item discount based on quantity in Woocommerce 3

My WP site sells customized t-shirts. 我的WP网站销售定制的T恤。 The customization plugin makes each customized shirt a line item in the woocommerce cart. 自定义插件使每个定制衬衫成为woocommerce购物车中的一个项目。 If there are 2 shirts ordered of one design (quantity of 2 on that line item) I want to discount. 如果订购了一件设计的2件衬衫(该订单项的数量为2件),我想打折。 But if there is just 1 item per line I dont want to discount. 但如果每行只有1个项目,我不想打折。

I found this solution 我找到了这个解决方案

Adding a discount by cart items conditionally based on the item quantity 根据商品数量有条件地按购物车商品添加折扣

But this seems to change the product price in the db, so after the discount kicks in for say 2 blue shirts because there were 2 ordered on 1 line, if I add a 3rd shirt on a separate line it also gets the discount, which I dont want. 但是这似乎改变了数据库中的产品价格,所以在折扣开始之后说2个蓝色衬衫,因为在1行上订购了2个,如果我在另一条线上添加第3件衬衫它也会得到折扣,我不想要。

Since woocommerce version 3+ the linked answer code doesn't work . 由于woocommerce版本3+ 链接的答案代码不起作用 It needs something different and can even be done in a better way . 它需要不同的东西,甚至可以以更好的方式完成

The code will apply a cart item discount based on the cart item quantity. 该代码将根据购物车商品数量应用购物车商品折扣。 In this code example, it will apply a discount of 5% on the cart item, when the quatity is equal or more than 2 (two). 在此代码示例中,当数量等于或大于2(两)时,它将对购物车项目应用5%的折扣。

The cart item unit price displayed is always the real product price. 显示的购物车商品单价始终是实际商品价格。 The discount will be effective and displayed on the cart item subtotal. 折扣将生效并显示在购物车项目小计上。

Additionally the product name will be appended with a discount mention. 此外,产品名称将附加折扣提及。

The code: 编码:

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 ){
    $product_id = $variation_id > 0 ? $variation_id : $product_id;

    ## ----- YOUR SETTING ----- ##
    $discount_percentage = 5; // Discount (5%)

    // The WC_Product Object
    $product = wc_get_product($product_id);

    // Only for non on sale products
    if( ! $product->is_on_sale() ){
        $price = (float) $product->get_price();

        // Set the Product default base price as custom cart item data
        $cart_item_data['base_price'] = $price;

        // Set the Product discounted price as custom cart item data
        $cart_item_data['new_price'] = $price * (100 - $discount_percentage) / 100;

        // Set the percentage as custom cart item data
        $cart_item_data['percentage'] = $discount_percentage;
    }

    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['base_price']) ) {
        $product        = $cart_item['data'];
        $product_price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $cart_item['base_price'] ) ) );
    }
    return $product_price;
}

// Display the product name with the discount percentage
add_filter( 'woocommerce_cart_item_name', 'append_percetage_to_item_name', 20, 3 );
function append_percetage_to_item_name( $product_name, $cart_item, $cart_item_key ){
    if( isset($cart_item['percentage']) && isset($cart_item['base_price']) ) {
        if( $cart_item['data']->get_price() != $cart_item['base_price'] )
            $product_name .= ' <em>(' . $cart_item['percentage'] . '% discounted)</em>';
    }
    return $product_name;
}

add_action( 'woocommerce_before_calculate_totals', 'custom_discounted_cart_item_price', 20, 1 );
function custom_discounted_cart_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    ## ----- YOUR SETTING ----- ##
    $targeted_qty = 2; // Targeted quantity

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {

        // For item quantity of 2 or more
        if( $cart_item['quantity'] >= $targeted_qty && isset($cart_item['new_price']) ){

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

Code goes in functions.php file of your active child theme (or active theme). 代码位于活动子主题(或活动主题)的functions.php文件中。 Tested and works. 经过测试和工作。

在此输入图像描述


To display the discounted product price instead of the original product price, just remove woocommerce_cart_item_price() function (and hook) 要显示折扣产品价格而不是原始产品价格,只需删除woocommerce_cart_item_price()函数(和挂钩) ...

Newest similar: Cart item quantity progressive percentage discount in Woocommerce 3 最新类似: Woocommerce 3中的购物车商品数量累进百分比折扣

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM