phpwoocommerceorderswoocommerce-subscriptions

How to check if current order is a switch order or a parent order in a subscription?


I have a few lines of code that look through an order and perform an action IF the order includes a particular subscription product (id 12496) and the total is more than 0.

function get_product_data( $customer_data, $order ) {

    foreach ( $order->get_items() as $item ) {

        if ( 12496 === $item->get_product_id() && ($order->get_total() > '0') ) {
            $customer_data['journey_sub_variation'] = $item['variation_id'];
        }
    }

    return $customer_data;

}
add_filter( 'wpf_woocommerce_customer_data', 'get_product_data', 10, 2 );

This all works ok, but it's not exactly what I wanted. The reason I added the " > '0' " condition is because I want the script to ignore orders that are "Switch Orders". But not all nill orders are necessarily switch orders. Parent orders in our subs are often 0 (we use free trials) so it's not the correct solution.

What I would really like to do is perform the action IF the order includes that subscription product (id 12496) and it is not a Switch order. Alternatively, we could check if the order in question is a parent order and so perform the action even if the total is 0.

Hope that makes sense.

I have tried playing with it for a few hours now, but can't get anything to work.

Any ideas?


Solution

  • LoicTheAztec's code did not work for me but was of great help in getting to a working solution. Here it is:

    function get_product_data( $customer_data, $order ) {
    
        foreach ( $order->get_items() as $item ) {
    
            if ( 12496 === $item->get_product_id() && (($order->get_total() > '0') || wcs_order_contains_subscription( $order, 'parent' )) ) {
                $customer_data['journey_sub_variation'] = $item['variation_id'];
            }
        }
    
        return $customer_data;
    
    }
    add_filter( 'wpf_woocommerce_customer_data', 'get_product_data', 10, 2 );
    

    Thanks again LoicTheAztec