对非“待售"商品应用欢迎折扣WooCommerce中的项目 [英] Apply a welcome discount to non "on sale" items in WooCommerce

查看:53
本文介绍了对非“待售"商品应用欢迎折扣WooCommerce中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码来设置欢迎"广告,注册用户的折扣:

I use this code to set up a "welcome" discount for a registered user:

 add_action( 'woocommerce_cart_calculate_fees', 'personal_discount_based', 20, 1 );
 function personal_discount_based( $cart ) {
  if ( is_admin() && ! defined( 'DOING_AJAX' ) )
     return; 
     $user = get_user_data();
     $usId = $user->ID;
     update_user_meta( $usId, 'discount', 5 );
     $personalDisconte = $user->discount;
     $orderNumbers = wc_get_customer_order_count($usid);

     if ( ! $personalDisconte == true )
    return; 
    $percentage = $personalDisconte;
    $discount = $cart->get_subtotal() * $percentage / 100;
    if(!empty($orderNumbers)  && $orderNumbers == 1){
      update_user_meta( $usId, 'discount', 0 );
    }elseif( $orderNumbers > 1){
      $cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
    }else{
       $cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
     }

   }

并且此代码执行其功能.但是现在我面临另一个问题.当然,折扣适用于整个订单金额.在这种情况下,订单可能已经包含具有折扣的产品.而且此选项不适合卖方.

And this code does its function. But now I am facing another problem. The discount applies to the entire order amount, of course. In this case, the order may already include products with a discount. And this option is not suitable for the seller.

此代码生成的折扣通常可能不应用于订单总额.但仅限于那些没有折扣的产品.

It is generally possible that the discount generated by this code is not applied to the total amount of the order. But only to those products that are without discount.

例如,一个用户向购物车中添加了三种产品:

For example, a user added three products to the cart:

第一项50美元第二项75 $第三项90美元(原价120美元)

Item one 50$ Item two 75$ Item three 90$ (old price 120$)

因此,我必须对50 + 75的总和应用折扣(这些是正常价格的产品)在50 + 75-5%加90之后,结果将是最终的总和.

So, I have to apply a discount to the sum of 50 + 75 (these are regular price product) After 50 + 75 - 5% plus 90 the result will be the final sum.

目前,我不知道该怎么做,也不确定是否可以这样做.如果有人可以提供帮助,请提供建议.

At the moment, I do not know how to do this and am not sure if it is possible to do it at all. If someone can help, give advice please.

推荐答案

如果您为尚未购买的新客户提供欢迎折扣,那么您的代码将变得毫无复杂性,因此,我简化了代码并简化了代码.

Your code is complicated for nothing if you are applying a welcome discount for new customers that doesn't have purchased yet, so I have simplified and lighten your code.

然后将购物车小计为非待售"商品,您需要遍历购物车商品才能获得的商品.

Then to get the cart subtotal for non "On sale" items you need to loop through cart items to get it.

重新访问的代码:

add_action( 'woocommerce_cart_calculate_fees', 'welcome_discount_on_normal_items', 20, 1 );
function welcome_discount_on_normal_items( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $user_id = get_current_user_id();

    if( $user_id > 0 ) {
        $percentage   = 5; // Discount percentage
        $orders_count = (int) wc_get_customer_order_count($user_id);
        $subtotal     = 0; // Initializing

        // Loop through cart items
        foreach( $cart->get_cart() as $cart_item ) {
            // Add non on sale items to subtotal
            if( ! $cart_item['data']->is_on_sale() ) {
                $subtotal += $cart_item['line_subtotal'];
            }
        }

        // Discount percentage amount on non "on sale" items subtotal
        $discount = $subtotal * $percentage / 100;

        // For new customer only that haven't purchase yet
        if( $subtotal > 0 && $orders_count === 0 ) {
            $cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
        }
    }
}

代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

这篇关于对非“待售"商品应用欢迎折扣WooCommerce中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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