Question

I am working in magento 1.9.2.2. I am trying to add cart pop up and list the items added in the cart without using any extension.the pop up is appearing successfully. I dont exactly know how to list the items added to the cart with its name, price and items image in the pop up. I just wanted to know how to retrieve the item collection. My code which is not working:

<div id="header-cart1new" class="block block-cartnew skip-content">
            <div class="ui-popover__pointer" style="left: 82%;"></div>
            <div id="header-cart1" class="block block-cart11 skip-content">
              <?php echo $this->getChildHtml('cart_sidebar'); ?>

                <?php $cartitem = Mage::getModel('checkout/cart')->getQuote();
                <?php if(count($cartitem)> '0'){
                    foreach ($cartitem->getAllItems() as $pro) {
                        $productId = $pro->getProductId();
                        $product = Mage::getModel('catalog/product')->load($productId);
                        $productName = $product->getName();
                        $productPrice = $product->getPrice(); 
                } 
                } ?>

     <?php else: ?>
    <p class="empty"> 
    <div class="text-center">
                <h3 class="mini-cart-empty fontsize18 no-margin-btm">
                         Your Shopping Cart is empty.
               </h3>
               <h4 class="mini-cart-empty fontsize15">
                What are you waiting for? Start shopping!
                </h4>
    </div>
    </p>
<?php endif ?>
            </div>
        </div>
        </div>
Était-ce utile?

La solution

Try this code

<?php $session= Mage::getSingleton('checkout/session');
    $items = $session->getQuote()->getAllItems();
?>
<div id="header-cart1new" class="block block-cartnew skip-content">
<div class="ui-popover__pointer" style="left: 82%;"></div>
<div id="header-cart1" class="block block-cart11 skip-content">
<?php if(count($items)): ?>
<?php foreach($items as $_item): ?>
    <?php $isVisibleProduct = $_item->getProduct()->isVisibleInSiteVisibility();?>
    <?php $product = $_item->getProduct(); ?>
    <a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(50, 50); ?>" width="50" height="50" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" /></a>
    <span><?php echo $_item->getName();?></span>
    <span><?php echo $_item->getQty();?></span>
    <span><?php echo $this->helper('checkout')->formatPrice($_item->getCalculationPrice()+$_item->getWeeeTaxAppliedAmount()+$_item->getWeeeTaxDisposition()); ?></span>
<?php endforeach; ?>
<?php else: ?>
<p class="empty"> 
    <div class="text-center">
        <h3 class="mini-cart-empty fontsize18 no-margin-btm"><?php echo $this->__("Your Shopping Cart is empty.")?></h3>
        <h4 class="mini-cart-empty fontsize15"><?php echo $this->__("What are you waiting for? Start shopping!") ?> </h4>
    </div>
    </p>
<?php endif ?>
</div>
</div>
</div>

Autres conseils

you can use sidebar cart in popup. use below it will show all item in cart.

<?php
              echo $this->getLayout()
              ->createBlock('checkout/cart_sidebar')
              ->setTemplate('checkout/cart/sidebar.phtml')
              ->toHtml(); ?>

or you get it programtically

$cartitem = Mage::getModel('checkout/cart')->getQuote();
foreach ($cartitem->getAllItems() as $pro) {
    $productId = $pro->getProductId();
    $product = Mage::getModel('catalog/product')->load($productId);
   echo $productName = $product->getName();
    echo $productPrice = $product->getPrice();
}

update on your code

<?php $cartitem = Mage::getModel('checkout/cart')->getQuote();
                <?php if(count($cartitem)){
                    foreach ($cartitem->getAllItems() as $pro) {
                        $productId = $pro->getProductId();
                        $product = Mage::getModel('catalog/product')->load($productId);
                        echo $productName = $product->getName();
                        echo $productPrice = $product->getPrice(); 
                } 
                } ?>
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top