I\'m trying to work on a custom conditional output where when a product loop is found with sales price, it adds a class to the sale price tag. If there\'s only regular price, it
This hook is a filter with 2 variables ($price
and $instance
) and you return $price
instead of echo $price
). You could try to use it this way:
add_filter('woocommerce_sale_price_html','price_custom_class', 10, 2 );
function price_custom_class( $price, $product ){
if (isset($product->sale_price)) {
$price = '<del class="strike">'.woocommerce_price( $product->regular_price ).'</del>
<ins class="highlight">'.woocommerce_price( $product->sale_price ).'</ins>';
}
else
{
$price = '<ins class="highlight">'.woocommerce_price( $product->regular_price ).'</ins>';
}
return $price;
}
This hook is for sale price normally.
Reference: woocommerce_sale_price_html
For regular price, you have woocommerce_price_html
filter hook:
add_filter( 'woocommerce_price_html', 'price_custom_class', 10, 2 );
function price_custom_class( $price, $product ){
// your code
return $price;
}
Reference: woocommerce_price_html
You need filter hook here, not action hook, to hook a function or method to a specific filter action. change to
add_filter('woocommerce_sale_price_html','price_custom_class');