Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EricBusch/c0d0b3dffcd3d534a1cd862283ed0378 to your computer and use it in GitHub Desktop.
Save EricBusch/c0d0b3dffcd3d534a1cd862283ed0378 to your computer and use it in GitHub Desktop.
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 and setting the lowest sale price found in the Comparison Set to the "Sale" price. [datafeedr][dfrcs]
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment