dongyumiao5210 2018-09-12 08:21
浏览 75
已采纳

在Woocommerce 3中获取并显示可变的产品价格范围

I am very new to wordpress and woocommerce. I am modifying the twenty seventeen theme's search result page to look like a table. Most of the products are variable products.I am using the code as below to show the results in a table

    <table class="search-res" style="table-layout: auto; width: 100%;">
            <tr><td>
            <?php
            if ( has_post_thumbnail()) 
                the_post_thumbnail('excerpt-thumb');        
                ?></td>
                <td>
                    <?php 
                    the_title( sprintf( '<th class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ) );
                    echo"</a></th>";
                    ?>
                </td>
                <td style="font-style:italic;font-size:small;"><?php the_excerpt(); ?></td>
            <th><?php 
                global $post;
   $product = new WC_Product($post->ID ); 
  echo wc_price($product->get_price_including_tax(1,$product->get_price()));
?></th>
            </tr>

        </table>

The problem that I am facing is here the minimum price of the product is gettiong displayed. Instead I want the price range to show up. Also how can I get rating and category attributes of the product to show up in the table?

  • 写回答

2条回答 默认 最新

  • douhui3330 2018-09-12 18:47
    关注

    You just need to use the WC_Product_Variable get_price_html() method that will do it for you:

    <?php
    global $post;
    
    // Get the WC_Product_Variable instance Object
    $product = wc_get_product( $post->ID ); // Works for any product type
    
    // Displaying the formatted "Min" - "Max" price range
    echo $product->get_price_html();
    ?>
    

    Tested and works

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改