WooCommerce - Get Product Info using product Object

WooCommerce: Get Product Info using $product Object

I’ve been working to WoCoomerce for a while. As a WooCommerce developer, everyday, I have to spend a lot of time exploring and find how to get product information once I have a product ID. I mostly search for following thing on Google.

  • How to get product SKU?
  • How to Get product Short Description?
  • How to get stock?
  • How to get Sale price? and So On…

My primary purpose to write this article save hours while we can spend time on coding and learning other things. We have following situations.

1. If you have Product ID

<?php

//If you have the producr ID, use wc_get_product to get $product Object
  
$product = wc_get_product( $product_id );
  
// Once you have access to $product variable

$product->get_id();
$product->get_name();
$product->get_type();
$product->get_status();
$product->get_sku();

// Get Pricing Information

$product->get_price();
$product->get_regular_price();
$product->get_sale_price();
$product->get_date_on_sale_from();
$product->get_date_on_sale_to();
$product->get_total_sales();

// Get Tax, Shipping & Stock Information

$product->get_tax_status();
$product->get_tax_class();
$product->get_manage_stock();
$product->get_stock_quantity();
$product->get_stock_status();
$product->get_backorders();
$product->get_sold_individually();
$product->get_purchase_note();
$product->get_shipping_class_id();

// Get Dimensions Information

$product->get_weight();
$product->get_length();
$product->get_width();
$product->get_height();
$product->get_dimensions();

// Get Linked Product Information

$product->get_upsell_ids();
$product->get_cross_sell_ids();
$product->get_parent_id();

// Get Variations and Attributes Information

$product->get_children(); // get variations
$product->get_attributes();
$product->get_default_attributes();
$product->get_attribute('attributeid'); // get specific attribute value

// Get Taxonomy Information

wc_get_product_category_list($product_id, $sep = ', ');
$product->get_category_ids();
$product->get_tag_ids();

// Get Download Information

$product->get_downloads();
$product->get_download_expiry();
$product->get_downloadable();
$product->get_download_limit();

// Get Image Information

$product->get_image_id();
$product->get_image();
$product->get_gallery_image_ids();

// Get Review Information

$product->get_reviews_allowed();
$product->get_rating_counts();
$product->get_average_rating();
$product->get_review_count();


PHP

2. If you have access to global $product variable

If you have access to the global $product variable in WooCommerce environment, you gain direct access to above all listed methods of methods that provide detailed information about the product.

<?php 

$product;

// Once you have access to $product variable

$product->get_id();
$product->get_name();
$product->get_type();
$product->get_status();
$product->get_sku();

// Get Pricing Information

$product->get_price();
$product->get_regular_price();
$product->get_sale_price();
$product->get_date_on_sale_from();
$product->get_date_on_sale_to();
$product->get_total_sales();

// Get Tax, Shipping & Stock Information

$product->get_tax_status();
$product->get_tax_class();
$product->get_manage_stock();
$product->get_stock_quantity();
$product->get_stock_status();
$product->get_backorders();
$product->get_sold_individually();
$product->get_purchase_note();
$product->get_shipping_class_id();

// Get Dimensions Information

$product->get_weight();
$product->get_length();
$product->get_width();
$product->get_height();
$product->get_dimensions();

// Get Linked Product Information

$product->get_upsell_ids();
$product->get_cross_sell_ids();
$product->get_parent_id();

// Get Variations and Attributes Information

$product->get_children(); // get variations
$product->get_attributes();
$product->get_default_attributes();
$product->get_attribute('attributeid'); // get specific attribute value

// Get Taxonomy Information

wc_get_product_category_list($product_id, $sep = ', ');
$product->get_category_ids();
$product->get_tag_ids();

// Get Download Information

$product->get_downloads();
$product->get_download_expiry();
$product->get_downloadable();
$product->get_download_limit();

// Get Image Information

$product->get_image_id();
$product->get_image();
$product->get_gallery_image_ids();

// Get Review Information

$product->get_reviews_allowed();
$product->get_rating_counts();
$product->get_average_rating();
$product->get_review_count();
PHP

3. You have access to the Cart object

If you have access to the Cart object in a WooCommerce environment, you gain the ability to interact with and modify the shopping cart contents. The Cart object provides methods to add, remove, and update items in the cart.

<?php
// How to get $product object from Cart object
  
$cart = WC()->cart->get_cart();
  
foreach( $cart as $cart_item_key => $cart_item ){
  
    $product = $cart_item['data'];
  
    // Now you have access to $product Object and you can use above all methods
  
    $product->get_type();
    $product->get_name();
   
   // and so on
  
}
PHP

4. If you have access to Order Object or Order ID

If you have access to the Order object or the Order ID in a WooCommerce environment, you can utilize a set of methods to retrieve and manipulate crucial information related to the specific order. These methods include accessing fundamental order details such as the order ID, status, and date. You can also retrieve billing and shipping information, the list of items in the order, payment details, and various other order-related data.

<?php

// How to get $product object from $order / $order_id

$order = wc_get_order( $order_id );
$items = $order->get_items();
  
foreach ( $items as $item ) {
    
    $product = $item->get_product();
  
    // Now you have access to $product Object and you can use above all methods
  
    $product->get_type();
    $product->get_name();
     
     // and so on
}
PHP

5. You have access to $post object

If you have access to the $post object in a WordPress environment, you can manipulate and retrieve information related to a specific post. The $post object encapsulates details such as the post ID, title, content, author, date, and other meta-information.

<?php
// How to G=get $product object from $post object
  
$product = wc_get_product( $post );
  
// Now you have access to $product Object and you can use above all methods
  
$product->get_type();
$product->get_name();

// and so on
PHP

Understanding and utilizing these objects and their associated methods are key aspects of effective development within the WordPress and WooCommerce ecosystems.