繁体   English   中英

如何使用编程代码动态生成woocommerce优惠券代码

[英]How to generate woocommerce coupon code dynamically using programming code

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

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

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

谢谢,凯坦。

您可以使用woocommerce_order_status_completed操作挂钩来完成订单。 并建立与后支柱式shop_coupon使用coupan wp_insert_post 检查下面的代码

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM