问题
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);
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.
How can I create multiple coupons programmatically in WooCommerce with multiple discount amounts?
回答1:
Your code is not applying (or adding a coupon)… It's "creating" a new 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.
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).
To generate multiple
coupons, you can just make a foreach loop that will iterate through a defined array of coupons costs.
1). First a utility function to generate unique non existing coupon names (coupon codes):
// 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).
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.
Notes:
- "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
来源:https://stackoverflow.com/questions/61868924/create-multiple-coupons-programmatically-in-woocommerce-3