编程语言
首页 > 编程语言> > php-在单个产品页面上显示类别和品牌名称

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

作者:互联网

在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