在Magento2中获取购物车详细信息

时间:2016-04-22 08:46:49

标签: magento2

我知道在Magento 1中,您可以在任何页面上获取购物车详情:

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

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

5 个答案:

答案 0 :(得分:12)

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 );
}

您可以在观察者

中获取报价数据

答案 1 :(得分:7)

我最后自己想出了这个:

<?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>

答案 2 :(得分:0)

您可以通过实施以下提及的代码轻松获取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" ;
    }
    ?>

答案 3 :(得分:0)

在结帐页面获取产品详情

 <?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());
    ?>

答案 4 :(得分:-2)

用法示例 1. \ Magento \ Checkout \ Block \ Cart \ AbstractCart :: getQuote() github.com 的magento / magento2 /斑点/ 958164 /应用程序/代码/ Magento的/结帐/砌块/车/ AbstractCart.php#L101-L112

/**
 * Get active quote
 *
 * @return Quote
 */
public function getQuote()
{
    if (null === $this->_quote) {
        $this->_quote = $this->_checkoutSession->getQuote();
    }
    return $this->_quote;
}
  1. \ Magento的\结帐\块\车\总计::的getQuote() github.com 的magento / magento2 /斑点/ 958164 /应用程序/代码/ Magento的/结帐/砌块/车/ Totals.php#L190-L205

    / **

    • 获取有效或自定义报价 *
    • @return \ Magento \ Quote \ Model \ Quote * / 公共函数getQuote() { if($ this-&gt; getCustomQuote()){     return $ this-&gt; getCustomQuote(); }

      if(null === $ this-&gt; _quote){     $ this-&gt; _quote = $ this-&gt; _checkoutSession-&gt; getQuote(); } 返回$ this-&gt; _quote; }

  2. \ Magento的\结帐\助手\车::的getQuote() github.com 的magento / magento2 /斑点/ 958164 /应用程序/代码/ Magento的/结帐/助手/ Cart.php#L146-L155

    / **

    • 检索当前报价实例 *
    • @return \ Magento \ Quote \ Model \ Quote
    • @codeCoverageIgnore * / 公共函数getQuote() { 返回$ this-&gt; _checkoutSession-&gt; getQuote(); }