ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php-在单个产品页面上显示类别和品牌名称

2019-10-13 03:32:38  阅读:249  来源: 互联网

标签:php wordpress woocommerce hook-woocommerce custom-taxonomy


在Woocommerce中,我正在使用YITH WooCommerce Brands plugin处理产品品牌.

目前,我在WooCommerce的简短描述下需要一个固定的文本.我想动态显示该文本中的产品名称(有效),以及产品类别名称[CATEGORY_NAME]和品牌名称[BRAND_NAME].

但我似乎无法使这些工作.

基于此答案的线程:Add Text under Single Product Short Description in 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
}

有什么想法可以在自定义文本中显示产品类别名称和产品品牌名称吗?

解决方法:

下面的代码将在产品简短描述之后输出带有正确产品类别和正确(Yith)产品品牌的自定义文本(因此,对于YITH WooCommerce Brands插件).

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文件中.测试和工作.

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

标签:php,wordpress,woocommerce,hook-woocommerce,custom-taxonomy
来源: https://codeday.me/bug/20191013/1905052.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有