php – 根据客户在Woocommerce中的总购买金额添加百分比折扣
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 根据客户在Woocommerce中的总购买金额添加百分比折扣,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4569字,纯文字阅读大概需要7分钟。
内容图文
在Woocommerce,我想根据客户的总购买金额设置百分比折扣.例如,如果总购买金额大于或等于200美元,则客户可获得5%的折扣.
所以,我有第一部分代码来显示总和:
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;
}
我想用我的代码和这个答案的代码:
Progressive discount based on cart total in WooCommerce
有没有办法使用它们来设置基于所有订单总和的折扣?
解决方法:
有多种方法可以获得客户的总购买金额:
1)您可以使用wc_get_customer_total_spent()专用Woocommerce功能替换您的第一个功能,但此功能采用所有订单付费状态(“处理”和“已完成”).
2)您也可以仅使用基于similar source code的“已完成”订单状态的轻量级SQL查询:
// 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文件中.经过测试和工作.
3)或者您可以使用自己的问题功能代码(但它更重).由你决定.
百分比折扣(2种方式):
1)负费用:
以下代码将根据客户的总购买金额设置百分比折扣(使用上面的函数):
// 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文件中.经过测试和工作.
2)自动添加优惠券代码(百分比折扣):
首先,您需要设置新的优惠券代码(百分比折扣类型为5%):
然后,您将使用以下代码,它将根据客户的总购买金额自动添加优惠券代码(使用上面的函数):
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文件中.经过测试和工作.
内容总结
以上是互联网集市为您收集整理的php – 根据客户在Woocommerce中的总购买金额添加百分比折扣全部内容,希望文章能够帮你解决php – 根据客户在Woocommerce中的总购买金额添加百分比折扣所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。