I have been trying to find a way to add a different suffix to both my retail prices and sales prices. I am going to be selling stone and I want my product page to display the following…..
$12.99 /SQFT
$9.99 /SQFT with 315 sqft (pallet) minimum purchase
When I add PHP code to my functions.php file, it displays
$0.00 /SQFT
$0.00 /SQFT with 315 sqft (pallet) minimum purchase
I have tried numerous different plugins but have not found one that will allow me to display this the way I want. The PHP code that I tried to add to my functions.php page is the following….
add_filter( 'woocommerce_get_price_html', 'custom_price_suffixes', 100, 2 );
function custom_price_suffixes( $price, $product ) {
if ( $product->is_on_sale() ) {
$sale_price = wc_price( $product->get_sale_price() ) . ' /SQFT with 315 sqft (pallet) minimum purchase';
$regular_price = wc_price( $product->get_regular_price() ) . ' /SQFT';
$price = '<del>' . $regular_price . '</del> <ins>' . $sale_price . '</ins>';
} else {
$price = wc_price( $product->get_price() ) . ' /sqft';
}
return $price;
}
That results in the $0.00. I have found through my testing that when the get_sale_price and get_regular_price are called, they display $0.00. I have tried just the get_price hook and it pulls the price. For some reason it is not pulling my pricing. My products have variants, nots sure if that is the problem? I would think there would be a way to add different suffix’s to each of the prices though.
2
Answers
I made a few minor corrections to the code, including adding spaces around the concatenation operators (.) for better readability.
There are some mistakes and missing cases with your code. The following will handle your custom price suffixes for all product types and cases:
Code goes in functions.php file of your child theme (or in a plugin). It should better work.