Get product details from $product object WooCommerce

Get product details from $product object WooCommerce

How do I obtain the WooCommerce product ID? How can I get the product’s SKU? Or how do you find out the regular price, sale price, stock, shipping class, tax class, images, dimensions, attributes, categories, and other product information?

To perform various store operations, such as determining which products were present in order, applying advanced product filters, editing prices and descriptions, and so on, you must obtain the product ID and other details.

When we have a $product object

Hooks (which take action and apply filters) make use of extra arguments given to the method. It’s fantastic if they let you utilize the “$product” object. You may also specify the “global $product” inside your function.

This is how you acquire all of the product information in both cases:

  /* For Get Product ID */

  /* For Get Product General Information */
  get_permalink( $product->get_id() );

  /* For Get Product Prices */

  /* For Get Product Taxs, Shipping Info & Stock */

  /* For Get Product Dimensions */

  /* For Get Linked Products */

  /* For Get Product Variations and Attributes */
  $product->get_children(); // get product variations
  $product->get_attribute( 'attributeid' ); //get specific attribute value

  /* For Get Product Taxonomies */

  /* For Get Product Downloads */

  /* For Get Product Images */

  /* For Get Product Reviews */

Get $product object by $product_id

If you have the product ID, you can access the product object with the following WooCommerce function:

  $product = wc_get_product( $product_id );

Get $order object by $order_id

  $order = wc_get_order( $order_id );
  $items = $order->get_items();

  foreach ( $items as $item ) {
      $product = wc_get_product( $item['product_id'] );
      /* Now you can get all information from $product object. */

  • The code starts by getting the order with an ID of “123”.
  •  Then it loops through all the items in that order.
  •  The loop starts by calling wc_get_order() which returns a WCOrder object.
  •  This is then passed to get_items(), which returns an array of WCItem objects, each representing an item in the order.
  •  The code then iterates over these items and gets their product information from $product->get_name().
  •  The code will iterate through all the products in the order and print out their names.

Get products from $cart object

How do I get the product information from within the cart? In this case, you will need to loop through all of the items in the cart and then apply the above rules.

  $cart = WC()->cart->get_cart();

  foreach( $cart as $cart_item ){

      $product = wc_get_product( $cart_item['product_id'] );

      /* Now you can get all information from $product object. */



If you enjoy coding and are familiar with hooks and filters, you can use the coding approach to obtain product information as needed.

How useful was this blog?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this blog.

Leave a comment

Your email address will not be published. Required fields are marked *