如何从 WooCommerce Bookings 中取消的预订创建优惠券?

问题描述

我有一个客户想要在取消预订时自动发放优惠券或商店积分。那可能吗? 我已经查看了“woocommerce_booking_cancelled”操作,但我不确定如何将其连接到制作优惠券的功能。 有什么想法吗?

实数

我已经提前了一点。使用此代码,我已经能够将正确的信息回显到屏幕上。现在我只需要找出如何在正确的客户帐户中创建优惠券。 有什么想法吗?

add_action('woocommerce_booking_cancelled','cancelled_info',10,2);
function cancelled_info($booking_id,$booking)
{
// Create random coupon code
$characters = "ABCDEFGHJKMNPQRSTUVWXYZ23456789";
$char_length = "8";
$random_cupon_code = substr(str_shuffle($characters),$char_length);
// Get the user ID from the Booking ID
$user_id = get_post_field('post_author',$booking_id);
$coupon_code = $random_cupon_code;
$download_credits = get_post_meta($booking_id,'_booking_cost',true);
/**
* Create a coupon programatically
*/
$amount = $download_credits; // Amount
$discount_type = 'fixed_cart'; // Type: fixed_cart,percent,fixed_product,percent_product
$coupon = array(
'post_title' => $coupon_code,'post_content' => '','post_status' => 'publish','post_author' => 1,'post_type' => 'shop_coupon'
);
$new_coupon_id = wp_insert_post($coupon);
// Add meta
update_post_meta($new_coupon_id,'discount_type',$discount_type);
update_post_meta($new_coupon_id,'coupon_amount',$amount);
update_post_meta($new_coupon_id,'individual_use','no');
update_post_meta($new_coupon_id,'product_ids','');
update_post_meta($new_coupon_id,'exclude_product_ids','usage_limit','1');
update_post_meta($new_coupon_id,'usage_limit_per_user','expiry_date','apply_before_tax','yes');
update_post_meta($new_coupon_id,'free_shipping','no');
echo "Verdi: " . $download_credits . "coupon id: " . $new_coupon_id . 
"coupon code: " . $coupon_code;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...