根据客户在Woocommerce中的总购买金额添加百分比折扣 [英] Add a percentage discount based on customer total purchases sum in Woocommerce

查看:74
本文介绍了根据客户在Woocommerce中的总购买金额添加百分比折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce中,我想基于客户的总购买金额来设置百分比折扣.例如,如果总购买金额大于或等于200$ ,则客户可获得 5%折扣.

In Woocommerce, I would like to set a percentage discount based on customer total purchases sum. For example if the total purchase sum greater or equal to 200$, customer get 5% discount.

因此,我有第一部分代码来显示总和:

So, I have first part of code for showing the total sum:

function get_customer_total_order() {
    $customer_orders = get_posts( array(
        'numberposts' => - 1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => array( 'shop_order' ),
        'post_status' => array( 'wc-completed' )
    ) );

    $total = 0;

    foreach ( $customer_orders as $customer_order ) {
        $order = wc_get_order( $customer_order );
        $total += $order->get_total();
    }

    return $total;
}

我想将我的代码与以下答案的代码一起使用:
基于WooCommerce中购物车总数的渐进式折扣

And I would like to use my code with the code of this answer:
Progressive discount based on cart total in WooCommerce

有没有办法同时使用它们来基于所有订单的总和设置折扣?

It there a way to use them both to set the discount based on total sum of all orders?

推荐答案

有多种获取客户总购买金额的方法:

There is multiple ways to get customer's total purchases sum:

1)您可以使用 wc_get_customer_total_spent() 专用的Woocommerce功能,但是该功能采用所有订单已付款状态(正在处理"和已完成").

2)您还可以基于:

// Utililty function to get customer's total purchases sum
function get_customer_total_purchases_sum() {
    $current_user_id = get_current_user_id(); // Current user ID

    if( $current_user_id == 0 ) return 0; // we return zero if customer is not logged in

    global $wpdb;

    // return the SQL query (paid orders sum)
    return $wpdb->get_var("SELECT SUM(pm.meta_value) FROM {$wpdb->prefix}postmeta as pm
    INNER JOIN {$wpdb->prefix}postmeta as pm2 ON pm.post_id = pm2.post_id
    INNER JOIN {$wpdb->prefix}posts as p ON pm.post_id = p.ID
    WHERE p.post_status LIKE 'wc-completed' AND p.post_type LIKE 'shop_order'
    AND pm.meta_key LIKE '_order_total' AND pm2.meta_key LIKE '_customer_user'
    AND pm2.meta_value LIKE '$current_user_id'");
}

此代码位于您的活动子主题(或主题)的function.php文件中.经过测试,可以正常工作.

This code goes on function.php file of your active child theme (or theme). Tested and works.

3)或者您也可以使用自己的问题功能代码(但是比较重).由你决定.

3) Or you can use your own question function code (but it's heavier). It's up to you.

1)负费用:

以下代码将基于客户的购买总金额(使用上述功能) 设置百分比折扣:

The following code will set a percentage discount based on customer's total purchases sum (using my function above):

// Percentage discount based on customer's total purchases sum
add_action('woocommerce_cart_calculate_fees', 'customer_purchases_total_sum_percentage_discount', 20, 1 );
function customer_purchases_total_sum_percentage_discount( $cart ){
    // Only for logged in user
    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || ! is_user_logged_in() )
        return;

    ## 1. Get the customer's purchases total sum (and save it in WC sessions to avoid multiple queries

    // Check if it's saved in WC_Session
    $purchases_sum = WC()->session->get( 'purchases_sum' ); 
    // If not get it and save it
    if( empty($purchases_sum) ){ 
        // ==> HERE goes the function to get customer's purchases total sum
        $purchases_sum = get_customer_total_purchases_sum();
        // Save it in WC_Session
        WC()->session->set('purchases_sum', $purchases_sum); 
    }

    ## 2. Set the discount percentage based on customer's total purchases sum
    if( $purchases_sum >= 200 ){
        $percent =  5; // 5%
    }

    if( isset($percent) && $percent > 0){
        $discount = $cart->cart_contents_total * $percent / 100; // discount calculation
        // Set the discount (For discounts (negative fee) the taxes as always included)
        $cart->add_fee( __('Discount', 'woocommerce' ) . " (" . $percent . "%)", -$discount);
    }
}

此代码位于您的活动子主题(或主题)的function.php文件中.经过测试,可以正常工作.

This code goes on function.php file of your active child theme (or theme). Tested and works.

2)自动添加优惠券代码(折扣百分比):

2) Auto adding a coupon code (percentage discount):

首先,您需要设置一个新的优惠券代码(5%的折扣类型):

First you need to set a new coupon code (percentage discount type of 5%):

然后,您将使用以下代码代替该代码,该代码将根据客户的购买总金额(使用上述功能)自动添加优惠券代码:

Then you will use the following code instead that will auto add a coupon code based on customer's total purchases sum (using my function above):

add_action( 'woocommerce_before_calculate_totals', 'customer_total_purchases_coupon_discount', 30, 1 );
function customer_total_purchases_coupon_discount( $cart ) {
    // Only for logged in user
    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || ! is_user_logged_in() )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // HERE define your coupon code (in lowercase)
    $coupon_code = 'customersilver';

    ## 1. Get the customer's purchases total sum (and save it in WC sessions to avoid multiple queries

    // Check if it's saved in WC_Session
    $purchases_sum = WC()->session->get( 'purchases_sum' );
    // If not get it and save it
    if( empty($purchases_sum) ){
        // ==> HERE goes the function to get customer's purchases total sum
        $purchases_sum = get_customer_total_purchases_sum();
        // Save it in WC_Session
        WC()->session->set('purchases_sum', $purchases_sum);
    }

    ## 2. Auto applying or removing a coupon code (percentage discount coupon)

    // Apply the coupon if there is at least 2 units of "5 Reusable wet"
    if ( ! $cart->has_discount( $coupon_code ) && $purchases_sum >= 200 ) {
        $cart->add_discount( $coupon_code );
    } elseif( $cart->has_discount( $coupon_code ) && $purchases_sum < 200 ) {
        $cart->remove_coupon( $coupon_code );
    }
}

此代码位于您的活动子主题(或主题)的function.php文件中.经过测试,可以正常工作.

This code goes on function.php file of your active child theme (or theme). Tested and works.

这篇关于根据客户在Woocommerce中的总购买金额添加百分比折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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