How to Get Product Price in WooCommerce: A Beginner’s Guide
WooCommerce is a fantastic platform for selling online, but sometimes you need to access specific product information programmatically. One of the most common things you’ll need is the product price. This article will walk you through how to get product price in WooCommerce in a simple and easy-to-understand way, even if you’re new to coding.
We’ll cover different scenarios and provide practical examples to help you get started. Think of this as your friendly guide to unlocking the power of WooCommerce prices!
Why Would You Need to Get the Product Price Programmatically?
Imagine you’re building a custom plugin or theme for your WooCommerce store. You might need to access the product price for various reasons, such as:
- Displaying custom discounts: You want to show a special discounted price based on user roles or purchase history.
- Creating dynamic pricing rules: The price changes based on quantity or other factors.
- Integrating with external systems: You need to send product price information to a third-party accounting or marketing platform.
- Building a custom product display: You want to present the product information in a unique way that’s not available in the default WooCommerce templates.
- `wc_get_product()` function: This is the foundation. It retrieves a `WC_Product` object, which contains all the product’s information, including its price.
- `$product->get_price()` method: This method is used on a `WC_Product` object to get the current price of the product. It’s the most reliable way to get the current price taking into account sales and discounts.
- `$product->get_regular_price()` method: This method retrieves the regular price of the product, before any sales or discounts are applied.
- `$product->get_sale_price()` method: This method returns the sale price of the product, if the product is on sale. If the product is not on sale, it will return an empty string.
Think of it like this: your store sells personalized mugs. You want to offer a 10% discount for customers who have purchased more than 5 mugs in the past. To apply this discount, you need to programmatically access the current product price and calculate the discount amount.
Methods to Get Product Price in WooCommerce
WooCommerce provides several functions to access product prices. Let’s explore the most common and useful ones:
Example: Getting the Current Product Price
Let’s say you have a product ID (e.g., `123`). Here’s how you can get the current price:
<?php
$product_id = 123; // Replace with your actual product ID
$product = wc_get_product( $product_id );
if ( $product ) {
$price = $product->get_price();
echo ‘The current price is: ‘ . wc_price( $price ); // wc_price() formats the price with currency symbol
} else {
echo ‘Product not found.’;
}
?>
Explanation:
1. We define the `$product_id`.
2. We use `wc_get_product()` to retrieve the `WC_Product` object based on the ID.
3. We check if the product exists.
4. If the product exists, we use `$product->get_price()` to get the current price.
5. We use `wc_price()` to format the price with the correct currency symbol and display it.
Real-Life Scenario: Imagine you’re creating a custom shortcode to display product information. You can use this code snippet to fetch and display the current price of a specific product within that shortcode.
Example: Getting the Regular and Sale Prices
Here’s how to get both the regular and sale prices of a product:
<?php
$product_id = 456; // Replace with your actual product ID
$product = wc_get_product( $product_id );
if ( $product ) {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
echo ‘Regular price: ‘ . wc_price( $regular_price ) . ‘
‘;
if ( ! empty( $sale_price ) ) {
echo ‘Sale price: ‘ . wc_price( $sale_price );
} else {
echo ‘Not on sale.’;
}
} else {
echo ‘Product not found.’;
}
?>
Explanation:
1. We retrieve the regular price using `$product->get_regular_price()`.
2. We retrieve the sale price using `$product->get_sale_price()`.
3. We check if the sale price is empty. If it’s not empty, the product is on sale, and we display the sale price. Otherwise, we indicate that the product is not on sale.
Real-Life Scenario: Think of a custom product page where you want to clearly show the original price and the discounted price (if applicable). This code snippet would allow you to display both prices side-by-side, highlighting the savings for the customer.
Important Considerations
- Product Type: The way you get the price might vary slightly depending on the product type (e.g., simple product, variable product). For variable products, you’ll often need to get the selected variation’s price. We’ll cover that in a more advanced tutorial later.
- Using `wc_price()`: Always use the `wc_price()` function to format the price for display. This ensures that the currency symbol and formatting are consistent with your WooCommerce settings.
- Security: When accepting product IDs from user input (e.g., through a form), always sanitize the input to prevent security vulnerabilities. Use functions like `absint()` to ensure the product ID is a positive integer.
- Context: The code examples above assume you have access to a product ID. You might need to fetch the product ID from a loop, a query, or another source depending on your specific needs.
Where to Put This Code?
You can place this code in Learn more about How To Delete Built By Woocommerce On Email various locations, depending on what you’re trying to achieve:
- Your theme’s `functions.php` file: For custom functions that you want to use throughout your theme. Be cautious when editing this file directly. Consider using a child theme.
- Custom plugin: For more complex functionality or if you want to keep your customizations separate from your theme. This is the recommended approach for maintainability.
- WooCommerce template files: For modifying the product display on specific pages (e.g., single product page, shop page). Always override templates in your theme to avoid losing changes during updates.
Conclusion
Getting the product price in WooCommerce is a fundamental skill for customizing your online store. By understanding the `wc_get_product()` function and the `$product->get_price()` method, you can unlock a wide range of possibilities for displaying and manipulating product prices. Remember to use `wc_price()` for proper formatting and consider the context in which you’re accessing the price. With a little practice, you’ll be a WooCommerce price pro in no time!