Php 将优惠券添加到预先存在的WooCommerce订阅
我已经启动了一个推荐计划,为用户提供25英镑的折扣优惠券代码,该代码非常有效,但不适用于已有的订阅者。我如何连接到续订并应用自动优惠券,以便订阅者也能从折扣中受益 我可以通过Php 将优惠券添加到预先存在的WooCommerce订阅,php,wordpress,woocommerce,hook-woocommerce,woocommerce-subscriptions,Php,Wordpress,Woocommerce,Hook Woocommerce,Woocommerce Subscriptions,我已经启动了一个推荐计划,为用户提供25英镑的折扣优惠券代码,该代码非常有效,但不适用于已有的订阅者。我如何连接到续订并应用自动优惠券,以便订阅者也能从折扣中受益 我可以通过woocommerce\u new\u order实现这一点吗 编辑: 更新帖子以帮助他人我最后使用的代码利用现有订阅者使用以下优惠券: // discount subscription renewals function discount_subscription_renewals($billing_email){
woocommerce\u new\u 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 );
//折扣订阅续订
功能折扣\订阅\续费($billing\电子邮件){
$query\u args=数组(
“每页帖子数”=>-1,
“post_type”=>“shop_优惠券”,
“发布状态”=>“发布”,
'orderby'=>array('title'=>'ASC'),
“元查询”=>数组(
排列(
'键'=>'折扣类型',
'value'=>'fixed_cart',
),
),
);
$coups=get\u posts($query\u args);
$优惠券代码=假;
$折扣=0;
foreach($优惠券作为$优惠券){
$customer_email=array($优惠券->customer_email[0]);
$金额=$优惠券->优惠券金额;
$usage\u count=$优惠券->usage\u count;
$usage\u limit=$优惠券->usage\u limit;
if(在数组中($billing\u email,$customer\u email)&($usage\u count<$usage\u limit)){
$coupon\u code=$coupon->post\u title;
打破
//$折扣+=$金额;
}
}
返回$U代码;
}
函数在创建续订订单时应用折扣($续订订单,$subscription){
$order=new WC\u order($renewal\u order->get\u id());
$billing_email=$order->get_billing_email();
$优惠券\代码=折扣\订阅\续费($billing\电子邮件);
if($U优惠券代码){
$order->apply_优惠券(优惠券代码);
//$order->set_total($order->get_total()-$coupon_amount);
}
退回$order;
}
添加过滤器(“wcs更新订单创建”,“在更新订单创建时应用折扣”,10,2);
现在我只需要一种方法来循环浏览所有优惠券,如果对该客户有效,请应用它。有什么帮助吗?嗨,迈克,你能帮我实现吗?只需复制上面的代码并将其添加到主题文件夹中的functions.php文件中谢谢迈克,感谢你的回复。但是他们在哪里应用优惠券代码呢?为我缺乏知识而道歉。他们不需要应用它。如果你为某个特定的人添加了优惠券,如果是他们的优惠券,那么当有订阅续订时,它将自动应用,而这在2021年不再有效。然而我认为这是因为get_billing_电子邮件已不再使用。改用:$order->billing\u email;
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 );