【问题标题】:Get shopping cart details in Magento2在 Magento2 中获取购物车详细信息
【发布时间】:2016-08-15 19:23:06
【问题描述】:

我知道在 Magento 1 中,您可以通过以下方式在任何页面上获取购物车详细信息:

$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item) {
    $productId = $item->getProduct()->getId();
    $productPrice = $item->getProduct()->getPrice();
}

我如何在 Magento 2 中做同样的事情?

【问题讨论】:

    标签: magento2


    【解决方案1】:
    protected $_checkoutSession;
    
    public function __construct (
        \Magento\Checkout\Model\Session $_checkoutSession
        ) {
        $this->_checkoutSession = $_checkoutSession;
    }
    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
      $cartData = $this->_checkoutSession->getQuote()->getAllVisibleItems();
      $cartDataCount = count( $cartData );
    }
    

    可以在observer中获取报价数据

    【讨论】:

    • 这应该是公认的答案。没有 ObjectManager 的直接实例化,并且逻辑不在模板中。
    • 先生,我们如何获取主页上的购物车数据?在主页上,当我运行代码时,我在购物车页面上得到 null array,它返回购物车产品...虽然我希望这些产品出现在我的主页上
    【解决方案2】:

    最后我自己想通了:

    <?php
    
    $om =   \Magento\Framework\App\ObjectManager::getInstance();
    $cartData = $om->create('Magento\Checkout\Model\Cart')->getQuote()->getAllVisibleItems();
    $cartDataCount = count( $cartData );
    
    ?>
    <div class="bagDrop" id="bagDrop">
        <h4><a href="<?php echo $block->getShoppingCartUrl(); ?>">Quote Basket</a></h4>
        <?php if( $cartDataCount > 1 ): ?>
            <a href="#" class="arr up off" id="bagDropScrollUp"></a>
        <?php endif; ?>
        <div class="bagDropWindow">
        <?php if( $cartDataCount > 0 ): ?>
            <div class="bagDropList" id="bagDropList">
                <?php foreach( $cartData as $item ): ?>
                    <?php $product = $item->getProduct(); ?>
                    <?php $image = $product['small_image'] == '' ? '/pub/static/frontend/Clear/usb2u/en_GB/images/default-category-image_1.png' : '/pub/media/catalog/product' . $product['small_image']; ?>
                    <a href="<?php echo $product['request_path']; ?>" class="bagDropListItem">
                        <img src="<?php echo $image; ?>">
                        <p>
                            <span class="name"><?php echo $product['name']; ?></span><br>
                            <span class="qty">x <?php echo $item->getQty(); ?></span>
                        </p>
                    </a>
                <?php endforeach; ?>
            </div>
        <?php else: ?>
            <div class="emptyList">No products in your basket.</div>
        <?php endif; ?>
        </div>
        <?php if( $cartDataCount > 1 ): ?>
            <a href="#" class="arr dn" id="bagDropScrollDown"></a>
        <?php endif; ?>
    </div>
    

    【讨论】:

      【解决方案3】:

      在结帐页面中获取产品详细信息

       <?php
          namespace namespace\modulename\Block\xxx;
      
          class xxx extends \Magento\Framework\View\Element\Template {
              public function __construct(
                   \Magento\Checkout\Model\Cart $cart,
                   \namespace\modulename\Model\CrossSellFactory $crosssell,
                   \Magento\Framework\View\Element\Template\Context $context,
                   \Magento\Customer\Model\Session $customerSession,
                   \Magento\Framework\ObjectManagerInterface $objectManager,
                   array $data = []
              ) {
                   parent::__construct($context, $data);
                   $this->cart = $cart;
                   $this->_crosssell = $crosssell;
                   $this->customerSession = $customerSession;
                   $this->_objectManager = $objectManager;
              }
              public function getProductIds()
              {
                   $productInfo = $this->cart->getQuote()->getItemsCollection();
                   foreach ($productInfo as $item) {
                       $item[] = $item->getProductId();
                       echo"<pre>";print_r($item->getProductId());
                   }
                       return $item;
              } 
          }
      

      将上面的 .php 文件放入你的块中,并返回 phtml 文件中的值,如下所示。

          <?php
          $Productdetails = $block->getProductIds();
          echo"<pre>";print_r($Productdetails->getName());
          ?>
      

      【讨论】:

      • 使用此代码在 magento2 的结帐页面中获取产品详细信息
      • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。一个正确的解释would greatly improve 它的长期价值通过展示为什么这是一个很好的解决问题的方法,并将使它对未来有其他类似问题的读者更有用。请edit您的回答添加一些解释,包括您所做的假设。
      【解决方案4】:

      您可以通过实现以下代码轻松获取 Magento 2 中的购物车详细信息:

      <?php
      $object =  \Magento\Framework\App\ObjectManager::getInstance();
      $cart = $object->create('Magento\Checkout\Model\Cart')->getQuote()->getAllVisibleItems();
      $cartCount = count( $cart );
      if($cartCount > 0){
          echo $cartCount;
          } else{
              echo "0" ;
          }
          ?>
      

      【讨论】:

      • 虽然我的购物车中有商品,但它返回 0
      【解决方案5】:

      用法示例:

      1. \Magento\Checkout\Block\Cart\AbstractCart::getQuote():

        /**
         * Get active quote
         *
         * @return Quote
         */
        public function getQuote()
        {
            if (null === $this->_quote) {
                $this->_quote = $this->_checkoutSession->getQuote();
            }
            return $this->_quote;
        }
        
      2. \Magento\Checkout\Block\Cart\Totals::getQuote():

        /**
          * Get active or custom quote
          *
          * @return \Magento\Quote\Model\Quote
          */
        public function getQuote()
        {
            if ($this->getCustomQuote()) {
                return $this->getCustomQuote();
            }
        
            if (null === $this->_quote) {
                $this->_quote = $this->_checkoutSession->getQuote();
            }
            return $this->_quote;
        }
        
      3. \Magento\Checkout\Helper\Cart::getQuote():

        /**
         * Retrieve current quote instance
         *
         * @return \Magento\Quote\Model\Quote
         * @codeCoverageIgnore
         */
        public function getQuote()
        {
            return $this->_checkoutSession->getQuote();
        }
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-29
        • 2019-09-23
        • 2012-08-30
        • 1970-01-01
        • 2015-04-19
        • 2018-09-14
        • 2014-08-14
        • 1970-01-01
        相关资源
        最近更新 更多