我正在创建一个功能,可以在woocommerce的每次购买中制作新的优惠券。我想将客户电子邮件限制为当前用户,但无法在设置中成功使用动态值:
$order = new WC_Order( $order_id );
if ( $order->user_id > 0 ) {
$coupon_code = 'UNIQUECODE'; // Code
$amount = '10'; // Amount
$discount_type = 'ign_store_credit';
$customer_email = ''; // How do I dynamically use the current user's email here?
$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', '' );
update_post_meta( $new_coupon_id, 'usage_limit', '' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
update_post_meta( $new_coupon_id, 'customer_email', $customer_email );
我尝试过$ user-> user_email,$ user_email
答案 0 :(得分:0)
使用Woocommerce版本3 + CRUD setters and getters methods从订单结算电子邮件中创建一个包含电子邮件限制的新优惠券:
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Only for registered customers
if ( $order->get_user_id() > 0 ){
// Coupon settings
$coupon_code = 'UNIQUECODE';
$discount_type = 'ign_store_credit';
$coupon_amount = '10'; // Amount
$customer_email = $order->get_billing_email();
// Get a new instance of the WC_Coupon object
$coupon = new WC_Coupon();
// Set the necessary coupon data
$coupon->set_code( $coupon_code );
$coupon->set_discount_type( $discount_type );
$coupon->set_amount( $coupon_amount );
$coupon->set_email_restrictions( array( $customer_email ) );
// Save the data
$coupon->save();
}
这样代码更轻,更高效,更紧凑。
经过测试和工作。