我正试图从一个CSV文件导入大约1000个优惠券代码到WooCoommerce
我用过这个密码,但是不管用
此代码可以程序化生成1张优惠券:
$coupon_code = 'UNIQUECODE'; // Code
$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' );
字符串
我需要对csv文件的每一行都做这个函数
你能帮我一下吗?我今天就需要答案。
2条答案
按热度按时间41ik7eoe1#
如果有人仍在寻找答案,可以使用for each循环来完成。我不知道确切的功能关闭我的头上,但步骤是相当简单的,谷歌将帮助你。
首先将CSV导入到数组中。与MySQL查询的方式非常相似。
然后执行PHP
foreach
循环。在循环内部,您使用问题中的代码填充数组中的变量。例如,
字符串
你也可以像这样在Meta部分包含数组数据:
型
同样,这只是一个概括的功能,可以做什么iCode98要求。php手册有很好的信息,也有很多关于将csv转换为数组和for each循环的教程。我可能有一个使用这个,我会更新这个与一个工作的例子,如果我建立它或如果有人真的需要它仍然。
更新:
这应该能够工作,但它确实依赖于输入文件
型
muk1a3rh2#
注意,在你完成之后,要注解掉add_action。如果没有,你将生成代码一遍又一遍,每次有人访问该页面。
或者您可以为函数添加更多限制。
字符串
删除您商店中的所有代码!注意使用它。
型