2

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

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

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

4

1 回答 1

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 在循环中发布的代码。未经实际测试)

于 2014-11-18T03:52:20.757 回答