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?
2
Answers
LoicTheAztec's code did not work for me but was of great help in getting to a working solution. Here it is:
Thanks again LoicTheAztec
To check if a given order was used to switch a subscription via checkout, you can use
wcs_order_contains_subscription()
conditional function like:It should work.
The 2nd argument from
wcs_order_contains_subscription()
conditional function is the order type (array|string) (optional). It can include ‘parent’, ‘renewal’, ‘resubscribe’ and/or ‘switch’. Defaults value is set to ‘parent’.Addition:
To check if an order is the main order (not a child order), you can use
WC_Order
get_parent_id()
method like: