【问题标题】:Woocommerce: Show Price Suffix only on Product PageWoocommerce:仅在产品页面上显示价格后缀
【发布时间】:2019-12-04 13:04:57
【问题描述】:

我将以下代码添加到“My Custom Functions PHP Inserter”插件中,以在我的 woocommerce 商店中显示自定义价格后缀。

add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );

function custom_price_suffix( $price, $product ){
    $price = $price . ' inkl. MwSt. und <a href="http://www.link.to/shippinginfo">zzgl. Versandkosten</a>'; 
    return apply_filters( 'woocommerce_get_price', $price );
}

它有效,但它还在产品类别页面和商店页面上显示价格。我怎样才能避免这种情况?

我尝试了这个 css 代码但不起作用:

.woocommerce-price-suffix {
      display: none;
    }
    .single-product .product-price-wrap .woocommerce-price-suffix {
      display: block !important;
    }

以下解决方案可能有效,但我不想在我的主题中覆盖 php 文件:hide Woocommerce price suffix on category page

我也想改变“inkl. MwSt. und zzgl. Versandkosten”的字体大小,但我不知道如何在 php.ini 中执行此操作。尝试了这个 css 但什么也没做:

.custom_price_suffix {
  font-size: small;
}

【问题讨论】:

  • 显示实时页面。

标签: php css woocommerce


【解决方案1】:

您可以使用内置的is_singular()函数来检查您是否在单个产品页面上

add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ) {
    if(is_singular('product')) {
        $price = $price . ' inkl. MwSt. und <a href="http://www.link.to/shippinginfo">zzgl. Versandkosten</a>'; 
    }
    return apply_filters( 'woocommerce_get_price', $price );
}

如果你想改变大小 - 你可以简单地将你的文本包装在一个跨度内并添加 CSS 到它 - 即将你的 $price 变量更改为:

$price = $price . ' <span class="make-me-small">inkl. MwSt. und <a href="http://www.link.to/shippinginfo">zzgl. Versandkosten</a></span>'; 

然后将以下内容添加到您的 CSS 中:

.make-me-small {
    font-size: 0.8rem;
}

编辑:添加特定位置的调整

根据您对基于网站基本语言调整链接文本的评论,有两种方法可以实现:

第一条路线(可能是最好的/公认的方式)是使用内置的字符串翻译管理功能。您可以使用 __() 函数并将其包裹在您的文本周围,如下所示:

$price = $price . __(' inkl. MwSt. und <a href="http://www.link.to/shippinginfo">zzgl. Versandkosten</a>', 'my-text-domain');

完成此操作后,您将看到您的文本现在将显示在仪表板上 WPML 菜单项下的 String Translations 选项卡下 - 您可以从那里手动为其分配一个新的基于不同站点语言的字符串。

第二条路线是使用switch(或if语句)语句在函数内部手动添加调整:

add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ) {
    if(is_singular('product')) {
        $language_code = apply_filters( 'wpml_current_language', NULL );
        switch ($language_code) {
            case 'de':
                $suffix = ' inkl. MwSt. und <a href="http://www.link.to/shippinginfo">zzgl. Versandkosten</a>';
                break;

            case 'it':
                $suffix = ' IVA e spese di <a href="http://www.link.to/shippinginfo">spedizione incluse</a>';
                break;

            default:
                $suffix = ' incl. VAT and <a href="http://www.link.to/shippinginfo">shipping costs</a>';
                break;
        }
        $price = $price . $suffix; 
    }
    return apply_filters( 'woocommerce_get_price', $price );
}

【讨论】:

  • 您的代码中的 { 括号似乎有误
  • 啊-感谢您发现@Andy!现在修好了。
  • @Andy 啊 - 是的,我把 return 放在了错误的地方 - 我已经编辑了答案来为你修复它:)
  • @Andy 太棒了——很好发现。我已经修正了撇号 :) 如果你愿意,我还调整了语言功能以使用你提到的最新钩子。干杯:)
  • 你太棒了!非常感谢@Frits :)
猜你喜欢
  • 1970-01-01
  • 2020-11-05
  • 2020-11-06
  • 2021-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-03
  • 2021-05-19
相关资源
最近更新 更多