Conditional user exit at the selling price of products and regular price

I am trying to work with a custom conditional output where, when a product cycle is found with a selling price, it adds a class to the trading price. If there is only a regular price, he adds this class to the usual price tags.

It seems I can not get this to work after I looked in different documents:

add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product ){
    ob_start();
        global $product; 
        if (isset($product->sale_price)) {
            return str_replace( '</del>', '<span class="amount">text</span></del>', $price );
            return str_replace( '</ins>', '<span class="highlight amount">highlight here</span></del>', $price );
        }
        else {
            return str_replace( '</ins>', '<span class="highlight amount">highlight here</span>text</del>', $price );
        }
}

I use the regular price filter and try to change the span class = "tag to span class =", however I still get the same result.
Any idea?

add_filter( 'woocommerce_price_html', 'price_custom_class', 10, 2 );
function price_custom_class( $price, $product ){ 
    return str_replace( '<span class="amount"></span>', '<ins><span class="amount">'.woocommerce_price( $product->regular_price    ).'</span></ins>', $price );
}
+1
source share
2 answers

2 ($price $instance), return $price echo $price). :

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;
}

.

: woocommerce_sale_price_html

woocommerce_price_html hook hook:

add_filter( 'woocommerce_price_html', 'price_custom_class', 10, 2 );
function price_custom_class( $price, $product ){ 
    // your code
    return $price;
}

: woocommerce_price_html

+2

, .  

add_filter('woocommerce_sale_price_html','price_custom_class'); 
+1

Source: https://habr.com/ru/post/1670322/


All Articles