有没有一种方法可以基于一定数量生成许多Woocommerce优惠券?

时间:2019-07-04 22:18:23

标签: php wordpress woocommerce webhooks

我正在修复一个插件加载的Woocommerce网站,现在我需要根据购买金额实现自动优惠券生成器:例如,如果客户购买了$ 100,我想建立一个规则$ 450,将产生4张$ 40的优惠券。所有这些代码都应发送给客户。

我已经阅读了WooCommerce文档,可以在其中创建优惠券:https://docs.woocommerce.com/document/create-a-coupon-programatically/,并且已经实现了该优惠券并发送了订单审核邮件(确认付款后):

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

现在这一半有效:它可以验证订单总额是否少于$ 100,并且不会生成优惠券,但是如果订单总额高于200,则即使订单总额为200、300 400等,它也只会给我一张优惠券。甚至无法生成一张以上的优惠券,似乎它只能循环一次并停止)。

谢谢!

1 个答案:

答案 0 :(得分:0)

已经解决,这是代码:

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

这还没有完成,现在我必须在邮件中以适当的数组格式显示它