douxiza9868 2013-07-22 14:11
浏览 31

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

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.

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.

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.

  • 写回答

1条回答 默认 最新

  • dongzhuo3059 2014-11-18 03:52
    关注

    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' );
    }
    

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

    评论

报告相同问题?

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题