[英]Create multiple coupons programmatically in WooCommerce 3+
In wooCommerce, I am using the following code to create programmatically a single coupon:在 wooCommerce 中,我使用以下代码以编程方式创建单个优惠券:
$coupon_amount = '20'; $code_value = wp_generate_password( 15, false ); $coupon_code = $code_value; $expiry_date = date('Ym-d', strtotime('+140 days')); function create_coupon_codes($coupon_amount, $coupon_code, $expiry_date, $email_address) { global $wpdb; $sql = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1;", $coupon_code ); $coupon_id = $wpdb->get_var( $sql ); if ( empty( $coupon_id ) ) { $coupon = array( 'post_title' => $coupon_code, 'post_content' => '', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'shop_coupon' ); $newcouponid = wp_insert_post( $coupon ); update_post_meta( $newcouponid, 'product_ids', '' ); update_post_meta( $newcouponid, 'exclude_product_ids', '' ); update_post_meta( $newcouponid, 'discount_type', 'store_credit' ); update_post_meta( $newcouponid, 'free_shipping', 'no' ); update_post_meta( $newcouponid, 'coupon_amount', $coupon_amount ); update_post_meta( $newcouponid, 'individual_use', 'yes' ); update_post_meta( $newcouponid, 'expiry_date', $expiry_date ); update_post_meta( $newcouponid, 'usage_limit', '1' ); update_post_meta( $newcouponid, 'apply_before_tax', 'yes' ); update_post_meta( $newcouponid, 'customer_email', $email_address ); } } create_coupon_codes($coupon_amount, $coupon_code, $expiry_date, $email_address);
It's working fine.它工作正常。 As you can see, the coupon code (coupon name) is auto generated using wp_generate_password()
WordPress function.如您所见,优惠券代码(优惠券名称)是使用wp_generate_password()
WordPress function 自动生成的。
Now I would like to create multiple coupons (instead a single user) with multiple discount amounts and multiple expiry dates and multiple coupon codes.现在我想创建多个优惠券(而不是单个用户) ,其中包含多个折扣金额、多个到期日期和多个优惠券代码。
How can I create multiple coupons programmatically in WooCommerce with multiple discount amounts?如何在 WooCommerce 中以编程方式创建具有多个折扣金额的多个优惠券?
Your code is not applying (or adding a coupon)… It's "creating" a new coupon!您的代码未应用(或添加优惠券)......它正在“创建”新优惠券! . .
since WooCommerce 3, your code is a bit outdated.由于 WooCommerce 3,您的代码有点过时了。 Instead you should better use the available WC_Coupon
setter methods like in the code below.相反,您应该更好地使用可用的WC_Coupon
设置方法,如下面的代码所示。
As you are generating the coupon code name with wp_generate_password()
function, you need to check that the new generated coupon code doesn't exist yet, as WooCommerce requires that each coupons code name is unique (see below a custom function for that purpose) .当您使用wp_generate_password()
function 生成优惠券代码名称时,您需要检查新生成的优惠券代码是否尚不存在,因为 WooCommerce 要求每个优惠券代码名称是唯一的(请参阅下面的自定义 ZC1C125268E67A944 用于此目的的自定义 ZC1C125268E67A944) .
To generate multiple
coupons, you can just make a foreach loop that will iterate through a defined array of coupons costs .要生成multiple
优惠券,您只需创建一个foreach 循环,该循环将遍历定义的优惠券成本数组。
1). 1)。 First a utility function to generate unique non existing coupon names (coupon codes) :首先一个实用程序 function 生成唯一的非现有优惠券名称(优惠券代码) :
// 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;
}
Code goes in functions.php
file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的functions.php
文件。
2). 2)。 Now the code with the foreach loop WC_Coupon
setter methods to generate multiple coupons based on a defined array of coupon discount amounts (replaced non existing 'store_credit' coupon type by 'fixed_cart') :现在使用 foreach 循环WC_Coupon
设置方法的代码根据定义的优惠券折扣金额数组生成多个优惠券(将不存在的 'store_credit' 优惠券类型替换为 'fixed_cart') :
// 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();
}
Tested and works.测试和工作。
Notes:笔记:
- "expiry_date" property is replaced by "date_expires" “expiry_date”属性被“date_expires”替换
apply_before_tax
property doesn't exist anymoreapply_before_tax
属性不再存在- "free_shipping" is always set to
false
(no) by default默认情况下,“free_shipping”始终设置为false
(否)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.