Forked from plugin-republic/woocommerce_get_price_html.php
Created
July 20, 2021 16:37
-
-
Save tamara-m/ef891322ccb2d0256f763b7a623be40e to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Filter the price html on shop and product pages | |
* @todo Display correct variation price range in variation.php | |
*/ | |
function prefix_get_price_html( $price, $product ) { | |
$rules = get_option( 'wcfad_dynamic_pricing_rules', false ); | |
// Get the best user role defined price | |
$price = $product->get_price(); | |
$best_price = wcfad_get_product_price_by_role( $product, $price ); | |
// Use this to return the standard price with no extra wording if there's no discount for this product | |
if( $price == $best_price ) { | |
$price_html = sprintf( | |
'<p class="price">%s</p>', | |
wc_price( $price ) | |
); | |
return $price_html; | |
} | |
// Check for variation pricing | |
if( $product->is_type( 'variable' ) ) { | |
$prices = $product->get_variation_prices( true ); | |
$min_variation_price = wcfad_get_product_price_by_role( $product, current( $prices['price'] ) ); | |
$max_variation_price = wcfad_get_product_price_by_role( $product, end( $prices['price'] ) ); | |
$min_max = array( $min_variation_price, $max_variation_price ); | |
} else { | |
$min_max = array( $best_price, $best_price ); | |
} | |
$qualifies = false; | |
$product_id = $product->get_id(); | |
if( $rules ) { | |
foreach( $rules as $rule ) { | |
$validation_function = ''; | |
// Iterate through each rule | |
if( ! empty( $rule['rule'] ) ) { | |
// Check if this product is covered by a pricing rule | |
$qualifies = false; | |
$label = isset( $rule['label'] ) ? $rule['label'] : ''; | |
$applies_to = isset( $rule['applies_to'] ) ? $rule['applies_to'] : 'all'; | |
// The list of products that qualify for the offer | |
if( $applies_to == 'all' ) { | |
$qualifies = true; | |
$min_max = wcfad_get_min_max_pricing( $min_max, $rule, $best_price ); | |
// break; | |
} else if( $applies_to == 'products' ) { | |
$buy_products = isset( $rule['buy_products'] ) ? $rule['buy_products'] : 'all'; | |
if( $buy_products == 'all' || ( is_array( $buy_products ) && in_array( $product_id, $buy_products ) ) ) { | |
$qualifies = true; | |
$min_max = wcfad_get_min_max_pricing( $min_max, $rule, $best_price ); | |
// break; | |
} | |
} else if( $applies_to == 'categories' ) { | |
$buy_categories = isset( $rule['buy_categories'] ) ? $rule['buy_categories'] : 'all'; | |
if( $buy_products == 'all' || ( is_array( $buy_products ) && has_term( $get_categories, 'product_cat', $product_id ) ) ) { | |
$qualifies = true; | |
$min_max = wcfad_get_min_max_pricing( $min_max, $rule, $best_price ); | |
// break; | |
} | |
} | |
} | |
} | |
} | |
if( $min_max[0] != $min_max[1] ) { | |
// Display the formatted price range | |
$price_html = sprintf( | |
'<p class="price">From %s</p>', | |
wc_price( $min_max[0] ) | |
); | |
} else { | |
$price_html = sprintf( | |
'<p class="price">From %s</p>', | |
wc_price( $best_price ) | |
); | |
} | |
if( $qualifies ) { | |
$price_html = sprintf( | |
'%s<br><span class="wcfad-rule-label">%s</span>', | |
$price_html, | |
$label | |
); | |
} | |
return $price_html; | |
} | |
add_filter( 'woocommerce_get_price_html', 'prefix_get_price_html', 999, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment