2017-11-16 1 views
1

Ich habe ein ähnliches Problem mentioned here und ich möchte, dass eine kurze Beschreibung des variablen Produkts auf der Katalogseite angezeigt wurde.Woocommerce: Anzeige Produktvariationsbeschreibung auf Shop-Katalogseite

Ich benutze WooCommerce Show Single Variations kommerzielle pluginn, aber es zeigt keine kurze Beschreibung.

Davor habe ich einige Code Beschreibung von einfachen Produkt auf Shop-Seite anzuzeigen, die gearbeitet und es sieht wie folgt aus:

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]*[email protected]', '\\1 ...', $text); 
     } 
echo "<span class='catalog-short-desc'>$text</span>"; 
} 

Ich wäre dankbar, wenn Sie mir sagen, wie Sie diesen Code zu ändern.

Antwort

0

aktualisiert: Da ich nicht und ich nicht Ihre kommerzielle Plug-in verwenden und wie Ihre Frage nicht so klar ist ... Es gibt 2 Möglichkeiten:

1) Sie möchten die kurze Beschreibung hinzufügen (aus dem übergeordneten variablen Produkt) für die Produktvarianten, die (mit Ihrem Plugin) auf Woocommerce-Archivseiten (wie Shop ...) angezeigt werden, genau wie die anderen Produkte.

Dies sollte den Trick (ungetestet ohne Fehler) tun:

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]*[email protected]', '\\1 ...', $text); 
    } 
    echo "<span class='catalog-short-desc'>$text</span>"; 
} 

2) Sie möchten die Beschreibung der Produktvarianten hinzuzufügen, die (mit Plugin) auf WooCommerce Archiven Seiten angezeigt werden (wie Shop ...), ebenso wie die anderen Produkte ... Für Produktvariation kurze Beschreibung nicht vorhanden (es leer ist) ...

Dies sollte den Trick (ungetestet ohne Fehler) tun:

// 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]*[email protected]', '\\1 ...', $text); 
    } 
    echo "<span class='catalog-short-desc'>$text</span>"; 
} 

-Code geht in function.php Datei Ihres aktiven Kind Thema (oder Thema) oder auch in jeder Plugin-Datei.

+0

Vielen Dank! Sorry für die unklare Frage und mein Englisch))) – deft1z

+1

Der erste Trick funktionierte für mich. Die zweite - hat nicht funktioniert. In Woocommerce habe ich Beschreibung Felder zu allen einzelnen Variationen separat - aber Beschreibung aus dem übergeordneten Produkt - wird auch für die Katalogseite tun))) Noch einmal, sehr, sehr großes Dankeschön an Sie. – deft1z

Verwandte Themen