skip to Main Content

I am looking for a documentation regarding setting a coupon’s used_by particular customer user_id on Woocommerce if coupon code to be added manually via REST API or from the Admin create new order endpoint. But I find no way to do it correctly. Hoping someone could point me out. Below is my code developed so far but it returned NULL.

$WC_Coupon = new WC_Coupon($request['code']);
$WC_Coupon->set_used_by( $request['customer_id'] );

2

Answers


  1. Chosen as BEST ANSWER

    Found the answer...The right function to use is increase_usage_count instead of directly using the Coupon object to set the used_by meta key.

    $WC_Coupon = new WC_Coupon($request['code']);
    $used_by = $request['customer_id'];
    $WC_Coupon->increase_usage_count( $used_by );
    

    Hope this snippet helps whoever that creates order via REST API and in need of a method to track who has consumed the coupon code.


  2. Are you sure that your $WC_Coupon is instantiated properly? No matter how a coupon was added into the system, once it is there, it should act like any other coupon.

    Would suggest you to take a var_dump of $WC_Coupon and see if it outputs a proper value otherwise, you might not be providing the constructor with a proper code parameter.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search