问题描述
在wooCommerce中,我使用以下代码以编程方式创建单个优惠券:
In wooCommerce, I am using the following code to create programmatically a single coupon:
$coupon_amount = '20';
$code_value = wp_generate_password( 15, false );
$coupon_code = $code_value;
$expiry_date = date('Y-m-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);
一切正常.如您所见,优惠券代码(优惠券名称)是使用 wp_generate_password()
WordPress功能.
It's working fine. As you can see, the coupon code (coupon name) is auto generated using 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.
如何在WooCommerce中以编程方式创建具有多个折扣金额的多个优惠券?
How can I create multiple coupons programmatically in WooCommerce with multiple discount amounts?
推荐答案
您的代码未应用(或添加优惠券)……这是正在创建"新的优惠券!.
Your code is not applying (or adding a coupon)… It's "creating" a new coupon!.
自WooCommerce 3起,您的代码有些过时了.相反,您最好使用可用的 WC_Coupon
设置器方法 ,如下面的代码所示.
since WooCommerce 3, your code is a bit outdated. Instead you should better use the available WC_Coupon
setter methods like in the code below.
在使用wp_generate_password()
函数生成优惠券代码名称时,您需要检查新生成的优惠券代码是否不存在,因为WooCommerce要求每个优惠券代码名称都是唯一的(请参见下文自定义功能).
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).
要生成multiple
个优惠券,您只需进行 foreach循环,该循环将循环遍历已定义的优惠券费用.
To generate multiple
coupons, you can just make a foreach loop that will iterate through a defined array of coupons costs.
1).首先一个实用程序函数,用于生成唯一的不存在的优惠券名称 (优惠券代码):
// 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;
}
代码进入您的活动子主题(或活动主题)的functions.php
文件中.
Code goes in functions.php
file of your active child theme (or active theme).
2).现在,带有foreach循环的代码 WC_Coupon
设置方法 ,以基于已定义的优惠券折扣金额数组(用"fixed_cart"替换不存在的"store_credit"优惠券类型)来生成多个优惠券:
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'):
// Here below define your coupons discount ammount
$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.
- 到期日期";属性被替换为"date_expires"
-
apply_before_tax
属性不再存在 - "free_shipping"默认情况下始终设置为
false
(no)
- "expiry_date" property is replaced by "date_expires"
apply_before_tax
property doesn't exist anymore- "free_shipping" is always set to
false
(no) by default
这篇关于在WooCommerce 3+中以编程方式创建多个优惠券的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!