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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。