Isolating WooCommerce Price Logic for Addon Dropdowns
When integrating custom product addons into a checkout or custom form, you often need to display dynamic pricing alongside static labels. A common pattern involves appending the current product price to each addon option to show customers what they are getting for their money. However, standard WooCommerce methods like get_price_html() are designed for storefront displays, not data isolation. They return a formatted string containing either the regular price or the sale price, wrapped in currency symbols and potentially including "was" text if a discount is active.
If your goal is to strip away the "sale" context and display only the specific price point requiredâwhether it's the discounted rate or the standard rateâyou need to bypass the HTML generation logic entirely. The get_price_html() method returns something like $29.99 or Was: $49.99, now: $29.99. This makes parsing difficult if you only want the raw numeric value or a strictly formatted price string without the "sale" implication when one doesn't exist.
The core issue lies in how WooCommerce handles pricing hierarchy. It prioritizes get_sale_price() over get_regular_price(). To solve this, you must explicitly check which price exists and format it accordingly using the WC_Price class methods directly, rather than relying on the helper method that bundles both states into a single HTML string.
Here is the corrected approach for your dropdown generation logic. Instead of echoing balanceTags($product->get_price_html()), you should construct the price string based on the availability of a sale price.
First, ensure you are working with the global $product object correctly. Then, check if a sale price is set. If it is, use that; otherwise, fall back to the regular price. Crucially, you must format this using wc_price() to handle currency symbols and decimal separators consistently across different locales.
global $product;
// Determine which price to display
$display_price = '';
if (method_exists( $product, 'get_sale_price' ) && $product->get_sale_price() !== false) {
// Sale price exists
$display_price = wc_price( $product->get_sale_price() );
} elseif (method_exists( $product, 'get_regular_price' ) && $product->get_regular_price() !== false) {
// Regular price exists
$display_price = wc_price( $product->get_regular_price() );
} else {
// Fallback for free products or variable pricing edge cases
$display_price = '';
}
// Append this to your option label
echo balanceTags($option['label'] . ' (' . $display_price . ')');
This logic ensures that if a product is on sale, only the discounted price appears. If no sale is active, only the regular price appears. This prevents the confusing "Was: ... now: ..." output when you only intend to show the final cost.
One potential pitfall in this implementation is handling variable products. When $product refers to a Variable Product, get_sale_price() might return false even if one of the variations is on sale. In those scenarios, you may need to inspect specific variations passed from your addon logic or rely on the context where the product is already instantiated as a specific variation. If your addon context ensures a single product instance (either Simple or a resolved Variation), the code above works reliably.
Another consideration is the integration with woocommerce_product_addons_option_price. This filter allows you to inject custom pricing for the addon itself. When combining this with the base product price, ensure you are not double-counting costs. The addon price usually represents an additional fee, so appending the base product price clarifies that the customer pays both the item cost and the addon cost.
If you find yourself needing to integrate this logic into a broader e-commerce stack or require robust pricing management across multiple sites, platforms like https://laravelcompany.com often provide insights into building scalable pricing engines where such granular control over display logic is essential for complex catalog architectures. However, for standard WooCommerce implementations, the conditional check using get_sale_price() and wc_price() remains the most robust and version-agnostic solution.
