Conditional custom output around products sale price and regular price

后端 未结 2 847
清歌不尽
清歌不尽 2021-01-23 15:25

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

相关标签:
2条回答
  • 2021-01-23 16:07

    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

    0 讨论(0)
  • 2021-01-23 16:16

    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'); 
    
    0 讨论(0)
提交回复
热议问题