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?

0

To check if a given order was used to switch a subscription via checkout, you can use wcs_order_contains_subscription() conditional function like:

function get_product_data( $customer_data, $order ) {
    // Exit if it is a switched subscription order
    if ( wcs_order_contains_subscription( $order, 'switch' ) ) {
        return $customer_data;
    }

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

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:

if ( ! $order->get_parent_id() ) {
    // It is a main root order
} else {
    // It is a child order
}

1

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật