如何在 Woocommerce 中更新优惠券代码对象
How to Update Coupon Code Objects in Woocommerce
如何在 Woocommerce (Wordpress) 中更新优惠券代码对象。
$beforeadduseremail="test@gmail.com";
update_post_meta( 21, 'email_restrictions', $beforeadduseremail);
可以通过不同的方式完成:
在 customer_email
元键上使用 Wordpress get_post_meta()
和 update_post_meta()
:
$coupon_post_id = 21; // The post ID
$user_email = 'test@gmail.com'; // The email to be added
// Get existing email restrictions
$email_restrictions = (array) get_post_meta( $coupon_post_id, 'customer_email', true );
// Add the new email to existing emails
if ( ! in_array( $user_email, $email_restrictions ) ) {
$email_restrictions[] = $user_email;
}
// Set a back updated email restrictions array
update_post_meta( $coupon_post_id, 'customer_email', $email_restrictions );
在 WC_Coupon
对象实例上使用 CRUD methods(自 WooCommerce 3 起):
$coupon_code = 'happysummer'; // The coupon code
$user_email = 'test@gmail.com'; // The email to be added
// Get an instance of the WC_Coupon object
$coupon = new WC_Coupon( $coupon_code );
// Get email restrictions
$email_restrictions = (array) $coupon->get_email_restrictions();
// Add the customer email to the restrictions array
$email_restrictions[] = $customer_email;
// set the new array of email restrictions
$coupon->set_email_restrictions( $email_restrictions );
// Save the coupon data
$coupon->save();
如何在 Woocommerce (Wordpress) 中更新优惠券代码对象。
$beforeadduseremail="test@gmail.com";
update_post_meta( 21, 'email_restrictions', $beforeadduseremail);
可以通过不同的方式完成:
在 customer_email
元键上使用 Wordpress get_post_meta()
和 update_post_meta()
:
$coupon_post_id = 21; // The post ID
$user_email = 'test@gmail.com'; // The email to be added
// Get existing email restrictions
$email_restrictions = (array) get_post_meta( $coupon_post_id, 'customer_email', true );
// Add the new email to existing emails
if ( ! in_array( $user_email, $email_restrictions ) ) {
$email_restrictions[] = $user_email;
}
// Set a back updated email restrictions array
update_post_meta( $coupon_post_id, 'customer_email', $email_restrictions );
在 WC_Coupon
对象实例上使用 CRUD methods(自 WooCommerce 3 起):
$coupon_code = 'happysummer'; // The coupon code
$user_email = 'test@gmail.com'; // The email to be added
// Get an instance of the WC_Coupon object
$coupon = new WC_Coupon( $coupon_code );
// Get email restrictions
$email_restrictions = (array) $coupon->get_email_restrictions();
// Add the customer email to the restrictions array
$email_restrictions[] = $customer_email;
// set the new array of email restrictions
$coupon->set_email_restrictions( $email_restrictions );
// Save the coupon data
$coupon->save();