Get woocommerce product price by id

i make one shortcode for get product title, image and price. i am getting all title, link and image properly but not getting price. but problem is

add_shortcode('product_data','custom_product_function');
function custom_product_function($atts)
{
    $post_id = $atts['id'];
    $title = get_the_title($post_id);
    $link = get_the_permalink($post_id);
    $price = get_the_price($post_id);
    $image = get_the_post_thumbnail($post_id, 'thumbnail');
    $data ='<div class="releated-products wow fadeInUp"><a href="'.$link.'">'.$image.'<h5>'.$title.'</h5><h6>'.$price.'</h6></a></div>';
    return $data;
}

$price = get_the_price($post_id); i guess this function not correct

any idea how to get price now.

Thanks you

Solutions

You can create a product object using the following function:

$product = wc_get_product( $post_id );

And after that you will be able to access to all product's data. All available methods can be found here, but the ones you need are:

$product->get_regular_price();
$product->get_sale_price();
$product->get_price();

Similar questions

How can I add below of Woocommerce out of stock product text the Woocommerce product price on a single product
I have a single woocommerce product page where only availability "Out of Stock" message is shown. How can I show a formatted price with currency below that text? I think I need to insert the following but I dont know how This is happening to all simple products. As settings, stock management is disabled. I don't know why price is not displayed. I t...
Variable product price not showing in listing and details page WooCommerce Price Based on Country
When the product is listed in Product listing page or Product details page, The price not showing.When I click on any variation like color,then only the product price is displayed.If I deactivate the plug-in its all works good. Note: I am using All the products are variable products(color). I surfed on net for this and found something like this *Wo...
WooCommerce product price visibility: add price back only for a specific category
The website I am working on requires you to be logged in to see prices and I've been using a plugin to do this. However, I was just thrown a curve ball and told that one specific category on the website must have prices shown all the time, regardless if the user is logged in or not. It looks like the plugin uses and to remove the prices. And this i...
If sale price is equal to zero show only regular product price in Woocommerce
I run my woocommerce product updates via xml feed upload. However when a product is not on sale the sale price is set to 0 and some blank. I want a situation whereby when a product has a sale price of 0 it should only show regular price. I did get it to work with this code However woocommerce then just displays this below, instead of just regular p...
Product price suffix with a price calculation based on weight in WooCommerce
I am using "https://stackoverflow.com/questions/53435276/custom-product-price-suffix-on-based-on-product-categories-in-woocommerce/53438506#53438506" answer thread that allow to adjust my pricing display with a prefix this way: But what I would actually like is to make the $price output what the price of the item is divided by the weight ...
WooCommerce - Show Regular and Sale Price for The Product Variation which has Flat Price
The scenario is Simple product, Variable product are woocommerce's two different product types. What I am trying to achieve is, there are some products which have over 50+ variations which have a different sale and regular prices. A price range for that product is fare. But there are also products which's variations have same regular and sale price...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.