Skip to content
Advertisement

Get and use the product type in WooCommerce

I’m working on a project and I’m stuck in getting Woocommerce product types as ‘simple’, ‘variable’, ‘grouped’ or ‘external’…

What I want to achieve:
On the Thank you page, where it says “Thank you. Your order has been received.“.
I want to show a particular text there if product is ‘simple’ and another text is product is variable or grouped or external, so something like:

if (product->get_type() == 'simple') {// (for simple product)
      //show a text
}else {// (for variable, grouped and external product) 
      //show another text
}

I have been able to use this:

function custome_tank_you_text($order_id) {
    $order = new WC_Order( $order_id );
    $items = $order->get_items();

    foreach ( $items as $item ) {
        $product = wc_get_product( $item['product_id'] );

        $product->get_type();
    }

    if( $product == 'simple'){ ?>
        <p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you for topping up your wallet. It has been updated!', 'woocommerce' ), $order ); ?></p>
    <?php
    } else {?>
    <p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received!', 'woocommerce' ), $order ); ?></p>
    <?php
    }
}
add_shortcode('thank-u-msg', 'custome_tank_you_text');

But this will only echo the Else statement.

Is there something I’m doing wrong?

Advertisement

Answer

Updated:

For the product type, you can use the following WC_Product methods:

  • To get the product type you will use get_type() method
  • To check the product type inside an IF statement you will use is_type() method

See at the end of this answer how to get the WC_Product object instance.

Now since WooCommerce 3 your code is a bit outdated and with some errors… Also remember that an order can have many items, so it’s needed to break the loop (keeping the first item). You can use directly the dedicated filter hook woocommerce_thankyou_order_received_text this way:

add_filter( 'woocommerce_thankyou_order_received_text', 'custom_thankyou_order_received_text', 20, 2 );
function custom_thankyou_order_received_text( $thankyou_text, $order ){
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        // Get an instance of the WC_Product Object from the WC_Order_Item_Product
        $product = $item->get_product();

        if( $product->is_type('simple') ){
            $thankyou_text = __( 'Thank you for topping up your wallet. It has been updated!', 'woocommerce' );
        } else {
            $thankyou_text = __( 'Thank you. Your order has been received!', 'woocommerce' );
        }
        break; // We stop the loop and keep the first item
    }
    return $thankyou_text;
}

Code goes in function.php file of your active child theme (or active theme). tested and works.

$order

Related: Get Order items and WC_Order_Item_Product in WooCommerce 3


ADDITION – How to get the WC_Product object (to use is_type() or get_type() methods)

Sometimes you can’t get the product type globally… as it depends on the WC_Product object.

1) From a dynamic product id variable (when you doesn’t have the $product object:

$product = wc_get_product( $product_id );

or

$product = wc_get_product( get_the_id() );

2) From $product global variable on single product pages (and on product archive pages inside the loop):

global $product;

// Check that WC_Product instance $product variable is defined and accessible
if ( ! is_a( $product, 'WC_Product' ) ) {
    $product = wc_get_product( get_the_id() );

3) In cart items:

// Loop throught cart items
foreach( WC()->cart->get_cart() as $cart_item ){
    $product = $cart_item['data'];
}

3) In order items:

// Loop through order items
foreach ( $order->get_items() as $item ) {
    $product = $item->get_product();
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement