Apply a discount on the cart content total excluding taxes in WooCommerce

六眼飞鱼酱① 提交于 2020-01-10 03:18:20

问题


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.

Here is what I have:

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?


回答1:


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

Note that the taxes will be always applied.

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

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 );
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and working. you will get something like that:

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



来源:https://stackoverflow.com/questions/47802738/apply-a-discount-on-the-cart-content-total-excluding-taxes-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!