【问题标题】:How to generate woocommerce coupon code dynamically using programming code如何使用编程代码动态生成 woocommerce 优惠券代码
【发布时间】:2018-03-24 06:18:36
【问题描述】:

我想动态生成 woocommerce 优惠券代码。

我的要求是在完成订单后自动在管理员端 woocommerce 优惠券代码列表中为特定产品生成一个优惠券代码。

所以任何人都知道我的上述需求解决方案然后请帮助我。

谢谢, 凯坦。

【问题讨论】:

    标签: wordpress plugins woocommerce orders coupon


    【解决方案1】:

    您可以使用woocommerce_order_status_completed 操作挂钩来完成订单。并使用wp_insert_post为coupan创建帖子类型为shop_coupon的帖子。检查下面的代码

    function action_woocommerce_order_status_completed( $order_id ) { 
    
       $order = wc_get_order( $order_id );
    
        $order_items = $order->get_items();
        // Iterating through each item in the order
        $item_quantity=0;
        foreach ($order_items as $item_id => $item_data) {
    
            $item_quantity=$order->get_item_meta($item_id, '_qty', true);
            if($item_quantity>1){
                $product_ids[]=$item_data['product_id'];
                $coupon_code = 'UNIQUECODE'.$order_id.$item_id; // 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',$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' );
                unset($product_ids);
            }
    
        }
    
    
    
    }; 
    // add the action 
    add_action( 'woocommerce_order_status_completed', 'action_woocommerce_order_status_completed', 10, 1 );
    

    【讨论】:

    • 感谢 Ankur,您的重播。我会尝试你的解决方案并更新你。谢谢。
    • 嗨 Ankur,在我的 functions.php 文件中添加上述代码后,站点无法正常工作,前端和后端都显示空白屏幕。所以请您查看您的上述代码并在此处更新我。谢谢。
    • $coupon_code = 'UNIQUECODE'$order_id;这一行的错误。请在 UNIQUECODE 和 $order_id 之间添加.。请检查编辑后的答案
    • 谢谢 Ankur,我这样做了,优惠券代码生成成功。再想一想如果客户购买了多个数量的产品,我们如何只允许生成该优惠券代码?谢谢。
    • @KetanPatel 立即查看答案
    【解决方案2】:

    编写一个函数,当使用woocommerce_order_status_completedwoocommerce_order_status_processing 钩子完成或下订单时将触发该函数。在函数内部,使用wp_insert_post 创建一个shop_coupon 类型的帖子。使用 wp_insert_post,您可以指定优惠券的标题、金额等。

    【讨论】:

    • 感谢 Neil k,您的重播。我会尝试你的解决方案并更新你。谢谢。
    猜你喜欢
    • 2018-07-03
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 2013-04-22
    相关资源
    最近更新 更多