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

时间:2020-07-16 20:53:53

标签: php html css wordpress woocommerce

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

我只想为产品详细信息页面添加另一个价格后缀。 后缀应包含一个链接,字体大小应可编辑。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分: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文件中。经过测试,可以正常工作。