Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AshlinRejo/2ca2b610955f1a3a6c0bf33602fca66d to your computer and use it in GitHub Desktop.
Save AshlinRejo/2ca2b610955f1a3a6c0bf33602fca66d to your computer and use it in GitHub Desktop.
Discount rules v2: Show only discounted price without strikeout for selected rule
/**
* Show only discounted price without strikeout for selected rule
* Products which has other rules will have strikeout
* */
if(!function_exists('showOnlyDiscountPriceWithoutStrikeout')){
function showOnlyDiscountPriceWithoutStrikeout($product, $discounts = false){
$awdr_rules_to_exclude_strikeout = array(1); // REPLACE THE RULE IDS HERE FOR WHICH WE NEED TO EXCLUDE STRIKEOUT eg: array(10,12)
$price = $product->get_price();
if($discounts == false){
$discounts = apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', $price, $product, 1, $price, 'all', true, false);
}
$show_only_discounted_price = false;
if(isset($discounts['total_discount_details']) && !empty($discounts['total_discount_details'])){
$applied_rule_ids = array_keys($discounts['total_discount_details']);
if(count(array_intersect($awdr_rules_to_exclude_strikeout, $applied_rule_ids)) > 0){
$show_only_discounted_price = true;
}
}
return $show_only_discounted_price;
}
}
add_filter('advanced_woo_discount_rules_strikeout_price_html', function ($html, $original_price, $discounted_price, $is_variable_product, $initial_price_html, $separator, $original_price_raw, $discounted_price_raw){
global $product;
if(!empty($product)){
$product_selected = $product;
$exclude_rule_id = 1;
if($is_variable_product){
$variation_prices = $product->get_variation_prices(false);
$variant_id = array_keys($variation_prices['price'])[0];
$product_selected = wc_get_product($variant_id);
}
$show_only_discounted_price = showOnlyDiscountPriceWithoutStrikeout($product_selected, false);
if($show_only_discounted_price){
$html = '<ins>' . $discounted_price . '</ins>';
}
}
return $html;
}, 10, 8);
add_filter('advanced_woo_discount_rules_dynamic_price_html_update', function ($strikeout_html, $product, $initial_price, $discounted_price, $discount_details, $prices){
$show_only_discounted_price = showOnlyDiscountPriceWithoutStrikeout($product);
if($show_only_discounted_price){
$ins_pattern = "/<ins>(.*?)<\/ins>/s";
preg_match($ins_pattern, $strikeout_html, $matches);
if($matches[1]){
$strikeout_html = '<ins>' . $matches[1] . '</ins>';
}
}
return $strikeout_html;
}, 10, 6);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment