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.
There are some mistakes and missing cases with your code. The following will handle your custom price suffixes for all product types and cases:
// Handle variable product formatted price range
add_filter( 'woocommerce_format_price_range', 'custom_format_price_range_suffixes', 20, 3 );
function custom_format_price_range_suffixes( $price, $from, $to ) {
$suffix = ' ' . esc_html__('/SQFT');
$suffix2 = $suffix . ' ' . esc_html__('with 315 sqft (pallet) minimum purchase');
$from = ( is_numeric( $from ) ? wc_price( $from ) : $from ) . $suffix;
$to = ( is_numeric( $to ) ? wc_price( $to ) : $to ) . $suffix2;
return sprintf( _x( '%1$s – %2$s', 'Price range: from-to', 'woocommerce' ), $from, $to );
}
// Handle products with a sale price
add_filter( 'woocommerce_format_sale_price', 'custom_format_sale_price_suffixes', 20, 3 );
function custom_format_sale_price_suffixes( $price, $regular_price, $sale_price ) {
$formatted_regular_price = is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price;
$formatted_sale_price = is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price;
$suffix = ' ' . esc_html__('/SQFT');
$suffix2 = $suffix . ' ' . esc_html__('with 315 sqft (pallet) minimum purchase');
// Strikethrough pricing.
$price = '<del aria-hidden="true">' . $formatted_regular_price . $suffix . '</del> ';
// For accessibility (a11y) we'll also display that information to screen readers.
$price .= '<span class="screen-reader-text">';
// translators: %s is a product's regular price.
$price .= esc_html( sprintf( __( 'Original price was: %s.', 'woocommerce' ), wp_strip_all_tags( $formatted_regular_price ) ) );
$price .= $suffix . '</span>';
// Add the sale price.
$price .= '<ins aria-hidden="true">' . $formatted_sale_price . $suffix2 . '</ins>';
// For accessibility (a11y) we'll also display that information to screen readers.
$price .= '<span class="screen-reader-text">';
// translators: %s is a product's current (sale) price.
$price .= esc_html( sprintf( __( 'Current price is: %s.', 'woocommerce' ), wp_strip_all_tags( $formatted_sale_price ) ) );
$price .= $suffix2 . '</span>';
return $price;
}
// Handle price suffix for other cases
add_filter( 'woocommerce_get_price_suffix', 'custom_price_suffix', 20, 4 );
function custom_price_suffix( $suffix, $product, $price, $qty ) {
if ( $product->is_on_sale() ) {
return ''; // Return no suffix as prices are already suffixed
} elseif ( $product->is_type('variable') ) {
$prices = $product->get_variation_prices( true );
$min_price = current( $prices['price'] );
$max_price = end( $prices['price'] );
if ( ! empty($prices['price']) && $min_price !== $max_price ) {
return ''; // Return no suffix as prices are already suffixed
}
}
return ' ' . esc_html__('/SQFT') . ' ' . esc_html__('with 315 sqft (pallet) minimum purchase');
}
Code goes in functions.php file of your child theme (or in a plugin). It should better work.