Display comparison set prices for a WooCommerce product

Here's how to change your WooCommerce product prices so that they match the lowest and highest prices found in its Comparison Set.

WooCommerce Product Prices matching Comparison Set pricing
1

Create a custom plugin

If you haven't done so already, create a custom plugin.

2

Add code

Paste in the code below into your custom plugin

<?php
/**
* Rewrites the price for an WooCommerce product if it contains a Comparison Set setting the
* highest price found in the Comparison Set to the "Regular" price of the product and setting
* the lowest sale price found in the Comparison Set to the "Sale" price of the product.
*
* This only works for a product which already has a Comparison Set. It does not
* work if the product has never had a Comparison Set created for it.
*
* @param string $price HTML of formatted price.
* @param WC_Product $product
*
* @return string Updated HTML.
*/
function mycode_change_product_price_to_reflect_prices_in_compset( $price, $product ) {
// Get the compset for this $product.
$source = dfrcs_wc_get_source_of_product( $product );
$source['context'] = 'wc_product_price_element';
$compset = new Dfrcs( $source );
if ( ! $compset->cached ) {
return $price;
}
if ( empty( $compset->lowest_priced_product ) || empty( $compset->highest_priced_product ) ) {
return $price;
}
$regular_price = $compset->highest_priced_product['price'];
$sale_price = $compset->lowest_priced_product['finalprice'];
if ( $sale_price >= $regular_price ) {
return wc_price(
dfrapi_int_to_price( $sale_price ),
array( 'currency' => $compset->lowest_priced_product['currency'] )
);
}
$currency = dfrcs_currency( $compset->lowest_priced_product );
$regular_price = $currency . dfrapi_int_to_price( $regular_price );
$sale_price = $currency . dfrapi_int_to_price( $sale_price );
return wc_format_sale_price( $regular_price, $sale_price );
}
add_filter( 'woocommerce_get_price_html', 'mycode_change_product_price_to_reflect_prices_in_compset', 20, 2 );
3

Save & Activate

Save your file and activate your custom plugin.

Heads-up!

This only works for a product which already has a Comparison Set. It does not work if the product has never had a Comparison Set created for it.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us