php - 将优惠券添加到现有的 WooCommerce 订阅
问题描述
我已经启动了一个推荐计划,它为用户提供了一个 25 英镑的折扣优惠券代码,它非常有效,但不适用于现有订阅者我如何才能进行续订并应用自动优惠券,以便订阅也可以从折扣中受益。?
我可以做到这一点woocommerce_new_order
吗?
编辑:
更新帖子以帮助其他人我最终使用的代码以利用现有的订阅者利用以下优惠券:
// discount subscription renewals
function discount_subscription_renewals($billing_email){
$query_args = array(
'posts_per_page' => -1,
'post_type' => 'shop_coupon',
'post_status' => 'publish',
'orderby' => array( 'title' => 'ASC' ),
'meta_query' => array(
array(
'key' => 'discount_type',
'value' => 'fixed_cart',
),
),
);
$coupons = get_posts( $query_args );
$coupon_code = false;
$discount = 0;
foreach ( $coupons as $coupon ) {
$customer_email = array($coupon->customer_email[0]);
$amount = $coupon->coupon_amount;
$usage_count = $coupon->usage_count;
$usage_limit = $coupon->usage_limit;
if (in_array($billing_email,$customer_email) && ($usage_count < $usage_limit)){
$coupon_code = $coupon->post_title;
break;
//$discount += $amount;
}
}
return $coupon_code;
}
function apply_discount_on_renewal_order_created( $renewal_order, $subscription ) {
$order = new WC_Order($renewal_order->get_id() );
$billing_email = $order->get_billing_email();
$coupon_code = discount_subscription_renewals($billing_email);
if ($coupon_code){
$order->apply_coupon($coupon_code);
//$order->set_total( $order->get_total() - $coupon_amount );
}
return $order;
}
add_filter( 'wcs_renewal_order_created', 'apply_discount_on_renewal_order_created', 10, 2 );
解决方案
function apply_discount_on_renewal_order_created( $renewal_order, $subscription ) {
$order = new WC_Order( $renewal_order->get_id() );
$coupon_amount = 25;
$order->set_total( $order->get_total() - $coupon_amount );
return $order;
}
add_filter( 'wcs_renewal_order_created', 'apply_discount_on_renewal_order_created', 10, 2 );
推荐阅读
- mysql - 按左外连接联合右外连接排序
- python - OpenPose 如何解决 GPU 内存的 F0414 错误?
- jquery - Jquery Datatables 自定义列搜索
- java - 连接 org.apache.http.conn.HttpHostConnect 时出现超时错误
- sql - 当未定义可能性列表时,使用查找表与“其他”情况的值
- python - 将 RestAPI 与聊天机器人合并
- c# - 在 MongoDB 中存储动态 JSON 数据
- java - 在 Java InputStream 中从 ByteArrayInputStream 转换为 BufferedInputStream
- python-3.x - 如何使用python将字段值存储在嵌套字典中?
- python - 如何通过调整中间的空格对子图进行分组