在Woocommerce中按项目ID获取购物车项目数量

时间:2017-10-28 03:43:10

标签: php wordpress woocommerce cart product-quantity

在WooCommerce中,我创建了一个迷你购物车,我希望在当前购物车中获取商品数量。

我试过这个:

  $items = $woocommerce->cart->get_cart();

      foreach($items as $item => $values) {
          $product_id = $values['product_id'];
          if ( $product_id == $id ){
            $product_qnty = $values['quantity'];
          }
          break;
      }

是否有任何单一功能可以按购物车项目获取购物车项目数量?

1 个答案:

答案 0 :(得分:2)

您可能应该首先查看woocommerce template code cart/mini-cart.php中您将找到官方相关代码的内容。

注意:“项目ID”仅在WC_Orders项目循环中可用,但在WC_Cart中不可用,这是“购物车项目密钥”。所以你肯定在谈论产品ID。但是,如果您查看官方模板cart / mini_cart的代码,则需要使用<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.2/css/swiper.min.css"> <link rel="stylesheet" href="css/app.css"> <style> .swiper-container { width: 100%; height: 100%; } .swiper-slide { text-align: center; font-size: 18px; background: #fff; /* Center slide text vertically */ display: -webkit-box; display: -ms-flexbox; /* width: 50px; */ display: -webkit-flex; display: flex; -webkit-box-pack: center; -ms-flex-pack: center; -webkit-justify-content: center; justify-content: center; -webkit-box-align: center; -ms-flex-align: center; -webkit-align-items: center; align-items: center; } </style> <title>xyz</title> </head> <body> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide">Slide 1</div> <div class="swiper-slide">Slide 2</div> <div class="swiper-slide">Slide 3</div> <div class="swiper-slide">Slide 4</div> <div class="swiper-slide">Slide 5</div> <div class="swiper-slide">Slide 6</div> <div class="swiper-slide">Slide 7</div> <div class="swiper-slide">Slide 8</div> <div class="swiper-slide">Slide 9</div> <div class="swiper-slide">Slide 10</div> </div> <div class="swiper-button-next"></div> <div class="swiper-button-prev"></div> </div> <script src="bower_components/jquery/dist/jquery.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.2/js/swiper.min.js"></script> <!-- Initialize Swiper --> <script> var swiper = new Swiper('.swiper-container', { slidesPerView: 4, spaceBetween: 30, slidesPerGroup: 4, loop: true, loopFillGroupWithBlank: false, pagination: { el: '.swiper-pagination', clickable: true, }, navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, }); </script> </body> </html> 对象而不是WC_Product ...

因此,您始终可以构建一个自定义函数,例如(带有 $product_id 参数,WC_Product对象),您可以在相应的模板代码中使用在您的自定义代码中:

$product

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

USAGE (示例):在这里,我们将输出 function get_item_qty( $product ){ foreach( WC()->cart->get_cart() as $cart_item ) // for variable products (product varations) $product_id = $product->get_parent_id(); if( $product_id == 0 || empty( $product_id ) ) $product_id = $product->get_id(); if ( $product_id == $cart_item['product_id'] ){ return $cart_item['quantity']; // break; } return; } 的数量({{1} } object)

$product

官方文档:Template Structure + Overriding Templates via a Theme