久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 WooCommerce 中對購物車內容總額應用折扣(不含稅

Apply a discount on the cart content total excluding taxes in WooCommerce(在 WooCommerce 中對購物車內容總額應用折扣(不含稅))
本文介紹了在 WooCommerce 中對購物車內容總額應用折扣(不含稅)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

如果用戶是第一次訂購,我需要在計算稅前對購物車小計應用折扣.但是,稅是在 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 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 made on the cart content total excl. Tax

    這篇關于在 WooCommerce 中對購物車內容總額應用折扣(不含稅)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

    【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 97av在线视频| 欧美综合一区 | 精品久久久久久 | 国产成人91 | 免费理论片 | 欧美三级 欧美一级 | 成年人在线播放 | 成人欧美激情 | 成人午夜精品 | 亚洲视频免费看 | 在线色 | 国产一级免费视频 | 三年中文在线观看免费大全中国 | 午夜精品视频在线 | 久久精品视频网 | 国产亚洲欧美日韩高清 | 亚洲久久久久久 | 成人黄性视频 | 黄色三级大片 | 欧美激情一区二区 | 精品国产一区二区三区久久久蜜月 | 黄视频网站在线观看 | 一级真人毛片 | 成人在线精品 | 国产成人91| 成人激情综合网 | 玖玖在线观看 | 久久久精品影院 | aaaaaa毛片 | 国产黄色在线观看 | 欧美做受喷浆在线观看 | 特级淫片裸体免费看 | 成人女同在线观看 | 欧美一级做性受免费大片免费 | 国产午夜精品一区二区三区 | 久久精品福利视频 | 国产黄色在线观看 | 久久手机免费视频 | 亚洲人网站 | 高潮毛片无遮挡免费看 | 911看片 |