在WooCommerce 3+中以编程方式创建多个优惠券

时间:2020-05-18 11:50:03

标签: php wordpress woocommerce foreach coupon

在wooCommerce中,我使用以下代码以编程方式创建单个优惠券:

$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函数自动生成的。

现在,我想创建多个优惠券(而不是单个用户),该优惠券具有多个折扣金额,多个到期日期和多个优惠券代码。

如何在WooCommerce中以编程方式创建具有多个折扣金额的多个优惠券?

1 个答案:

答案 0 :(得分:3)

您的代码未应用(或添加优惠券)…是“创建”新优惠券!

自WooCommerce 3以来,您的代码有些过时了。相反,您最好像下面的代码一样使用可用的WC_Coupon setter methods

在使用wp_generate_password()函数生成优惠券代码名称时,您需要检查新生成的优惠券代码是否不存在,因为WooCommerce要求每个优惠券代码名称都是唯一的(请参见在为此目的的自定义函数下方)

要生成multiple优惠券,您只需进行 foreach循环,即可循环遍历已定义的优惠券费用

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文件中。


2)。现在,带有foreach循环WC_Coupon setter methods的代码可根据已定义的优惠券折扣金额数组(由'fixed_cart'替换不存在的'store_credit'优惠券类型)来生成多个优惠券:

// 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();
}

经过测试可以正常工作。

注释:

  • “ expiry_date”属性替换为“ date_expires”
  • apply_before_tax属性不再存在
  • “免费送货”始终默认设置为false (否)