php - 如何在 Woocommerce 中更新优惠券代码对象
问题描述
如何在 Woocommerce (Wordpress) 中更新优惠券代码对象。
$beforeadduseremail="test@gmail.com";
update_post_meta( 21, 'email_restrictions', $beforeadduseremail);
解决方案
它可以通过不同的方式完成:
使用Wordpressget_post_meta()
和元键:update_post_meta()
customer_email
$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 );
在对象实例上使用CRUD 方法(从 WooCommerce 3 开始):WC_Coupon
$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();
推荐阅读
- python - 遍历 DataFrame 并跟踪某个序列持续时间
- php - 用 PHP 字符串中的外部链接替换相对链接
- jenkins - 有没有办法使用 jenkins-cli 获取 jenkins 内部版本号
- python - 当 precompute_distances 设置为 True 时,k-means 预计算哪些距离?
- database - 从旧版本开始自动更新 wordpress 数据库
- octave - 如何在 ubuntu 上修复 octave 上的 xlsread 错误
- objective-c - 无法在 NSBundle 中获取图像路径?
- google-cloud-storage - 使用 Google datalab:将 csv 写入存储
- scala - 映射值在 scala Flink 中不返回任何内容
- node.js - 如何在 react-redux 框架上显示来自节点服务器的成功响应