php - WooCommerce:如果客户是 VIP,则自动生成优惠券
问题描述
如果有人花费超过 750 美元,我想生成一张优惠券。
我在想我可以在结帐页面或电子邮件上显示优惠券。优惠券的有效期为自发行之日起 12 个月。
我发现了一些非常有用的代码,我将它们组装成我认为应该可以工作的东西,但它没有
我将非常感谢您指出我可能会出错的地方。
首先,如果客户确实提取了用户 ID 和订单 ID 并发送到优惠券代码生成,我想看看他们是否花费了超过 750 美元
// Function to work out if a customer is a VIP
function ch_cust_is_a_vip()
{
if (ch_get_customer_spend() > 750)
{
$ch_order = wc_get_order( $order_id ); // get current order_id
$ch_user = get_current_user_id(); // Current user ID
coupon_code_generation( $ch_order, $ch_user );
}
}
add_filter( 'woocommerce_before_checkout_form_cart_notices', 'ch_cust_is_a_vip', 20, 2 ); // woocommerce_before_checkout_form_cart_notices
计算客户支出的功能
// function to return total customer spend
function ch_get_customer_spend()
{
global $wpdb;
$user_id = get_current_user_id(); // Current user ID
$user_purchases_total_sum = $wpdb->get_var( "
SELECT SUM(pm.meta_value) FROM {$wpdb->prefix}postmeta as pm
INNER JOIN {$wpdb->prefix}posts as p ON pm.post_id = p.ID
INNER JOIN {$wpdb->prefix}postmeta as pm2 ON pm.post_id = pm2.post_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 $user_id " );
return $user_purchases_total_sum;
}
和优惠券代码生成,我发现这个广告刚刚添加了 12 个月的过期日期。
// Utility function that check if coupon exist
function does_coupon_exist( $coupon_code )
{
global $wpdb;
$value = $wpdb->get_var( "
SELECT ID
FROM {$wpdb->prefix}posts
WHERE post_type = 'shop_coupon'
AND post_name = '".strtolower($coupon_code)."'
AND post_status = 'publish'; ");
return $value > 0 ? true : false;
}
////////////////////////////////////
// custom coupon generation
///////////////////////////////////
function coupon_code_generation( $order_id, $i )
{
$coupon_code = $order_id."".$i; // Coupon Code
// Check that coupon code not exists
if( ! does_coupon_exist( $coupon_code ) )
{
// Get a new instance of the WC_Coupon object
$coupon = new WC_Coupon();
// Get the instance of the WC_Order object
$order = wc_get_order( $order_id );
## --- Coupon settings --- ##
$discount_type = 'percent'; // set the dicount Type
$coupon_amount = '30'; // set the discount Amount
$customer_email = array( $order->get_billing_email() ); // Customer billing email
$product_categories_names = array('skincare');
$date_expires = strtotime("+ 12 Months"); // put an expiry date 12 months from today
// Convert to term IDs
$term_ids = array();
foreach( $product_categories_names as $term_name )
{
if ( term_exists( $term_name, 'product_cat' ) )
$term_ids[] = get_term_by( 'name', $term_name, 'product_cat' )->term_id;
}
## --- Coupon settings --- ##
// Set the necessary coupon data
$coupon->set_code( $coupon_code );
$coupon->set_discount_type( $discount_type );
$coupon->set_amount( $coupon_amount );
if( is_array($term_ids) && sizeof($term_ids) > 0 )
$coupon->set_product_categories( $term_ids );
$coupon->set_email_restrictions( $customer_email );
$coupon->set_individual_use( true );
$coupon->set_usage_limit( 1 );
$coupon->set_usage_limit_per_user( 1 );
$coupon->set_limit_usage_to_x_items( 1 );
$coupon->set_date_expires( date( "Y-m-d H:i:s", strtotime($date_expires) ) );
// Save the data
$post_id = $coupon->save();
}
echo isset($post_id) && $post_id > 0 ? sprintf(
'<div class="couponCode"><strong>%s <code>%s</code></strong>.<hr></div>',
__("Your Coupon Code for your next purchase is", "woocommerce"), $coupon_code
) : __("Sorry, a coupon code already exist.", "woocommerce");
}
我有时确实会看到“ $customer_email = array( $order->get_billing_email() );
//客户帐单电子邮件”的错误,但并非总是如此
解决方案
推荐阅读
- scala - 在scala的split()方法中使用单引号和双引号有什么区别?
- javascript - 在 laravel 中使用 axios 调用的 Vue 自动完成
- amazon-web-services - 尝试使用 AWS cli 执行 AWS s3 ls 时访问被拒绝
- android - 按键排序数组适用于 iOS,但不适用于 Anroid
- neo4j - 在 Neo4J 中,如何匹配子图,但过滤另一个子图中的节点
- c# - 将 MSTest 与 ANTS Performance Profiler 一起使用时缺少源代码
- android - Appium 自动服务器 - 没有找到可以自动化 Chrome '68.0.3440' 的 Chromedriver
- bash - AWK 在代表命令结果的数字中添加数字
- php - 有没有办法拦截对特定目录的请求,然后选择我想如何响应该请求?
- python - 将日期添加到列表但 datetime.datetime 不可迭代