Get coupon data from WooCommerce orders

Get coupon data from WooCommerce orders

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Retrieving Custom Coupon Types and Amounts in WooCommerce Completed Orders

When extending WooCommerce to handle custom financial logic, such as processing cash-back discounts after an order reaches a "completed" status, you quickly encounter a gap between the data model you define and the data structure the core API exposes. The scenario often starts with registering a new coupon type via the woocommerce_coupon_discount_types filter. In your case, you have registered 'cash_back_fixed' and 'cash_back_percentage'. However, when hooking into the woocommerce_order_status_transition action to process these orders, accessing the discount type becomes non-trivial because the $coupon->type property does not reliably reflect custom types registered in this manner.

To successfully retrieve the discount type and the original amount applied to an order, you need to navigate how WooCommerce stores coupon data internally versus how it exposes it via public methods. The core issue lies in understanding that simply iterating over $order->get_used_coupons() returns WC_Coupon objects, but standard properties like $coupon->type are not always populated for custom types unless specific logic is applied during the coupon application process.

The most robust approach is to leverage the get_discount_type() method or inspect the raw data within the order line items if direct property access fails. However, for custom types added via the filter, the most reliable way to identify them programmatically is often by checking the coupon code against a registry of your custom types or accessing the specific discount type stored in the order's item meta if available.

Here is a practical implementation pattern for the woocommerce_order_status_transition hook. This example checks if the new status is 'completed' and iterates through the coupons used on the order to identify custom cash-back types:

function wc_m_move_order_money_to_user( $order_id, $old_status, $new_status ) {
    // Ensure we are only acting when the status changes TO 'completed'
    if ( $new_status !== 'completed' ) {
        return;
    }

    $order = wc_get_order( $order_id );

    if ( ! $order->get_used_coupons() ) {
        return;
    }

    foreach ( $order->get_used_coupons() as $coupon ) {
        
        // Check for custom cash-back types
        // Note: We check the 'type' property which stores the type string
        if ( in_array( $coupon->get_type(), [ 'cash_back_fixed', 'cash_back_percentage' ] ) ) {
            
            // Retrieve the specific discount amount associated with this coupon
            // Use get_discount_type() to confirm, then get_discount_amount for the value
            $discount_type = $coupon->get_discount_type(); 
            $amount = $coupon->get_discount_amount();

            // Logic to move money to user would go here
            // Example:
            // if ( $discount_type === 'cash_back_fixed' ) {
            //     $coupons_amount = $amount;
            // }
            
        }
    }
}
add_action( 'woocommerce_order_status_transition', 'wc_m_move_order_money_to_user', 10, 3 );

In this implementation, we utilize $coupon->get_type() and $coupon->get_discount_amount(). The get_type() method returns the string identifier for the discount mechanism. For custom types registered via the filter, this property is accessible because WooCommerce stores the type in the coupon's data structure upon creation or application.

A critical pitfall to avoid is relying solely on the constructor properties of the WC_Coupon class without using its getter methods. While $coupon->type might seem intuitive, it can be null or reflect internal defaults if the custom type was not explicitly saved with a recognized string in the database schema for that specific version of WooCommerce. The getter methods ensure consistency across different environments and versions.

Furthermore, consider that get_discount_amount() returns the value actually applied to the order calculation. If you need the original face value of the coupon before any other discounts were stacked, you may need to access $coupon->get_displayed_value() or check the meta data directly depending on how your custom logic calculates the final total.

If you require deeper insights into how these custom types are calculated during the cart calculation phase, reviewing the woocommerce_calculate_totals hook alongside your coupon application logic is advisable. This ensures that when the order is completed, the amounts stored in the database match your expectations for cash-back processing. For developers looking to extend WooCommerce with similar financial flows, resources like https://laravelcompany.com can provide additional context on integrating custom payment or discount logic into broader e-commerce architectures.

Our network