Question

I'm creating a plugin for WooCommerce to modify the Checkout page. I would like to do some conditional filters based on the users shopping cart. Is there a way to look at the items in the shopping cart, and determine if any of the items are NOT downloadable/virtual?

So far I have a filter and in the filter I want to hide the address fields. This works great, but now I want to make it conditional, and only hide the address fields if a non-downloadable product is included. Thanks

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
    $hasPhysicalProduct = 0;
    if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];
            if ( SOMEHOW CHECK IF THE PRODUCT IS DOWNLOADABLE ) {
                $hasPhysicalProduct = 1;
            }
        }
    }
Was it helpful?

Solution

$_product is an instance of WC_Product, so you should be able to do this:

if ( $_product.is_downloadable() ) {
     $hasPhysicalProduct = 1;
}

The WooCommerce docs has information about the WC_Product class members

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top