1

I have a similar problem mentioned here and I want that a short description of the variable product was displayed on the catalog page.

I'm using WooCommerce Show Single Variations commercial pluginn, but it doesn't display short description.

Before that I used some code to display description of simple product on shop page that worked and it looks like this:

add_action( 'woocommerce_after_shop_loop_item_title', 
'add_short_description', 9 );
function add_short_description() {
global $post;
$text = $post->post_excerpt;
$maxchar = 75; //максимальное кол-во символов

$text = preg_replace ('~\[[^\]]+\]~', '', $text ); //убираем шорткоды

//удаляем все html символы
//$text = strip_tags( $text);

// Обрезаем
if ( mb_strlen( $text ) > $maxchar ){
            $text = mb_substr( $text, 0, $maxchar );
            $text = preg_replace('@(.*)\s[^\s]*$@s', '\\1 ...', $text );
        }
echo "<span class='catalog-short-desc'>$text</span>";
}

I would be grateful if you tell me how to change this code.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
deft1z
  • 13
  • 1
  • 5

1 Answers1

0

Updated: As I dont have and I don't use your commercial plugin and as your question is not so clear… There is 2 possibilities:

1) You would like to add the short description (from the parent variable product) for the product variations that are displayed (with your plugin) on woocommerce archives pages (like shop …), just like the other products.

This should do the trick (untested with no errors):

add_action( 'woocommerce_after_shop_loop_item_title', 'add_short_description', 9 );
function add_short_description() {
    global $post;

    // 1. Product variation
    if ($post->post_type = 'product_variation'){
        $post_parent_id = $post->post_parent; // Get the parent variable product ID
        $post_parent = get_post( $post_parent_id ); // Get the parent variable WP_Post object
        $text = $post_parent->post_excerpt; // Get the short description from the parent variable product
    } 
    // 2. Other cases (simple, variable …)
    else {
        $text = $post->post_excerpt;
    }

    $maxchar = 75; // Maximum number of characters

    $text = preg_replace ('~\[[^\]]+\]~', '', $text ); // Remove shortcodes

    // Remove all html symbols
    //$text = strip_tags( $text);

    // Crop
    if ( mb_strlen( $text ) > $maxchar ){
        $text = mb_substr( $text, 0, $maxchar );
        $text = preg_replace('@(.*)\s[^\s]*$@s', '\\1 ...', $text );
    }
    echo "<span class='catalog-short-desc'>$text</span>";
}

2) You would like to add the description of the product variations that are displayed (with your plugin) on woocommerce archives pages (like shop …), just like the other products… For Product variation short description doesn't exist (it's empty)

This should do the trick (untested with no errors):

// For Woocommerce versions 3+ only
add_action( 'woocommerce_after_shop_loop_item_title', 'add_short_description', 9 );
function add_short_description() {
    global $post, $product;

    // 1. Product variation
    if ( $product->is_type('product_variation') ){
        // Get the product variation description
        // short description doesn't exist for product variations
        $text = $product->get_description();

        // If the product variation description is empty, we get the parent short description
        if( empty( $text ) ){
            $parent_id = $product->get_parent_id(); // The parent variable product ID
            $parent_product = wc_get_product( $parent_id ); // The parent WC_Product object
            $text = $parent_product->get_short_description();
        }
    }
    // 2. Other cases (simple, variable …)
    else {
        $text = $product->get_short_description();
    }

    $maxchar = 75; // Maximum number of characters

    $text = preg_replace ('~\[[^\]]+\]~', '', $text ); // Remove shortcodes

    // Remove all html symbols
    //$text = strip_tags( $text);

    // Crop
    if ( mb_strlen( $text ) > $maxchar ){
        $text = mb_substr( $text, 0, $maxchar );
        $text = preg_replace('@(.*)\s[^\s]*$@s', '\\1 ...', $text );
    }
    echo "<span class='catalog-short-desc'>$text</span>";
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank You so much! Sorry for the unclear question and my English ))) – deft1z Nov 17 '17 at 07:49
  • 1
    The first trick worked for me. The second - didn't work. In woocommerce I have description fields to all single variations separately - but discription from the parent product - will do too for the catalog page))) Once again, very very big thanx to You. – deft1z Nov 17 '17 at 07:56