dongyue110702 2019-05-12 07:23
浏览 258
已采纳

显示未针对Woocommerce产品变体的变化设置的产品属性

I have custom tab on each product variation with some 'variation description' static content and that I have displayed on product page as well in > product summary when I select a variation (like in this link).

And now I need to display the same on shop page in product loop (see my shop page).

OR even better to display specific attributes. I tried this code:

add_action( 'woocommerce_after_shop_loop_item', 'custom_before_title' );
function custom_before_title() {
    global $product;

    echo '<h4><b>Seizoen:</b>' . $product->get_attribute('pa_seizoen') .'</h4>';
    echo '<h4><b>Maat:</b> ' . $product->get_attribute('pa_maat') .'</h4>';
    echo '<h4><b>Tijk:</b> ' . $product->get_attribute('pa_tijk-weving') .'</h4>';
    echo '<h4><b>Vulkracht:</b> ' . $product->get_attribute('pa_fullkraft[') .'</h4>';
    echo '<h4><b>Vulkracht:</b> ' . $product->get_attribute('pa_vulling') .'</h4>';
}

But whatever I do I get same result, it displays only product attributes that are in the product variations: first two "season and size".

Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • dongren1353 2019-05-12 13:33
    关注

    You are using a plugin (or some customizations) that is displaying on your shop page (or archive pages) each product variations individually.

    So you need to get the parent variable product for other product attributes that are not set for variations (and the product description or other required related data):

    add_action( 'woocommerce_after_shop_loop_item', 'custom_before_title' );
    function custom_before_title() {
        global $product;
    
        if( $seizoen = $product->get_attribute('pa_seizoen') ) {
            echo '<h4><strong>Seizoen:</strong>' . $seizoen . '</h4>';
        }
        if( $maat = $product->get_attribute('pa_maat') ) {
            echo '<h4><strong>Maat:</strong> ' . $maat . '</h4>';
        }
    
        $parent_id = $product->get_parent_id(); // Get the parent Variable Product ID
        // Below, all Parent variable product data
        if( $parent_id > 0 && $parent_product = wc_get_product($parent_id) ) {
            if( $tijk_weving = $parent_product->get_attribute('pa_tijk-weving') ) {
                echo '<h4><strong>Tijk:</strong> ' . $tijk_weving . '</h4>';
            }
            if( $fullkraft = $parent_product->get_attribute('pa_fullkraft') ) {
                echo '<h4><strong>Vulkracht:</strong> ' . $fullkraft . '</h4>';
            }
            if( $vulling = $parent_product->get_attribute('pa_vulling') ) {
                echo '<h4><strong>Vulling:</strong> ' . $vulling . '</h4>';
            }
    
            // Display the parent variable product description
            echo '<p><strong>Beschrijving:</strong> ' . $parent_product->get_description() . '</p>';
    
            // Display the parent variable product short description
            echo '<p><strong>Korte beschrijving:</strong> ' . $parent_product->get_short_description() . '</p>';
        }  
    
        // Display the product variation description
        // echo '<p><strong>Beschrijving:</strong> ' . $product->get_description() . '</p>';
    
        // Display the product variation weight
        // echo '<p><strong>Gewicht:</strong> ' . wc_format_weight( $product->get_weight() ) . '</p>';
    
        // Display the product variation dimensions
        // echo '<p><strong>Dimensies:</strong> ' . $product->get_dimensions() . '</p>';
    }
    

    Code goes in functions.php file of your active child theme (or active theme). It should work now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化