有条件的定制产品周围的产品销售价格和正常价格(Conditional custom output around products sale price and regular price)

编程入门 行业动态 更新时间:2024-10-25 22:30:53
有条件的定制产品周围的产品销售价格和正常价格(Conditional custom output around products sale price and regular price)

我正在尝试使用自定义条件输出,当找到具有销售价格的产品循环时,它会在销售价格标签中添加一个类。 如果只有正常价格,则将此类添加到常规价格标签中。

从不同的文档中查看和关闭后,我似乎无法使其工作:

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

我正在使用常规价格过滤器并尝试将span class =“amount”标签更改为ins span class =“amount”,但我仍然得到相同的输出。 任何想法?

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

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 adds this class to regular price tag.

I can't seem to get this to work after looking on & off from different documentations:

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'm using the regular price filter & trying to change the span class="amount" tag to ins span class="amount", however I still get the same output. 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 ); }

最满意答案

这个钩子是一个带有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过滤钩子:

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

参考: woocommerce_price_html

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

更多推荐

本文发布于:2023-08-07 17:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465129.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:产品   有条件   销售价格   价格   Conditional

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!