PHP wc_get_product Examples

Today, We want to share with you wc_get_product.In this post we will show you wc_get_product returns empty object, hear for wc_get_product returns false we will give you demo and example for implement.In this post, we will learn about List of all WordPress WooCommerce hooks with an example.

wc_get_product example

in this example you can get all the products details using wc_get_product methods like as a access to $product_id, Get to the Order object or Order ID and Get $product object from Cart object.

Read Also:  how to generate pdf file in php with demo?

Using wc_get_product() with a PHP variable for product ID

$courseID = the_field('course_id');

$_product = new WC_Product($courseID);

You have access to $product variable

Get Product ID

$product->get_id();

Get Product General Info

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

You have access to $product_id

here you can use Get $product object from product ID

$product = wc_get_product( $product_id );
  
//get products data
$product->get_status();
$product->get_featured();
$product->get_catalog_visibility();
$product->get_description();
$product->get_short_description();
$product->get_sku();
$product->get_type();
$product->get_name();

Get to the Order object or Order ID

here simple Get $product object from $order / $order_id

$order = wc_get_order( $order_id );
$items = $order->get_items();
  
foreach ( $items as $item ) {
  
    $product = wc_get_product( $item['product_id'] );

	  //get products data
	$product->get_status();
	$product->get_featured();
	$product->get_catalog_visibility();
	$product->get_description();
	$product->get_short_description();
	$product->get_sku();
	$product->get_type();
	$product->get_name();

  
}

Get $product object from Cart object

  
$cart = WC()->cart->get_cart();
  
foreach( $cart as $cart_item ){
  
    $product = wc_get_product( $cart_item['product_id'] );
  
	 //get products data
	$product->get_status();
	$product->get_featured();
	$product->get_catalog_visibility();
	$product->get_description();
	$product->get_short_description();
	$product->get_sku();
	$product->get_type();
	$product->get_name();
  
}

I hope you get an idea about wc_get_product.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.