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

时间:2013-07-22 14:11:51

标签: php wordpress woocommerce

我一直在寻找一种方法来批量添加优惠券到WooCommerce,它实际上是一个包含800个会员号码的列表,可以给予折扣,优惠券似乎是最好的方式。

我找到了一种以编程方式添加单个优惠券的方法:http://docs.woothemes.com/document/create-a-coupon-programatically/但是我有限的PHP知识似乎不足以添加800.

我猜一个数组或以某种方式链接到.csv会这样做,但我不确定。我会感激任何帮助。

1 个答案:

答案 0 :(得分:3)

您可以创建一个包含800个不同键的数组,只需创建一个foreach循环,为数组中的每个不同键重复此过程

forehach ( $your_800_array as $coupon_code){
 //the code from the page you posted
 $amount = '10'; // Amount
 $discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
 $coupon = array(
 'post_title' => $coupon_code,
 'post_content' => '',
 'post_status' => 'publish',
 'post_author' => 1,
 'post_type' => 'shop_coupon'
 );
 $new_coupon_id = wp_insert_post( $coupon );
 // Add meta
 update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
 update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
 update_post_meta( $new_coupon_id, 'individual_use', 'no' );
 update_post_meta( $new_coupon_id, 'product_ids', '' );
 update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
 update_post_meta( $new_coupon_id, 'usage_limit', '' );
 update_post_meta( $new_coupon_id, 'expiry_date', '' );
 update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
 update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
}

(使用OP在循环内发布的代码。未实际测试过)