Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
12.9k views
in Technique[技术] by (71.8m points)

php - Apply a discount on the cart content total excluding taxes in 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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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:

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...