【问题标题】:How to add a CSS class for the displayed discounted percentage near sale price on single product pages in WooCommerce如何在 WooCommerce 中的单个产品页面上为显示的接近销售价格的折扣百分比添加 CSS 类
【发布时间】:2020-06-12 18:47:02
【问题描述】:

我使用“Display the discounted percentage near sale price in Single product pages for WC 3.0+”(原始答案代码)

我使用此代码在单个产品页面上显示价格后的销售百分比。但我想定制它——给它背景、填充等......

所以我需要在该函数中包含 CSS 类名才能通过 CSS 自定义它

我只想将其添加到“-percentage”(我不想将其应用于折扣价或正价)

【问题讨论】:

  • 你的意思是? <del class="myclass">。您的问题对我来说不是很清楚,您可以更详细地调整它吗?
  • 我使用此代码在单个产品页面上显示价格后的销售百分比。但我想自定义它 - 给它背景、填充等。所以我需要在该函数中包含 CSS 类名(“myclass”)才能通过 css 自定义它
  • 是的,您已经在问题中提到了这一点。我要求提供更多更清晰的细节。该函数提供了一定的输出,你想在哪个元素上添加类?我可以假设我刚才提到的例子已经解决了你的问题吗?
  • 我希望它同时应用“-”和价格,而不仅仅是价格

标签: php css wordpress woocommerce discount


【解决方案1】:

一些附加信息

所以你得到

function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
    $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
    $percentage_txt = '<span class="my-class">' . __('-', 'woocommerce' ) . $percentage . '</span>';
    $price = '<del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del>
    <ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) . $percentage_txt : $sale_price . $percentage_txt ) . '</ins>';

    return $price;
}
add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );

【讨论】:

  • 您好,抱歉打扰了...我希望此功能仅适用于单个产品页面。(现在它也适用于商店页面)如何做到这一点?
  • 添加带有 Conditional Tag 的 if 语句 - is_product() 在单个产品页面上返回 true。看看这是如何在this answer 中应用的
  • 嗨 :) 我遇到了可变产品的问题。如果产品是可变的,它显示 NAN% 你知道如何解决它吗?谢谢
  • 我刚刚重新测试了这段代码,这段代码仍然适用于单一和可变产品,没有任何问题。我认为问题出在其他地方,您必须在自己的设置中进行调试才能解决问题
  • 好的,谢谢
猜你喜欢
  • 2017-10-01
  • 2018-07-06
  • 1970-01-01
  • 2018-05-11
  • 2019-12-25
  • 1970-01-01
  • 1970-01-01
  • 2019-03-04
相关资源
最近更新 更多