Display category and brand name on single product page

前端 未结 1 1410
没有蜡笔的小新
没有蜡笔的小新 2021-01-25 10:24

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

I\'m currently struggling with a fixed text That I want under my short description i

1条回答
  •  -上瘾入骨i
    2021-01-25 10:33

    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 = ''.__("klantenservice").'';
    
        // 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 );
    
        ?>
        

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

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

    0 讨论(0)
提交回复
热议问题