Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WP][WooCommerce] Calculate sale price dynamically / programmatically based on taxonomy term custom field % (also works for Facebook Catalog feed)
<?php
// Backend
function wc_custom_dynamic_sale_price( $sale_price, $product ) {
$id = $product->get_id();
$tax = get_the_terms($id,'product_cat');
$regular_price = wc_format_decimal( $product->get_price() );
$discount_total = 0;
foreach ($tax as $term){
$disc_single = get_field('sale_value', $term->taxonomy . '_' . $term->term_id);
$discount_total += $disc_single;
}
if($discount_total !== 0 || $discount_total !== '')
return wc_format_decimal($regular_price - ($regular_price / 100 * $discount_total));
elseif($discount_total == 0 || $discount_total == '')
return '';
else
return $sale_price;
};
if(is_admin()) {
add_filter( 'woocommerce_product_get_sale_price', 'wc_custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'wc_custom_dynamic_sale_price', 10, 2 );
}
function custom_dynamic_sale_price_html( $price_html, $product ) {
if( $product->is_type('variable') ) return $price_html;
$price_html = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ) . $product->get_price_suffix();
return $price_html;
}
if(is_admin()) {
add_filter( 'woocommerce_get_price_html', 'custom_dynamic_sale_price_html', 20, 2 );
}
// Frontend
function wc_change_product_price_display( $price ) {
$id = get_the_ID();
$tax = get_the_terms($id,'product_cat');
$discount_total = 0;
foreach ($tax as $term){
$disc_single = get_field('sale_value', $term->taxonomy . '_' . $term->term_id);
$discount_total += $disc_single;
}
if ($discount_total !== 0){
$symbol = get_woocommerce_currency_symbol();
$new_price = preg_replace("/[^0-9,]/", "",$price);
$price = '<del>'.$price.'</del> '.wc_format_decimal(($new_price - ($new_price / 100 * $discount_total))).$symbol;
}
return $price;
}
if(!is_admin()) {
add_filter( 'woocommerce_get_price_html', 'wc_change_product_price_display', 999 );
}
add_filter( 'woocommerce_cart_item_price', 'wc_change_product_price_display', 999 );
// Discount in cart
function wc_prefix_add_discount_line( $cart ) {
$items = $cart->get_cart();
$disc_cart_total = 0;
foreach ($items as $item){
$item_id = $item['product_id'];
$discount_total = 0;
$tax = get_the_terms($item_id,'product_cat');
foreach ($tax as $term){
$disc_single = get_field('sale_value', $term->taxonomy . '_' . $term->term_id);
$discount_total += $disc_single;
}
if ($discount_total !== 0){
$current_price = $item['line_total'];
$discount_price = $current_price / 100 * $discount_total;
$disc_cart_total +=$discount_price;
}
}
if ($disc_cart_total !== 0){
$cart->add_fee( __( 'Sale', TEXT_DOMAIN ) , -$disc_cart_total );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'wc_prefix_add_discount_line' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.