简体   繁体   English

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

[英]Show a price suffix only on all WooCommerce single products

I am using Show Price Suffix only on all WooCommerce Product loops answer code to my previous question, to display a price suffix after the price on all pages except the Product Detail Page (WooCommerce).只在所有 WooCommerce 产品循环上使用显示价格后缀回答我上一个问题的代码,以便在除产品详细信息页面 (WooCommerce) 之外的所有页面上显示价格后缀。

I want to have another price suffix only for the Product Detail Page.我只想为产品详细信息页面设置另一个价格后缀。 The suffix should include a link and the font size should be editable.后缀应该包含一个链接,并且字体大小应该是可编辑的。

Can anyone help me?谁能帮我?

To display a price suffix on single products only with a custom link, try the following:要仅使用自定义链接在单个产品上显示价格后缀,请尝试以下操作:

add_filter( 'woocommerce_get_price_suffix', 'additional_single_product_price_suffix', 999, 4 );
function additional_single_product_price_suffix( $html, $product, $price, $qty ){
    global $woocommerce_loop;

    // Not on single products
    if ( ( is_product() && isset($woocommerce_loop['name']) && empty($woocommerce_loop['name']) ) ) {
        // Define below the link for your price suffix
        $link = home_url( "/somelink.html" );

        $html .= ' <a href="' . $link . '" target="_blank" class="price-suffix">' . __('Suffix 2') . '</a>';
    }
    return $html;
}

Inline CSS style rules (can be added instead to the theme's styles.ccs file) :内联 CSS 样式规则(可以添加到主题的 styles.ccs 文件中)

add_action('wp_head', 'product_price_suffix_css_styling_rules', 9999 );
function product_price_suffix_css_styling_rules() {
    // Only on single product pages
    if( is_product() ):
    ?><style>
        a.price-suffix, a.price-suffix:visited {font-size: 13px; color: #DC143C;}
        a.price-suffix:hover, a.price-suffix:active {color: #960404}
    </style><?php
    endif;
}

Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的functions.php 文件。 Tested and works.测试和工作。


If the price suffix should include a link inside the text, use the following:如果价格后缀应在文本中包含链接,请使用以下内容:

add_filter( 'woocommerce_get_price_suffix', 'additional_single_product_price_suffix', 999, 4 );
function additional_single_product_price_suffix( $html, $product, $price, $qty ){
    global $woocommerce_loop;

    // Not on single products
    if ( ( is_product() && isset($woocommerce_loop['name']) && empty($woocommerce_loop['name']) ) ) {
        // Define below the link for your price suffix
        $link = home_url( "/somelink.html" );

        $html .= sprintf( ' <span class="price-suffix">' . __('Suffix %s') . '</span>', '<a href="' . $link . '"  target="_blank">' . __("link") . '</a>');
    }
    return $html;
}

Inline CSS style rules (can be added instead to the theme's styles.ccs file) :内联 CSS 样式规则(可以添加到主题的 styles.ccs 文件中)

add_action('wp_head', 'product_price_suffix_css_styling_rules', 9999 );
function product_price_suffix_css_styling_rules() {
    // Only on single product pages
    if( is_product() ):
    ?><style>
        span.price-suffix {font-size: 13px; color: #000000;}
        span.price-suffix > a, span.price-suffix > a:visited {color: #DC143C}
        span.price-suffix > a:hover, span.price-suffix > a:active {color: #960404}
    </style><?php
    endif;
}

Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的functions.php 文件。 Tested and works.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM