Get product information in woocommerce (Product name, type, id, price, sku etc..)

In day to day development tasks we need specific product information to do some operations on it or else we have to show this information somewhere. But every time it take much time to find its functions and utilize it in our code. So this post is for you guys, if you have product id or object id then you can use below function list to get information according to it.

For example you can get product name, sku, description, price, quantity, downloads etc. Please go through below list of functions.

Product general information functions

$product = wc_get_product( $product_id );

$product->get_id();
$product->get_name();
$product->get_slug();
$product->get_type();
$product->get_sku();
get_permalink( $product->get_id() );
$product->get_featured();
$product->get_status();
$product->get_description();
$product->get_short_description();
$product->get_virtual();
$product->get_menu_order();
$product->get_catalog_visibility();
$product->get_date_created();
$product->get_date_modified();

Product price related functions

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

Product stock and shipping related functions

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

Product link products and dimentions related functions

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

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

Product downloads, taxonomy and variations related functions

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

$product->get_category_ids();
$product->get_categories();
$product->get_tag_ids();

$product->get_default_attributes();
$product->get_attributes();

Product reviews and images related functions

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

get_the_post_thumbnail_url( $product->get_id(), 'full' );
$product->get_gallery_image_ids();
$product->get_image_id();

Source : docs.woocommerce, businessbloomer.com

How to use this functions :

To use this function you should have product id or order id or cart id. Using this id you can make object of product and then you can use above functions to perform operations as you want. Check below code to access these functions in a proper way.

// pass product id to wc_get_product() function to make product object
$product = wc_get_product( $product_id );

// by this $product object you can use its functions
$product->get_sku();
$product->get_price();
$product->get_stock_quantity();
$product->get_height();
$product->get_categories();
$product->get_review_count();
//... and so on...

In order and cart it has many products collection will be there. so to get each product information we have to loop through access information of product. So we have to use WC_Order() to make object of order to get products first. Check below code :

// make order object first from order id
$order_object = new WC_Order( $order_id );

// get products from order object
$order_items = $order_object->get_items();

// loop through access order items and make product object to acess its functions
foreach ( $order_items as $order_item) {
 
    $product = wc_get_product( $order_item['product_id'] );
 
    // here you can get all information of each product
 
    $product->get_sku(); 
    $product->get_price(); 
    $product->get_stock_quantity(); 
    $product->get_height(); 
    $product->get_categories(); 
    $product->get_review_count();
    //... and so on...
 
}

In a same way of order object we can get products from cart object. But this time we will get products from wc()->cart. Check below code :

// make cart object first from current cart data
 
$cart_data = WC()->cart->get_cart();
 
foreach( $cart_data as $cart_item ){
 
    $product = wc_get_product( $cart_item['product_id'] );
 
    // here you can get all information of each product
 
    $product->get_sku(); 
    $product->get_price(); 
    $product->get_stock_quantity(); 
    $product->get_height(); 
    $product->get_categories(); 
    $product->get_review_count();
    //... and so on...
 
}

You can use this functions in your parent or child theme functions.php and other templates also. you can also use it in your wordpress plugin.

Conclusion :

As you can see, these functions are very help full to make customization in woo-commerce related functionality. It saves my time a lot to get all functions at one place and i hope it will save your time also. To make this article i followed some sources like docs.woocommerce, businessbloomer.com. You can also check this links to get more information related to woocommerce. I hope this information is useful to you. In future post we will see some more articles and features provided by woocommerce that saves our time. So stay connected with my blog to get useful stuff in simple way, and i will be more happy to get some feedback from you in comment section, and also share this post if you like and if it is useful for others. Thanks. 🙂

Leave a Reply

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