在WooCommerce中对购物车总计(不含税)应用折扣 [英] Apply a discount on the cart content total excluding taxes in WooCommerce

查看:160
本文介绍了在WooCommerce中对购物车总计(不含税)应用折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户是第一次订购,我需要对购物车小计应用折扣,然后再计算税款.但是,税是在WooCommerce中按项目计算的,之后再加到小计中. 因此,我需要在购物车上的商品贴现折扣之前,先要对WooCommerce计算税款.这样,税收是基于折价而不是原始价格的.

I need to apply a discount to the cart subtotal before tax is calculated if a user is ordering for the first time. However, tax is calculated per-item in WooCommerce and added to the subtotal afterwards. So I need to apply the discount to the items in the cart before WooCommerce calculates the tax on them. This way the tax is based off of the discounted prices rather than the original prices.

这就是我所拥有的:

function first_order_add_five_percent_discount($cart_object) {

    if ( is_user_logged_in() ) {
        //current user id
        $currentUser_id = get_current_user_id();
        //amount of orders by current user
        $orderAmount = wc_get_customer_order_count( $currentUser_id ); 

        //if user has 0 orders...
        if ($orderAmount == 0) {
            //for each item in cart
            foreach ( $cart_object->get_cart() as $item_values ) {

                //$item_id = $item_values['data']->id; // Product ID
                $item_qty = $item_values['quantity']; // Item quantity
                $original_price = $item_values['data']->price; // Product original price
                echo $original_price . "<br>";
                $totalPrice = $original_price * $item_qty;
                $discountedPrice = $totalPrice * .05;
                $newPrice = $original_price - $discountedPrice;
                echo $totalPrice . "<br>";
                echo $discountedPrice . "<br>";
                echo $newPrice . "<br>";
                $item_values['data']->set_price($newPrice);

            }

        } else {
            //do nothing
        }
    }
}

add_action( 'woocommerce_before_calculate_totals', 'first_order_add_five_percent_discount' );

这回显了我需要的正确数字,但是现在我需要将这些价格应用于购物车.目前,购物车中的价格没有变化.

This echos out the right numbers I need, but now I need to apply those prices to the cart. Right now the prices in the cart do not change.

如何从此函数的计算中将新价格应用于购物车?

How can I apply the new prices from this function's calculations to the cart?

推荐答案

更新:使用负费用并不是最好的方法,Woocommerce不建议这样做.

UPDATE: Using a negative fee is not the best way and not recommended by Woocommerce.

改为尝试:
- Woocommerce 3中的购物车数量渐进百分比折扣
-基于Woocommerce 3中数量的购物车折扣
-根据具体情况自动应用优惠券Woocommerce中的购物车商品计数

Try instead:
- Cart item quantity progressive percentage discount in Woocommerce 3
- Cart item discount based on quantity in Woocommerce 3
- Apply automatically a coupon based on specific cart items count in Woocommerce

还有一种更简单的方法,就是使用负数费用,是折扣.它使用WC_Cart方法add_fee() ,您可以在其中禁用税收:

There is a much more simpler way, is to use a negative fee, so a discount. It use the WC_Cart method add_fee() where you can disable tax:

add_action( 'woocommerce_cart_calculate_fees','new_customers_discount', 10, 1 );
function new_customers_discount( $wc_cart ) {
    if ( is_admin() && ! defined('DOING_AJAX') ) return; // We exit

    // Only for logged in users
    if ( ! is_user_logged_in() ) return; // We exit

    // Only for new customers without orders
    if ( wc_get_customer_order_count( get_current_user_id() ) != 0 ) return;  // We exit

    // discount percentage
    $percent = 5;

    // Calculation
    $discount = $wc_cart->cart_contents_total * $percent / 100;

    $wc_cart->add_fee( __( 'Discount', 'woocommerce')." ($percent%)", -$discount );
}

代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

经过测试,可以正常工作.你会得到类似的东西:

Tested and working. you will get something like that:

如您所见,折扣是购物车内容总和中的佣金.税收

As you can see the discount is maid on the cart content total excl. Tax

这篇关于在WooCommerce中对购物车总计(不含税)应用折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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