【问题标题】:Show a price suffix only on all WooCommerce single products仅在所有 WooCommerce 单品上显示价格后缀
【发布时间】:2020-11-06 15:08:55
【问题描述】:

我正在使用 Show Price Suffix only on all WooCommerce Product loops 回答我上一个问题的代码,以便在除产品详细信息页面 (WooCommerce) 之外的所有页面上显示价格后缀。

我想为产品详细信息页面设置另一个价格后缀。 后缀应该包含一个链接,并且字体大小应该是可编辑的。

谁能帮帮我?

【问题讨论】:

    标签: php html css wordpress woocommerce


    【解决方案1】:

    要仅使用自定义链接在单个产品上显示价格后缀,请尝试以下操作:

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

    内联 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;
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。


    如果价格后缀应在文本中包含链接,请使用以下内容:

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

    内联 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;
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 感谢它的工作,但链接应该在新标签中打开。以及如何更改链接的字体大小?
    • 是的,我的意思是新窗口。我想更改文本和链接的字体大小。你能帮帮我吗?
    • 它工作完美,你能添加一些我可以改变链接颜色的东西吗?那会很好
    • 它的工作,你能不能只为链接添加一个悬停颜色,然后它就完美了
    • @Hans1234 更新了……试试看。如果这个答案回答了你的问题,你可以请accept回答,谢谢。
    猜你喜欢
    • 2020-11-05
    • 2019-12-04
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 2020-11-13
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多