Php 在WooCommerce 3中以编程方式创建多个优惠券+
在wooCommerce中,我使用以下代码以编程方式创建单个优惠券: 美元息票金额='20'; $code_value=wp_generate_password 15,false; $code\U code=$code\U值; $expiration_date=日期'Y-m-d',标准时间'+140天'; 函数创建优惠券代码$优惠券金额,$优惠券代码,$到期日期,$电子邮件地址{ 全球$wpdb; $sql=$wpdb->prepare SELECT ID FROM$wpdb->post_title=%s和post_type='shop_coupon'和post_status='publish'按post_date DESC LIMIT 1;发布订单,$coupon_code; $toucon\u id=$wpdb->get\u var$sql; 如果$优惠券id为空{ $优惠券=阵列 “post_title”=>$优惠券代码, “发布内容”=>, “发布状态”=>“发布”, “后作者”=>1, “post_type”=>“shop_优惠券” ; $newcouponid=wp\u insert\u post$优惠券; 更新发布元数据$newcouponid,“产品ID”; 更新发布元数据$newcouponid,“排除产品ID”; 更新发布元$newcouponid,'折扣类型','商店信用'; 更新'u post'u meta$newcouponid','free'u shipping','no'; 更新$newcouponid、'coupon\u amount'、$coupon\u amount; 更新发布元$newcouponid,'个人使用','是'; 更新$newcouponid、$expiration\u date、$expiration\u date; 更新'u post'u meta$newcouponid','usage'u limit','1'; 更新后元$newcouponid,'税前申请','是'; 更新\u post\u meta$newcouponid,'customer\u email',$email\u address; } }Php 在WooCommerce 3中以编程方式创建多个优惠券+,php,wordpress,woocommerce,foreach,coupon,Php,Wordpress,Woocommerce,Foreach,Coupon,在wooCommerce中,我使用以下代码以编程方式创建单个优惠券: 美元息票金额='20'; $code_value=wp_generate_password 15,false; $code\U code=$code\U值; $expiration_date=日期'Y-m-d',标准时间'+140天'; 函数创建优惠券代码$优惠券金额,$优惠券代码,$到期日期,$电子邮件地址{ 全球$wpdb; $sql=$wpdb->prepare SELECT ID FROM$wpdb->post_ti
创建优惠券代码$优惠券金额,$优惠券代码,$到期日期,$电子邮件地址 您的代码没有应用或添加优惠券…它正在创建新优惠券 自从WooCommerce 3以来,您的代码有点过时。相反,您应该更好地使用下面代码中提供的 当您使用wp_generate_password函数生成优惠券代码名时,您需要检查新生成的优惠券代码是否存在,因为WooCommerce要求每个优惠券代码名都是唯一的。为此,请参见下面的自定义函数 要生成多个优惠券,您只需创建一个foreach循环,该循环将遍历优惠券成本的定义数组 一,。首先是一个实用函数,用于生成唯一的不存在优惠券名称优惠券代码: 测试和工作 注: expirement\u date属性被date\u expires替换 税前应用属性不再存在 默认情况下,免费送货始终设置为false no
// Utility function that generate a non existing coupon code (as each coupon code has to be unique)
function generate_coupon_code() {
global $wpdb;
// Get an array of all existing coupon codes
$coupon_codes = $wpdb->get_col("SELECT post_name FROM $wpdb->posts WHERE post_type = 'shop_coupon'");
for ( $i = 0; $i < 1; $i++ ) {
$generated_code = strtolower( wp_generate_password( 15, false ) );
// Check if the generated code doesn't exist yet
if( in_array( $generated_code, $coupon_codes ) ) {
$i--; // continue the loop and generate a new code
} else {
break; // stop the loop: The generated coupon code doesn't exist already
}
}
return $generated_code;
}
// Here below define your coupons discount amount
$discount_amounts = array( 12, 18, 15, 10 );
// Set some coupon data by default
$date_expires = date('Y-m-d', strtotime('+371 days'));
$discount_type = 'fixed_cart'; // 'store_credit' doesn't exist
// Loop through the defined array of coupon discount amounts
foreach( $discount_amounts as $coupon_amount ) {
// Get an emty instance of the WC_Coupon Object
$coupon = new WC_Coupon();
// Generate a non existing coupon code name
$coupon_code = generate_coupon_code();
// Set the necessary coupon data (since WC 3+)
$coupon->set_code( $coupon_code );
$coupon->set_discount_type( $discount_type );
$coupon->set_amount( $coupon_amount );
$coupon->set_date_expires( $date_expires );
$coupon->set_usage_limit( 1 );
$coupon->set_individual_use( true );
// Create, publish and save coupon (data)
$coupon->save();
}