Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce Get Product Values by ID

Trying to get Product Data on custom Template By product ID, right now i have this code to get Product Title.

$productId = 164;
echo $p_title = get_the_title( $productId );

looking for Short Description, Price, Product Image, Product Url, Product Brand. Or might be loop will be better but that loop should work with product static ID.

Thanks in advance.

like image 423
Justin K Avatar asked Feb 02 '16 22:02

Justin K


People also ask

How do I get product image from product ID in WooCommerce?

“get product image woocommerce” Code Answer's php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $product_id ), 'single-post-thumbnail' );?> <img src="<? php echo $image[0]; ?>" data-id="<? php echo $loop->post->ID; ?>">


1 Answers

You would probably be better served by creating a new product object.

$productId = 164;
$product = wc_get_product( $productId );
echo $product->get_title();
echo $product->get_price_html();

Note, that the short description is merely the post's post_excerpt. If using outside of the loop (where $post is automatically defined) then you would need to get the post directly.

$post = get_post( $productId );

echo apply_filters( 'woocommerce_short_description', $post->post_excerpt );

or alternatively, if you've already defined the product object you could do

echo apply_filters( 'woocommerce_short_description', $product->post->post_excerpt );

since the WooCommerce product class will automatically define the $product->post property with get_post().

apply_filters() means that functions can be attached at this point to modify the content of $product->post->post_content. At a minimum, I know that wpautop() is attached to the woocommerce_short_description filter to create paragraph breaks.

like image 177
helgatheviking Avatar answered Sep 28 '22 22:09

helgatheviking