通过Coupon分组Woocommerce用户

时间:2018-12-18 14:52:24

标签: wordpress woocommerce

希望有些传说可以将我指向正确的方向。

我想对在woocommerce中使用某些优惠券代码的用户进行购买。我意识到有些插件,例如woocommerce组,可以让您将优惠券限制为用户组,但是我需要另一种方式。

例如:某人在购买时使用优惠券代码“板球”,然后会将用户添加到“板球用户”组。

非常感谢V!

1 个答案:

答案 0 :(得分:1)

add_action('woocommerce_thankyou', 'coupon_group', 10, 1);

function coupon_group( $order_id ){
  $order = wc_get_order( $order_id );

    foreach( $order->get_used_coupons() as $coupon_name ){

        // Retrieving the coupon ID
        $coupon_post_obj = get_page_by_title($coupon_name, OBJECT, 'shop_coupon');
        $coupon_id = $coupon_post_obj->ID;


        $coupons_obj = new WC_Coupon($coupon_id);

        if( $coupons_obj->is_type( 'cricket' ) ){

                //get the user id of the customer from the order
                $user_id = $order->get_user_id()
                /*
                *
                * logic that adds the user to the group cricket users
                * i.e. $add_user_to_coupon_group = add_user_meta($user_id, 'custom_user_group', 'cricket_users');
                *
                *
                */
        }


    }
}