Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce - How to check if an order has physical products?

I need to determine if the order is an order that needs shipping of physical goods.

So I can check if the order has physical goods in it or if the order has only digital/virtual products.

How can I do this in the best way?

I get all the items of the order with:

$order = new WC_Order($order_id);

$items = $order->get_items();
like image 213
Torben Avatar asked Oct 27 '25 11:10

Torben


1 Answers

You could do something like this. You need to loop through the order items, and get the product ids to use the method WC_Product::is_virtual();


$order = new WC_Order($order_id);

foreach ($order->get_items() as $order_item){
    $item = wc_get_product($order_item->get_product_id());
    if (!$item->is_virtual()) {
        // this order contains a physical product do what you want here or return false
    }
}
like image 129
Howard E Avatar answered Oct 29 '25 07:10

Howard E



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!