Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricBusch/afcedeba8251ddec2fc0d262ffc5526f to your computer and use it in GitHub Desktop.
Save EricBusch/afcedeba8251ddec2fc0d262ffc5526f to your computer and use it in GitHub Desktop.
Replace the price of a WooCommerce product with the lowest price of a cached comp set. [datafeedr][dfrcs]
<?php
/**
* Replace the price of a WooCommerce product with the lowest price of a cached comp set.
*
* Example: From $29.99
*
* @param string $price Price wrapped in HTML markup.
* @param WC_Product $product
*
* @return string Updated HTML markup.
*/
add_filter( 'woocommerce_get_price_html', 'mycode_change_main_product_price', 20, 2 );
function mycode_change_main_product_price( $price, $product ) {
// Create $source product from WooCommerce
$source = dfrcs_wc_get_source_of_product();
$source['context'] = 'wc_single_product_page_price_element';
// Get the compset for this product.
$compset = new Dfrcs( $source );
// If we have a cached version (because we don't want to do this on every product every page load), get
// the lowest priced product and set that equal to the price.
if ( $compset->cached && ! $compset->cache_is_expired && ! empty( $compset->lowest_priced_product ) ) {
$currency = dfrcs_currency( $compset->lowest_priced_product );
$price = 'From ' . $currency . dfrapi_int_to_price( $compset->lowest_priced_product['finalprice'] );
}
return $price;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment