用自定义文本替换WooCommerce的0销售价格,保持正常价格的删除线。

huangapple go评论60阅读模式
英文:

Replace WooCommerce 0 sale price with a custom text keeping regular price strike-through

问题

Here is the modified PHP code to change the WooCommerce product's sale price to "Free for members" without affecting the regular price and maintaining the strike-through for the regular price:

function my_wc_custom_get_price_html( $price, $product ) {
    if ( $product->get_price() == 0 ) {
        if ( $product->is_on_sale() && $product->get_regular_price() ) {
            $regular_price = wc_get_price_to_display( $product, array( 'qty' => 1, 'price' => $product->get_regular_price() ) );

            $price = '<del>' . wc_price( $regular_price ) . '</del> ' . __( 'Free for members!', 'woocommerce' );
        } else {
            $price = '<span class="amount">' . __( 'Free for members!', 'woocommerce' ) . '</span>';
        }
    }

    return $price;
}

add_filter( 'woocommerce_get_price_html', 'my_wc_custom_get_price_html', 10, 2 );

This code will ensure that the strike-through is applied only to the regular price while displaying "Free for members" as the sale price, as shown in your desired screenshot.

英文:

I have been trying to change WooCommerce product's sale price only to "Free for members" instead of 0.
I wanted to change the sale price to a custom text like:

用自定义文本替换WooCommerce的0销售价格,保持正常价格的删除线。

I have searched the internet & found code snippets which does the same thing, but the problem is that it changes both sale price & regular price too.

This was the code I found here on Stack Overflow:

function my_wc_custom_get_price_html( $price, $product ) {
    if ( $product-&gt;get_price() == 0 ) {
        if ( $product-&gt;is_on_sale() &amp;&amp; $product-&gt;get_regular_price() ) {
            $regular_price = wc_get_price_to_display( $product, array( &#39;qty&#39; =&gt; 1, &#39;price&#39; =&gt; $product-&gt;get_regular_price() ) );

            $price = wc_format_price_range( $regular_price, __( &#39;Free for members!&#39;, &#39;woocommerce&#39; ) );
        } else {
            $price = &#39;&lt;span class=&quot;amount&quot;&gt;&#39; . __( &#39;Free!&#39;, &#39;woocommerce&#39; ) . &#39;&lt;/span&gt;&#39;;
        }
    }

    return $price;
}

add_filter( &#39;woocommerce_get_price_html&#39;, &#39;my_wc_custom_get_price_html&#39;, 10, 2 );

The problem is this code removes the strike-through on regular price too. Here is the result when I try to add some inline CSS to add strike-through:

用自定义文本替换WooCommerce的0销售价格,保持正常价格的删除线。

What I am trying to achieve is to change the sale price like in this screenshot:

用自定义文本替换WooCommerce的0销售价格,保持正常价格的删除线。

答案1

得分: 2

尝试以下简化的代码,以在产品以0价格上架时显示带有自定义文本的删除线常规价格:

add_filter( 'woocommerce_get_price_html', 'custom_formatted_sale_price_html', 10, 2 );
function custom_formatted_sale_price_html( $price_html, $product ) {
    if ( $product->is_on_sale() && $product->get_price() !== 0 ) {
        $regular_price   = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
        $sale_price_text = $regular_price > 0 ? __( 'Free only for members!', 'woocommerce' ) : __( 'Free!', 'woocommerce' );
        $style           = $regular_price > 0 ? ' style="background-color:#08A04B;color:white;padding:0 5px;"' : '';

        return '<del aria-hidden="true">' . wc_price( $regular_price ) . '</del> <span'.$style.'>' .  $sale_price_text . '</span>';
    }
    return $price_html;
}

它应该按照您的期望工作。

英文:

Try the following simplified code to display a strike-through regular price with a custom text, when a product is on sale with a 0 (zero) price:

add_filter( &#39;woocommerce_get_price_html&#39;, &#39;custom_formatted_sale_price_html&#39;, 10, 2 );
function custom_formatted_sale_price_html( $price_html, $product ) {
    if ( $product-&gt;is_on_sale() &amp;&amp; $product-&gt;get_price() !== 0 ) {
        $regular_price   = wc_get_price_to_display( $product, array( &#39;price&#39; =&gt; $product-&gt;get_regular_price() ) );
        $sale_price_text = $regular_price &gt; 0 ? __( &#39;Free only for members!&#39;, &#39;woocommerce&#39; ) : __( &#39;Free!&#39;, &#39;woocommerce&#39; );
        $style           = $regular_price &gt; 0 ? &#39; style=&quot;background-color:#08A04B;color:white;padding:0 5px;&quot;&#39; : &#39;&#39;;
 
        return &#39;&lt;del aria-hidden=&quot;true&quot;&gt;&#39; . wc_price( $regular_price ) . &#39;&lt;/del&gt; &lt;span&#39;.$style.&#39;&gt;&#39; .  $sale_price_text . &#39;&lt;/span&gt;&#39;;
    }
    return $price_html;
}

It should work as you expect.

huangapple
  • 本文由 发表于 2023年7月23日 19:45:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定