donpb2823 2019-07-04 22:18
浏览 118
已采纳

有没有办法根据一定数量生成一些Woocommerce优惠券?

I'm fixing a plugin-loaded Woocommerce site and now I have a requirement to implement an automatic coupon generator based on the purchase amount: for example, I would like to establish a rule for every $100 purchase, if the customer buys $450, 4 discount coupons of $40 will be generated. All of this codes should be sent to the customer.

I've reviewed the WooCommerce documentation where I can create the coupons: https://docs.woocommerce.com/document/create-a-coupon-programatically/ and already implemented it and sending in the order review mail (after payment has been confirmed):

function add_order_email_instructions( $order, $sent_to_admin ) {

    if ( ! $sent_to_admin && $order->get_user_id() && 'processing' == $order->get_status()) {

        $lista_cupones  = array();
        $amount = '40';

        for ($i=1; $i<=$order->get_total()/100; $i++) {

            $longitud = 12;
            $key = '';
            $pattern = '1234567890ABCDFGHIJKLMNOPQRSTUWYZ';
            $max = strlen($pattern)-1;
            for($i=0;$i < $longitud;$i++) $key .= $pattern{mt_rand(0,$max)};
            $coupon_code = $key;

            $discount_type = 'fixed_cart'; 
            $coupon = array(
                'post_title' => $coupon_code,
                'post_content' => ''.$to,
                'post_status' => 'publish',
                'post_author' => 1,
                'post_type'     => 'shop_coupon'
            );

            $new_coupon_id = wp_insert_post( $coupon );
            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', 'yes' );
            update_post_meta( $new_coupon_id, 'product_ids', '' );
            update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
            update_post_meta( $new_coupon_id, 'minimum_amount', '100' );
            update_post_meta( $new_coupon_id, 'exclude_sale_items', 'yes' );
            update_post_meta( $new_coupon_id, 'exclude_sale', 'yes' );
            update_post_meta( $new_coupon_id, 'usage_limit', '1' );
            update_post_meta( $new_coupon_id, 'expiry_date', strtotime("+3 days") );
            update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
            update_post_meta( $new_coupon_id, 'free_shipping', 'no' );

            array_push($lista_cupones, $coupon_code);

        }
        echo '<h2>';

        printf( __( 'Obt&eacute;n 40 soles de descuento')
        );
        echo '</h2>';

        printf(
            __( 'Gracias por su compra. Use el código de descuento <strong>%2$s</strong> para recibir %1$s soles de descuento en su siguiente compra.'),
            $amount,
            $coupon_code
        );
        echo '</p>';
    }
}
add_action('woocommerce_email_before_order_table','add_order_email_instructions',10);

Now this half works: it validates if order total is less than $100 and doesn't generate a coupon but if it's higher it only gives me one coupon even if order total is 200, 300 400, etc. (it doesn't even generate more than one coupon, seems like it only goes through the loop once and stops).

Thanks!

  • 写回答

1条回答 默认 最新

  • dqdl6469 2019-07-16 17:13
    关注

    Already solved, this is the code:

    function create_coupon () {
        $amount = '40';
        $longitud = 12;
        $key = '';
        $pattern = '1234567890ABCDFGHIJKLMNOPQRSTUWYZ';
        $max = strlen($pattern)-1;
        for($i=0;$i < $longitud;$i++) $key .= $pattern{mt_rand(0,$max)};
        $coupon_code = $key;
    
        $discount_type = 'fixed_cart'; 
        $coupon = array(
            'post_title' => $coupon_code,
            'post_content' => ''.$to,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_type'     => 'shop_coupon'
        );
    
        $new_coupon_id = wp_insert_post( $coupon );
        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', 'yes' );
        update_post_meta( $new_coupon_id, 'product_ids', '' );
        update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
        update_post_meta( $new_coupon_id, 'minimum_amount', '100' );
        update_post_meta( $new_coupon_id, 'exclude_sale_items', 'yes' );
        update_post_meta( $new_coupon_id, 'exclude_sale', 'yes' );
        update_post_meta( $new_coupon_id, 'usage_limit', '1' );
        update_post_meta( $new_coupon_id, 'expiry_date', strtotime("+3 days") );
        update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
        update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
        //update_post_meta( $new_coupon_id, 'customer_email', $order->get_billing_email());
    
        return $coupon_code;
    }
    
    function add_order_email_instructions( $order, $sent_to_admin ) {
    
            if ( ! $sent_to_admin && $order->get_user_id() && 'processing' == $order->get_status()) {
    
                $lista_cupones  = [];
                $numero_cupones = $order->get_total()/100;
                for ($i=1; $i<=$numero_cupones; $i++) { 
                    array_push($lista_cupones, create_coupon());
                }
                echo '<h2>';
    
                printf( __( 'Obt&eacute;n 40 soles de descuento')
                );
                echo '</h2>';
    
                printf(
                    __( 'Gracias por su compra. Use el código de descuento <strong>%2$s</strong> para recibir %1$s soles de descuento en su siguiente compra.'),
                    $amount,
                    $lista_cupones 
                );
                echo '<br></p>';
            }
    }
    add_action('woocommerce_email_before_order_table','add_order_email_instructions',10);
    

    This is unfinished, now I have to show it in the adequate array format within the mail

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。