在Magento2中获取购物车详细信息
我知道在Magento 1中,您可以通过以下方式在任何页面上获取购物车详细信息:在Magento2中获取购物车详细信息,magento2,Magento2,我知道在Magento 1中,您可以通过以下方式在任何页面上获取购物车详细信息: $cart = Mage::getModel('checkout/cart')->getQuote(); foreach ($cart->getAllItems() as $item) { $productId = $item->getProduct()->getId(); $productPrice = $item->getProduct()->getPrice
$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item) {
$productId = $item->getProduct()->getId();
$productPrice = $item->getProduct()->getPrice();
}
如何在Magento 2中执行相同的操作?用法示例:
/**
*获取活动报价
*
*@返回报价
*/
公共函数getQuote()
{
如果(null==$this->\u引号){
$this->_quote=$this->_checkoutSession->getQuote();
}
返回$this->\u报价;
}
/**
*获取活动报价或自定义报价
*
*@return\Magento\Quote\Model\Quote
*/
公共函数getQuote()
{
如果($this->getCustomQuote()){
返回$this->getCustomQuote();
}
如果(null==$this->\u引号){
$this->_quote=$this->_checkoutSession->getQuote();
}
返回$this->\u报价;
}
/**
*检索当前报价实例
*
*@return\Magento\Quote\Model\Quote
*@codeCoverageIgnore
*/
公共函数getQuote()
{
返回$this->u checkoutSession->getQuote();
}
最后我自己想出来了:
<?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>
篮子里没有产品。
您可以在observer中获取报价数据通过实现下面提到的代码,您可以轻松获取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" ;
}
?>
在结账页面获取产品详细信息
<?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;
}
}
这应该是公认的答案。没有ObjectManager的直接实例化,并且逻辑不在模板中。先生,我们如何在主页上获取购物车数据??在主页上,我得到空数组,而在购物车页面上,当我运行代码时,它会将购物车产品返回给我。。。虽然我希望这些产品出现在我的主页上,但请使用此代码在magento2Thank的签出页面中获取产品详细信息,以获取此代码片段,这可能会提供一些有限的即时帮助。通过说明为什么这是一个很好的问题解决方案来正确解释它的长期价值,并将使它对未来有其他类似问题的读者更有用。请在您的答案中添加一些解释,包括您所做的假设。尽管我的购物车中有商品,但它返回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
$Productdetails = $block->getProductIds();
echo"<pre>";print_r($Productdetails->getName());
?>