問題描述
如果用戶是第一次訂購,我需要在計算稅前對購物車小計應用折扣.但是,稅是在 WooCommerce 中按項目計算的,然后添加到小計中.因此,我需要在 WooCommerce 計算對購物車中的商品征稅之前將折扣應用于購物車中的商品.這樣,稅款是基于折扣價而不是原始價格.
這是我所擁有的:
function first_order_add_five_percent_discount($cart_object) {如果 ( is_user_logged_in() ) {//當前用戶ID$currentUser_id = get_current_user_id();//當前用戶的訂單數量$orderAmount = wc_get_customer_order_count( $currentUser_id );//如果用戶有0個訂單...如果 ($orderAmount == 0) {//對于購物車中的每個項目foreach ( $cart_object->get_cart() 作為 $item_values ) {//$item_id = $item_values['data']->id;//產品編號$item_qty = $item_values['數量'];//物品數量$original_price = $item_values['data']->price;//產品原價回聲 $original_price ."<br>";$totalPrice = $original_price * $item_qty;$discountedPrice = $totalPrice * .05;$newPrice = $original_price - $discountedPrice;回聲 $totalPrice ."<br>";回聲 $discountedPrice ."<br>";回聲 $newPrice ."<br>";$item_values['data']->set_price($newPrice);}} 別的 {//沒做什么}}}add_action('woocommerce_before_calculate_totals', 'first_order_add_five_percent_discount');
這反映了我需要的正確數字,但現在我需要將這些價格應用到購物車.現在購物車中的價格沒有變化.
如何將來自此函數計算的新價格應用到購物車?
更新:使用負費用不是最好的方法,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?
解決方案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
methodadd_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 made on the cart content total excl. Tax
這篇關于在 WooCommerce 中對購物車內容總額應用折扣(不含稅)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!- Cart item quantity progressive percentage discount in Woocommerce 3