首页 文章

Woocommerce:在商店目录页面上显示产品变化描述

提问于
浏览
1

我有类似的问题mentioned here,我希望在目录页面上显示变量产品的简短描述 .

我正在使用WooCommerce Show Single Variations商业插件,但它没有显示简短描述 .

在此之前,我使用一些代码在商店页面上显示简单产品的描述,它看起来像这样:

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>";
}

如果您告诉我如何更改此代码,我将不胜感激 .

1 回答

  • 0

    Updated :因为我没有,我不使用你的商业插件,因为你的问题不是那么清楚......有两种可能性:

    1)您希望在woocommerce存档页面(如商店......)上添加显示(使用您的插件)的产品变体的简短描述(来自父变量产品),就像其他产品一样 .

    这应该做的伎俩(未经测试,没有错误):

    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)您想在woocommerce存档页面(如商店......)上添加显示的产品变体描述(如您的插件...),就像其他产品一样......对于产品变体,简短说明没有't exist (it'空)...

    这应该做的伎俩(未经测试,没有错误):

    // 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>";
    }
    

    代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中 .

相关问题