Woocommerce:仅在产品页面上显示价格后缀

安迪

我将以下代码添加到“我的自定义函数PHP插入程序”插件中,以在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文件:在类别页面上隐藏Woocommerce价格后缀

我也想更改“ inkl。MwSt。和zzgl。Versandkosten”的字体大小,但是我不知道如何在php中做到这一点。尝试过此CSS,但不执行任何操作:

.custom_price_suffix {
  font-size: small;
}

油条

您可以使用内置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菜单项下的“字符串翻译”选项卡下-从此处您可以根据不同的网站语言手动为其分配新的字符串。

第二种方法是使用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 );
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Woocommerce,在产品页面上删除价格

仅在 WooCommerce 单个产品页面上添加价格后缀,没有链接产品

WooCommerce在产品页面上的外部产品的“添加到购物车”按钮上显示价格

根据 WooCommerce 商店和存档页面上的产品类别在产品价格下方显示图像

在产品页面上显示运输成本-WooCommerce

在产品页面上禁用WooCommerce SKU

在Woocommerce存档页面上在产品价格之后添加图标

如何在产品页面上添加带有价格的单选按钮?WordPress woocommerce

Lightbox ix中的Woocommerce产品图像未显示在产品详细信息页面上

获取woocommerce产品属性url以图像形式显示在产品页面上

使用WooCommerce Action Hook显示产品在产品页面上的年代

WooCommerce:仅在单个产品上显示价格后缀-不适用于相关产品

如何更改woocommerce价格样式,但仅在当前产品的单个产品页面上

在 WooCommerce 中审查提交后在产品页面上显示通知

在产品页面上的表格中显示WooCommerce变化数据(尺寸,重量等)

在产品页面上显示阵列中的产品属性值

仅在所有WooCommerce产品循环上显示价格后缀

仅在所有WooCommerce单一产品上显示价格后缀

在产品编辑页面上删除WooCommerce元框

Woocommerce-在产品页面上获取运费

根据产品价格在WooCommerce产品页面上显示其他货币

如何在产品页面上显示下载链接?

Wordpress-在产品页面上显示MySQL列数据

如何在产品页面上显示shopify安全徽标?

选择产品选项后,在产品页面上重新加载价格

Woocommerce - 仅在产品标题下显示父类别

在WooCommerce单一产品页面上显示总价格,包括原始产品价格*最小数量

如何在自定义页面上按ID号显示Woocommerce产品价格?

在页面上显示 Woocommerce 中低于特定价格的所有产品