【问题标题】:Show total price for grouped product on shop page in Woocommerce在 Woocommerce 的商店页面上显示分组产品的总价
【发布时间】:2018-08-02 09:35:07
【问题描述】:

我想显示我的分组产品的总价格,而不是价格范围。我已经用这个 sn-p 在产品页面上修复了这个问题。

如何使用此代码或解决商店页面上的问题?

global $product;
$price = $product->get_price_html();
if ( $product->get_type() == 'grouped') {
    $children = $product->get_children();
    $price = 0;
    foreach ($children as $key => $value) {
        $_product = wc_get_product( $value );
        $price += $_product->get_price();
    }
    $price = get_woocommerce_currency_symbol( '' ) . ' ' . $price;
}
?>
<p class="price"><?php echo $price; ?></p>

【问题讨论】:

    标签: php wordpress woocommerce product price


    【解决方案1】:

    尝试使用全局过滤器更改商店循环中返回的价格:

    add_filter( 'woocommerce_get_price_html', 'highest_price', 10, 2 );
    
    function highest_price( $price, $product ) {
      if ( $product->get_type() == 'grouped') {
        $children = $product->get_children();
        $price = 0;
        foreach ($children as $key => $value) {
          $_product = wc_get_product( $value );
          $price += $_product->get_price();
        }
        $price = get_woocommerce_currency_symbol( '' ) . ' ' . $price;
      }
      return $price;
    }
    

    我无法测试此解决方案,但如果它不起作用,请尝试将过滤器添加到另一个函数,而不是 woocommerce_get_price_html,例如 woocommerce_template_loop_price

    【讨论】:

      【解决方案2】:

      我测试了以下解决方案,它适用于 Woocommerce 3.6.3。在此之后,组产品显示的价格是其所有子产品的总和。

      function max_grouped_price( $price_this_get_price_suffix, $instance, $child_prices ) { 
      
          return wc_price(array_sum($child_prices)); 
      } 
      
      add_filter( 'woocommerce_grouped_price_html', 'max_grouped_price', 10, 3 );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-24
        • 1970-01-01
        • 1970-01-01
        • 2022-01-26
        • 1970-01-01
        • 2021-12-12
        • 1970-01-01
        • 2018-11-13
        相关资源
        最近更新 更多