Created
May 16, 2017 13:04
-
-
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]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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