【问题标题】:WooCommerce - get number of different products in cartWooCommerce - 获取购物车中不同产品的数量
【发布时间】:2016-05-17 10:52:08
【问题描述】:

WC()->cart->get_cart_contents_count() 表示 WooCommerce 购物车中的产品总数。

但是您如何获得不同产品的数量?

例如:

  • 3 * 产品 A
  • 2 * 产品 B

WC()->cart->get_cart_contents_count() 将返回 5。 但我们想得到 2。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    您可以简单地使用count(WC()->cart->get_cart());,因为get_cart() 返回一个项目数组。

    Documentation

    【讨论】:

      【解决方案2】:
      $cart = $_SESSION['wfcart'];
      $count=0
      foreach($cart->items as $item){
          $count+= $cart->itemqtys[$item];
      }
      

      【讨论】:

      • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
      猜你喜欢
      • 2021-05-16
      • 2017-04-27
      • 1970-01-01
      • 2021-07-19
      • 2018-05-18
      • 2018-02-17
      • 1970-01-01
      • 2014-05-13
      • 2015-03-31
      相关资源
      最近更新 更多