Coupon is never created on WooCommerce payment_complete hook

73 views Asked by At

I'm currently having some issue with a coupon creation in the payment_complete hook.

add_action('woocommerce_payment_complete', 'when_payment_complete'); 
function when_payment_complete( $order_id ) {

    $referral_coupon = new WC_Coupon();
    
    $bytes = random_bytes(8);
    $encoded = base64_encode($bytes);
    $stripped = str_replace(['=', '+', '/'], '', $encoded);
    $stripped = strtoupper($stripped);
    $prefix = strtoupper(substr(md5($user->user_email), 0, 3));
    
    $referral_coupon->set_code( 'CHILO-REF-' . $prefix . $stripped );
    $referral_coupon->set_description( 'TEST REFERRAL COUPON' );
    $referral_coupon->set_discount_type( 'fixed_cart' );
    $referral_coupon->set_amount( 50 );
    $referral_coupon->set_status( "publish" );
    $referral_coupon->set_individual_use(false);
    $referral_coupon->set_usage_limit( 0 );
    $referral_coupon->save();
}

The same block of code is used in a wordpress script and is working perfectly fine.

I don't know shy the coupon is never created

1

There are 1 answers

0
LoicTheAztec On

In your code, $user is not defined and $user->user_email can be replaced with the billing email from the order. So maybe try:

add_action('woocommerce_payment_complete', 'create_referral_coupon_on_payment_complete'); 
function create_referral_coupon_on_payment_complete( $order_id ) {
    // Get an instance of the WC_Order Object
    $order = wc_get_order( $order_id );
    
    // Generate coupon code
    $coupon_code  = 'CHILO-REF-';
    $coupon_code .= strtoupper(str_replace(['=', '+', '/'], '', base64_encode(random_bytes(8))));
    $coupon_code .= strtoupper(substr(md5($order->get_billing_email()), 0, 3));

    // Get a virgin instance of the WC_Coupon object
    $referral_coupon = new WC_Coupon(); 

    $referral_coupon->set_code( $coupon_code );
    $referral_coupon->set_description( 'TEST REFERRAL COUPON' );
    $referral_coupon->set_discount_type( 'fixed_cart' );
    $referral_coupon->set_amount( 50 );
    $referral_coupon->set_status( "publish" );
    $referral_coupon->set_individual_use(false);
    $referral_coupon->set_usage_limit( 0 );
    $referral_coupon->save();

Code goes in functions.php file of your child theme (or in a plugin). It could work.


If it doesn't work, what you can do, is to use an alternative to woocommerce_payment_complete hook with the following, targeting instead paid order statuses and flagging the order when the coupon is created (avoiding multiple coupons creation):

add_action('woocommerce_order_status_completed', 'create_referral_coupon_on_paid_order', 10, 2); 
add_action('woocommerce_order_status_processing', 'create_referral_coupon_on_paid_order', 10, 2); 
function create_referral_coupon_on_paid_order( $order_id, $order ) {
    if( ! $order->get_meta('referral_coupon_created') ) {
        // Generate coupon code
        $coupon_code  = 'CHILO-REF-';
        $coupon_code .= strtoupper(str_replace(['=', '+', '/'], '', base64_encode(random_bytes(8))));
        $coupon_code .= strtoupper(substr(md5($order->get_billing_email()), 0, 3));

        // Get a virgin instance of the WC_Coupon object
        $referral_coupon = new WC_Coupon(); 
        
        $referral_coupon->set_code( $coupon_code );
        $referral_coupon->set_description( 'TEST REFERRAL COUPON' );
        $referral_coupon->set_discount_type( 'fixed_cart' );
        $referral_coupon->set_amount( 50 );
        $referral_coupon->set_status( "publish" );
        $referral_coupon->set_individual_use(false);
        $referral_coupon->set_usage_limit( 0 );
        $referral_coupon->save();

        // Flag order
        $order->add_meta_data('referral_coupon_created', 'yes', true); 
        $order->save();
    }
}

Code goes in functions.php file of your child theme (or in a plugin). It should work.