Created
March 21, 2018 17:29
-
-
Save WildCoders/6ca086d2f4b491b04c4cada403cd2fbe to your computer and use it in GitHub Desktop.
WooCommerce Category Discount - Modify price html
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
function woo_cat_disc_lite_disp_price_html($price, $product){ | |
global $wcd_model; | |
$prefix = WOO_CAT_DISC_META_PREFIX; // Get prefix | |
if ($product->is_type('simple')) { | |
$product_id = $product->get_id(); | |
$disc_data = $wcd_model->wcd_get_disc_details_from_productid($product_id); // Get discount data | |
if (!empty($disc_data)) { | |
$price = wc_get_price_to_display($product, array('price' => $disc_data['price'])); | |
$new_price = wc_get_price_to_display($product, array('price' => $disc_data['disc_price'])); | |
$disc_label = __(' ', 'woocatdisc') . $disc_data['disc_label'] . __(' ', 'woocatdisc'); | |
$price = wc_format_sale_price($price, $new_price) . $disc_label; | |
} | |
} elseif ($product->is_type('variable')) { | |
$product_id = $product->get_id(); | |
$prices = $product->get_variation_prices(true); | |
if (empty($prices['price'])) { | |
$price = apply_filters('woocommerce_variable_empty_price_html', '', $product); | |
} else { | |
$min_price = $max_price = $min_reg_price = $max_reg_price = ''; | |
$min_price = current($prices['price']); | |
$min_price_key = key($prices['price']); | |
$max_price = end($prices['price']); | |
$max_price_key = key($prices['price']); | |
$min_disc_data = $wcd_model->wcd_get_disc_details_from_productid($product_id, $min_price_key); // Get minimum discount data | |
$max_disc_data = $wcd_model->wcd_get_disc_details_from_productid($product_id, $max_price_key); // Get maximum discount data | |
if (!empty($min_disc_data) && !empty($max_disc_data)) { | |
$pro_min_disc = $min_disc_data['disc_price']; | |
$pro_max_disc = $max_disc_data['disc_price']; | |
$pro_max_reg_price = $max_disc_data['price']; | |
$disc_text = __(' ', 'woocatdisc') . $min_disc_data['disc_label'] . __(' ', 'woocatdisc'); | |
if ($min_price !== $max_price) { | |
$price = $wcd_model->wcd_format_price_range($product, $min_price, $max_price, $pro_min_disc, $pro_max_disc, $disc_text) . $product->get_price_suffix(); | |
} elseif ($product->is_on_sale() && $min_reg_price === $max_reg_price) { | |
$price = wc_format_sale_price(wc_price($pro_max_reg_price), wc_price($pro_min_disc)) . $product->get_price_suffix(); | |
} else { | |
$price = wc_price($pro_min_disc) . $product->get_price_suffix(); | |
} | |
} | |
} | |
} | |
return $price; | |
} | |
add_action('woo_cat_disc_lite_disp_price_html', 'woo_cat_disc_lite_disp_price_html', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment