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

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

问题描述

如果用户是第一次订购,我需要在计算税前对购物车小计应用折扣.但是,税是在 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:

    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 中对购物车内容总额应用折扣(不含税)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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