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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么