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

时间:2018-07-13 12:24:43

标签: 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
}

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

1 个答案:

答案 0 :(得分:1)

下面的代码将在产品简短描述之后为您输出自定义文本,其中包含正确的产品类别和正确的(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文件中。经过测试并可以正常工作。

  

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