[英]Create multiple coupons programmatically in WooCommerce 3+
在 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);
它工作正常。 如您所見,優惠券代碼(優惠券名稱)是使用wp_generate_password()
WordPress function 自動生成的。
現在我想創建多個優惠券(而不是單個用戶) ,其中包含多個折扣金額、多個到期日期和多個優惠券代碼。
如何在 WooCommerce 中以編程方式創建具有多個折扣金額的多個優惠券?
您的代碼未應用(或添加優惠券)......它正在“創建”新優惠券! .
由於 WooCommerce 3,您的代碼有點過時了。 相反,您應該更好地使用可用的WC_Coupon
設置方法,如下面的代碼所示。
當您使用wp_generate_password()
function 生成優惠券代碼名稱時,您需要檢查新生成的優惠券代碼是否尚不存在,因為 WooCommerce 要求每個優惠券代碼名稱是唯一的(請參閱下面的自定義 ZC1C125268E67A944 用於此目的的自定義 ZC1C125268E67A944) .
要生成multiple
優惠券,您只需創建一個foreach 循環,該循環將遍歷定義的優惠券成本數組。
1)。 首先一個實用程序 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;
}
代碼進入您的活動子主題(或活動主題)的functions.php
文件。
2)。 現在使用 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();
}
測試和工作。
筆記:
- “expiry_date”屬性被“date_expires”替換
apply_before_tax
屬性不再存在- 默認情況下,“free_shipping”始終設置為
false
(否)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.