php - 如何更新 Woocommerce 中的优惠券代码对象

标签 php wordpress methods woocommerce coupon

如何更新 Woocommerce (Wordpress) 中的优惠券代码对象。

$beforeadduseremail="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7400110700341319151d185a171b19" rel="noreferrer noopener nofollow">[email protected]</a>";

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     = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb9f8e989fab8c868a8287c5888486" rel="noreferrer noopener nofollow">[email protected]</a>'; // 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 methodsWC_Coupon 对象实例上(自 WooCommerce 3 起):

$coupon_code = 'happysummer'; // The coupon code
$user_email  = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7501100601351218141c195b161a18" rel="noreferrer noopener nofollow">[email protected]</a>'; // 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();

关于php - 如何更新 Woocommerce 中的优惠券代码对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56059687/

相关文章:

php - 从匹配的表 A 和表 B 中创建一个表

php - 10,000,000行数据库的最佳存储引擎

javascript - 基本对象/函数链在 JavaScript 中是如何工作的?

java - 以编程方式检索方法的参数和值

Php:覆盖抽象方法出错

php - 在 php 中的一个字段中输入值并将另一个字段留空会得到错误的结果

html - 在 Pagelines Pro Wordpress 主题中指定 <img> 的图像尺寸

WordPress 重命名主题文件夹

javascript - 为 li 列表创建 "prev/next"

java - 将方法调用放入 ArrayList?