在 WooCommerce 中以编程方式创建多个优惠券 [英] Programatically create multiple coupons in WooCommerce

查看:33
本文介绍了在 WooCommerce 中以编程方式创建多个优惠券的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been looking for a way to add coupons in bulk to WooCommerce, it's actually a list of 800 membership numbers, that grant a discount, and coupons seem to the best way to do this.

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

I've found a way to add a single coupon programatically: http://docs.woothemes.com/document/create-a-coupon-programatically/ but my limited PHP knowledge doesn't seem adequate to add 800.

我猜测数组或以某种方式链接到 .csv 会做到这一点,但我不确定.如有任何帮助,我将不胜感激.

I'm guessing an array or somehow linking to a .csv would do it, but I'm not sure. I'd be grateful for any help.

推荐答案

您可以创建一个包含 800 个不同键的数组,然后执行 foreach 循环来为数组上的每个不同键重复该过程,如下所示

You could create an array of 800 diferent keys and just make a foreach loop to repeat the process for each different key you have on the array like this

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

(Used the code that OP posted inside the loop. Not actually tested)

这篇关于在 WooCommerce 中以编程方式创建多个优惠券的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆