How to display Woocommerce product price by ID number on a custom page?



How to Display Woocommerce Product Price by ID Number on a Custom Page
Are you looking to display the price of a specific product on a custom page in Woocommerce? Look no further! In this blog post, we'll explore easy solutions to this common problem.
The Dilemma
Recently, a reader reached out to us with a question. They wanted to display the price of a product in Woocommerce on a custom page. However, the existing shortcode not only displayed the price but also added an "Add to Cart" button. The reader only wanted the price, no buttons.
The Solution
Fortunately, it is possible to achieve this desired result. Here's how you can display the price of a specific product by its ID number on a custom page:
First, you will need the ID number of the product you want to display the price for. You can find this ID number by navigating to the "Products" section in your Woocommerce dashboard and hovering over the desired product. Look at the URL in your browser's address bar, and you should see a number following "post=" - this is the ID number.
Once you have the ID number, open the custom page where you want to display the price and add the following code snippet:
<?php
$product_id = YOUR_PRODUCT_ID;
$product = wc_get_product( $product_id );
$price = $product->get_price();
echo "Price: $" . $price;
?>
Replace
YOUR_PRODUCT_ID
with the actual ID number of the product you want to display. This code will retrieve the price of the specified product and store it in the$price
variable.Afterwards, the price will be displayed on the custom page using the
echo
statement. Feel free to customize the output format to better suit your needs.
That's it! You have successfully displayed the price of a specific product on your custom page, without any unwanted buttons.
Have Fun Customizing!
Now that you know how to display the price of a Woocommerce product by its ID number, the possibilities for customization are endless! You can incorporate this feature into your website's design, create product comparison tables, or even use it for dynamic pricing.
If you have any other questions or need further assistance, don't hesitate to reach out. We're always here to help!
Happy customizing! 💪😎🎉
Was this blog post helpful? Did you encounter any issues or have additional questions? Comment below and let's continue the conversation!