在单个产品页面上显示类别和品牌名称 [英] Display category and brand name on single product page

查看:105
本文介绍了在单个产品页面上显示类别和品牌名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce中,我正在使用 YITH WooCommerce品牌插件处理产品品牌。

In Woocommerce, I am using YITH WooCommerce Brands plugin to handle product brands.

我目前正在努力解决WooCommerce中简短说明下想要的固定文本。我想在该文本(有效)中动态显示产品名称,还要显示产品类别名称 [CATEGORY_NAME] 和品牌名称 [BRAND_NAME]

I'm currently struggling with a fixed text That I want under my short description in WooCommerce. I want to display dynamically the product name in that text (which works), but also the product category name [CATEGORY_NAME] and brand name [BRAND_NAME].

但我似乎无法使它们发挥作用。

But I can't seem to get those to work.

基于此答案线程:在Woocommerce中的单个产品简短描述下添加文本

这是我的代码版本:

// Add text after short description

add_action( 'woocommerce_single_product_summary', 'custom_single_product_summary', 2 );
function custom_single_product_summary(){
    global $product;

    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
    add_action( 'woocommerce_single_product_summary', 'custom_single_excerpt', 20 );
}

function custom_single_excerpt(){
    global $post, $product;

    $short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt );

    if ( ! $short_description )
        return;

    // The custom text
    $custom_text = 'Zoekt u naast de '.$product->get_name().' andere [PRODUCT_CATEGORY] van dit merk? Bekijk dan eens de gehele collectie van [BRAND_NAME]. Powerlight is officieel dealer van [BRAND_NAME]. Heeft u een specifieke vraag over dit product? Neem gerust eens contact op met onze <span style="text-decoration: underline;"><a href="https://power-light.nl/contact/">klantenservice</a></span>. Onze adviseurs staan graag voor u klaar.';

    ?>
    <div class="woocommerce-product-details__short-description">
        <?php echo $short_description . $custom_text; // WPCS: XSS ok. ?>
    </div>
    <?php
}

任何想法都是我如何展示产品

Any idea's how I can display the product category name and the product brand name in my custom text?

推荐答案

下面的代码将在产品简称后输出您的自定义文本正确的产品类别和正确的(Yith)产品品牌(因此适用于YITH WooCommerce Brands插件)

The code below will output you custom text just after the product short description with the correct Product category et the correct (Yith) product Brand (So for YITH WooCommerce Brands plugin).

add_action( 'woocommerce_single_product_summary', 'custom_single_product_summary', 2 );
function custom_single_product_summary(){
    global $product;

    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
    add_action( 'woocommerce_single_product_summary', 'custom_single_excerpt', 20 );
}

function custom_single_excerpt(){
    global $post, $product;

    $short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt );

    if ( ! $short_description )
        return;

    // Get product categories
    $categories = wp_get_post_terms( $post->ID, 'product_cat', array( 'fields' => 'names' ) );
    // Get product brands (NOTE: for Woocommerce brands plugin, the taxonomy is 'product_brand')
    $brands     = wp_get_post_terms( $post->ID, 'yith_product_brand', array( 'fields' => 'names' ) );

    // The custom link
    $custom_link = '<span style="text-decoration: underline;"><a href="https://power-light.nl/contact/">'.__("klantenservice").'</a></span>';

    // The custom text
    $custom_text = sprintf(__("Zoekt u naast de %s andere %s van dit merk? Bekijk dan eens de gehele collectie van %s. Powerlight is officieel dealer van %s. Heeft u een specifieke vraag over dit product? Neem gerust eens contact op met onze %s. Onze adviseurs staan graag voor u klaar."), $product->get_name(), reset($categories), reset($brands), reset($brands), $custom_link );

    ?>
    <div class="woocommerce-product-details__short-description">
        <?php echo $short_description . $custom_text; // WPCS: XSS ok. ?>
    </div>
    <?php
}

代码进入function.php文件您的活动子主题(或活动主题)。经过测试并可以正常工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.

如果使用 Woocommerce Brands插件,则必须在代码'yith_product_brand'中替换'product_brand'。就是这样。

If you use Woocommerce Brands plugin, you will have to replace in the code 'yith_product_brand' by 'product_brand'. That's all.

这篇关于在单个产品页面上显示类别和品牌名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆