Hi everyone! I'm new here, so forgive the big blast of text here but I'm hoping someone has some helpful suggestions on this one. I'm having an issue changing Woocommerce product archive / shop page pricing display. We're (still) using Woocommerce 4.8.0 and WordPress 5.7.1.
Here's what I am trying to do...
This shop carries parts for thousands of bike makes, models & model-years. One part might fit 1000 different bikes, but it might be priced differently from one bike make-model-year to another.
We allow the user to look-up their SPECIFIC bike make, model and year and browse parts that fit. When the user specifies their exact bike, we can provide the price for the part that fits their bike. It's a lot like WC Product Variations, but for several reasons, we're not using Variations... instead we are dynamically modifying the "simple product" pricing, featured image, sku etc. from a lookup table based on the user's selections.
When the specific bike is not defined -- for example, when we only know the make and model but not the specific year -- we want to display the applicable range of pricing on the shop/product category archive pages and on the product page. The lookup table provides this data.
We're modifying the price display using the "woocommerce_get_price_html" filter as follows:
`/*** Change the appearance of the price display on shop & product pages ***/
function change_product_price_html( $price_html, $product ) {
global $priceArray; // this is a global price array for price ranges.
$pid = $product->get_id();
// Default to product price unless we have variations or a range (below)
$reg = number_format($product->get_regular_price(),2);
$sale = number_format($product->get_sale_price(),2);
// $pbmy is used to specify whether the particular product category we're viewing is for a part, brand, model or year. This meta data is stored using ACF (Advanced Custom Fields).
$pbmy = '';
// On product category archive pages, get the category ID and get the ACF definition for $pbmy.
if ( is_product_category() ) {
$category=get_queried_object();
$taxonomy = $category->taxonomy;
$current_catid = $category->term_id;
// Determine whether this is a bike year category -- if so, look up pricing on this variation product.
$pbmy = get_field('part_brand_model_or_year', $taxonomy . '_' . $current_catid);
}
// If $pbmy !== 'y' then we don't know the full make-model-year of the bike and should lookup whether there is a price range that would be the case if isset($priceArray[$pid])...
if (isset($priceArray[$pid]) && strtolower($pbmy)!=='y') { // If there is a range to display...
$minreg = $priceArray[$pid]["MIN_regprice"];
$maxreg = $priceArray[$pid]["MAX_regprice"];
$minsale = $priceArray[$pid]["MIN_saleprice"];
$maxsale = $priceArray[$pid]["MAX_saleprice"];
if ($minreg !== "0" && $maxreg !== "0") {
if (intval($minreg) < intval($maxreg)) {
$reg = $minreg . " - " . $maxreg;
} else {
$reg=number_format($product->get_regular_price(),2);
}
}
if ($minsale !== "0" && $maxsale !== "0") {
if (intval($minsale) < intval($maxsale)) {
$sale = $minsale . " - " . $maxsale;
} else {
$sale=number_format($product->get_sale_price(),2);
}
}
}
// Otherwise, on category pages where we DO know the bike make-model-year ($pbmy='y'), lookup the specific price.
if ( is_product_category() && strtolower($pbmy)=='y' ) {
// The function "getPPVariants" returns specific price data from a database lookup table.
$variationdata = getPPVariants($pid,$current_catid);
if (isset($variationdata[0]->reg_price)) $reg = $variationdata[0]->reg_price;
if (isset($variationdata[0]->sale_price)) $sale = $variationdata[0]->sale_price;
}
$price_html = '
<span class="woocs_price_code" data-product-id="'.$pid.'">
<span class="shopprice">Retail Price </span>
<del>
<span class="woocommerce-Price-amount amount">
<bdi><span class="woocommerce-Price-currencySymbol">$</span>'.$reg.'</bdi>
</span>
</del>
<br />
<span class="shopprice">Our Price </span>
<ins>
<span class="woocommerce-Price-amount amount">
<bdi><span class="woocommerce-Price-currencySymbol">$</span>'.$sale.'</bdi>
</span>
</ins>
</span>';
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'change_product_price_html', 10 , 2);`
For the case where we know the specific bike make-model-year and want to display a unique price for each part, everything works great for a second or two... then, for COMPLETELY UNKNOWN REASONS, the pricing gets overwritten and we then see a price range displayed, as though we forgot the bike make-model-year. It's almost as though this filter gets called multiple times, and after the first round, we lose the criteria we use to determine whether this is a "known" bike make-model-year (and we revert back to looking-up and displaying a price range, if there is one).
I've tried removing the filter prior to establishing the new one. That didn't work. I'm also not using any caching.
Has anyone seen anything like this?? Any pointers?