Magento – How to i do cross sell products blocks on add to cart popup box

addtocartcartcross-sellsmagento-1.9popup

How can i display my cross sell products blocks under add to cart popup box,

my add to cart block,

<?php

class Cmsmart_AjaxCart_IndexController extends Mage_Core_Controller_Front_Action
{
    public function _constructor(){
        $_SERVER['REQUEST_URI'] = array_pop(explode('web.com', @$_SERVER['HTTP_REFERER']));
    }

    protected function _getCart()
    {
        return Mage::getSingleton('checkout/cart');
    }

    protected function _getSession()
    {
        return Mage::getSingleton('checkout/session');
    }

    protected function _updateShoppingCart()
    {
        try {
            $cartData = $this->getRequest()->getParam('cart');
            if (is_array($cartData)) {
                $filter = new Zend_Filter_LocalizedToNormalized(
                    array('locale' => Mage::app()->getLocale()->getLocaleCode())
                );
                foreach ($cartData as $index => $data) {
                    if (isset($data['qty'])) {
                        $cartData[$index]['qty'] = $filter->filter(trim($data['qty']));
                    }
                }
                $cart = $this->_getCart();
                if (! $cart->getCustomerSession()->getCustomer()->getId() && $cart->getQuote()->getCustomerId()) {
                    $cart->getQuote()->setCustomerId(null);
                }

                $cartData = $cart->suggestItemsQty($cartData);
                $cart->updateItems($cartData)
                    ->save();
            }
            $this->_getSession()->setCartWasUpdated(true);
        } catch (Mage_Core_Exception $e) {
            $this->_getSession()->addError(Mage::helper('core')->escapeHtml($e->getMessage()));
        } catch (Exception $e) {
            $this->_getSession()->addException($e, $this->__('Cannot update shopping cart.'));
            Mage::logException($e);
        }
    }

    public function indexAction(){
        $delete = $this->getRequest()->getParam('delete');
        $kiemtra = true;

        $update = $this->getLayout()->getUpdate();
        $update->addHandle('default');
        $this->addActionLayoutHandles();
        if(!$delete){
            $product = $this->_getProductFromUrl();
            $product = Mage::registry('product');
            $options = $product->getProductOptionsCollection();
            $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($product);
            $col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
            //$kiemtra = (!$product->getTypeInstance(true)->hasRequiredOptions($product) || $this->getRequest()->getParam('product') || count($col) == 0);
            //$kiemtra = ($product->getTypeId() == 'simple'  || $this->getRequest()->getParam('product') || $delete) && !$product->getTypeInstance(true)->hasRequiredOptions($product); 
            $kiemtra = $delete || $this->getRequest()->getParam('product') || !$product->getTypeInstance(true)->hasRequiredOptions($product);
            if($kiemtra) {
                self::tryaddAction($product, $_GET);
            }

            Mage::dispatchEvent('catalog_controller_product_view', array('product'=>$product));
            if ($this->getRequest()->getParam('options')) {
                $notice = $product->getTypeInstance(true)->getSpecifyOptionMessage();
                Mage::getSingleton('catalog/session')->addNotice($notice);
            }
            if(@$product) Mage::getSingleton('catalog/session')->setLastViewedProductId($product->getId());

            if(@$product){
                $update->addHandle('PRODUCT_TYPE_'.$product->getTypeId());
                $update->addHandle('PRODUCT_'.$product->getId());
            }
        }else{
            $update->addUpdate('<remove name="product.info"/>');    
            $kiemtra = true;
            if($delete == 'all'){
                 self::_emptyShoppingCart();
            }else{
                if($delete != 'udaj') { $this->removeAction($delete); }else {
                    if(@$_POST['cart']) $this->_updateShoppingCart();
                        //foreach(@$_POST['cart'] as $k => $v){
                    //  $this->udpateAction($k, $v);
                }
            }
        }

        $this->loadLayoutUpdates();
        $this->generateLayoutXml()->generateLayoutBlocks();

        $json_encode = array();
        $json_encode['ajaxcart'] = '';
        $json_encode['ajaxsidebar'] = '';
        $json_encode['ajaxSummaryCount'] = '';
        $json_encode['ajaxCountItem'] = '';
        $json_encode['ajaxcartmsg'] = '';
        $json_encode['ajaxcontinue'] = '';

        if($kiemtra) {
        // Show the message add to cart is success          
        if(@$product) $json_encode['ajaxcontinue'] = '
        <div>
            <ul class="messages ajaxcart-messages">
                <li class="success-msg">
                    <ul>
                        <li><span><a title="'.@$product->getName().'" href="'.@$product->getProductUrl().'">'.@$product->getName().' </a> '.$this->__('has been added to cart').'.</span></li>






                        <li><button onclick="setLocation(\''.Mage::getBaseUrl().'checkout/\')" class="button btn-continue" title="'.$this->__('Checkout').'" type="button"><span><span>'.$this->__('Checkout').'</span></span></button>
                    </ul>
                </li>
            </ul>
        </div>';

        //<button class="button btn-continue closemsg" title="Continue Shopping" type="button"><span><span>Continue Shopping</span></span></button>
            //<button onclick="setLocation(\''.Mage::getBaseUrl().'checkout/onepage/\')" class="button btn-continue" title="'.$this->__('Proceed to Checkout').'" type="button"><span><span>'.$this->__('Proceed to Checkout').'</span></span></button>

        // Drop Down Cart Block 
            $block = $this->getLayout()->createBlock('checkout/cart_sidebar')->setBlockId('block-id-in-magento')->setTemplate('checkout/cart/sidebar.phtml');
            //$this->getLayout()->getBlock('content')->append($block);
            $count = Mage::helper('checkout/cart')->getSummaryCount();
            if ($count == 1) {
                $text = $this->__('My Cart (%s item)', $count);
            } elseif ($count > 0) {
                $text = $this->__('My Cart (%s items)', $count);
            } else {
                $text = $this->__('My Cart');
            }
            $json_encode['ajaxCountItem'] = $count;
            $json_encode['ajaxSummaryCount'] = $text;
            $json_encode['ajaxsidebar'] = str_replace('class="block block-cart"', 'id="cart-ajax-sidebar"', @$block->toHtml());

            $block2 = $this->getLayout()->createBlock('checkout/cart_sidebar')
                ->setTemplate('cmsmart/ajaxcart/view/sidebar.phtml')
                ->addItemRender('simple', 'checkout/cart_item_renderer', 'cmsmart/ajaxcart/view/sidebar/default.phtml')
                ->addItemRender('grouped', 'checkout/cart_item_renderer', 'cmsmart/ajaxcart/view/sidebar/default.phtml')
                ->addItemRender('configurable', 'checkout/cart_item_renderer', 'cmsmart/ajaxcart/view/sidebar/default.phtml');

            $json_encode['ajaxcart'] = @$block2->toHtml();

        }else{
            $json_encode['ajaxcartmsg'] = $this->getLayout()->getBlock('content')->toHtml();
        }
        echo json_encode($json_encode);
    }

    private function tryaddAction($product, $params = array()){     

        $cart = Mage::getModel("checkout/cart");                            
        $cart->addProduct($product->getId(), $params);                          
        return $cart->save();
    }

    private function udpateAction($itemId, $qty){
        $qty = (int)$qty?(int)$qty:1;
        $cartHelper = Mage::getSingleton('checkout/cart');
        $cartHelper->updateItem($itemId, $qty);
        //$items = $cartHelper->getCart()->getItems();
//      foreach ($items as $item) {
//          if ($item->getId() == $itemId) {
//              $item->setQty($qty);
//          }
//      }
        $cartHelper->save(); 
        Mage::getSingleton('checkout/session')->setCartWasUpdated(true);                
    }

    private function removeAction($itemId){
        try{
            $cartHelper = Mage::helper('checkout/cart');
            $cartHelper->getCart()->removeItem($itemId)->save();
        }catch (Mage_Core_Exception $exception) {}
    }

    private function emptyAction(){
        $cartHelper = Mage::helper('checkout/cart');
        $items = $cartHelper->getCart()->getItems();
        foreach ($items as $item) {
            $cartHelper->getCart()->removeItem($item->getItemId());
        }
        $cartHelper->getCart()->save();               
    }

    protected function _emptyShoppingCart()
    {
        try {
            $this->_getCart()->truncate()->save();
            $this->_getSession()->setCartWasUpdated(true);
        } catch (Mage_Core_Exception $exception) {
            $this->_getSession()->addError($exception->getMessage());
        } catch (Exception $exception) {
            $this->_getSession()->addException($exception, $this->__('Cannot update shopping cart.'));
        }
    }

    public function _getProductFromUrl(){
        Mage::dispatchEvent('catalog_controller_product_init_before', array('controller_action'=>$this));
        $product_id = $this->getRequest()->getParam('product');
        if(!$product_id):
            $path  = $this->getRequest()->getParam('id');
            $product_id = (int) $path;

            if(!$product_id){

                $path[0] == "\/" ? $path = substr($path, 1, strlen($path)) : $path;
                $tableName = Mage::getSingleton('core/resource')->getTableName('core_url_rewrite'); 
                $write = Mage::getSingleton('core/resource')->getConnection('core_write');

                $rs = $write->query('select `product_id` from `'.$tableName.'` where `request_path` = "'.$path.'"');

                if ($row = $rs->fetch() ) {
                    $product_id = $row['product_id'];
                }   
            }
        endif;  

        if (!$product_id) {
            return false;
        }

        $product = Mage::getModel('catalog/product')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->load($product_id);

        if (!Mage::helper('catalog/product')->canShow($product)) {
            return false;
        }
        if (!in_array(Mage::app()->getStore()->getWebsiteId(), $product->getWebsiteIds())) {
            return false;
        }

        $category = null;
        if ($categoryId) {
            $category = Mage::getModel('catalog/category')->load($categoryId);
            $product->setCategory($category);
            Mage::register('current_category', $category);
        }
        elseif ($categoryId = Mage::getSingleton('catalog/session')->getLastVisitedCategoryId()) {
            if ($product->canBeShowInCategory($categoryId)) {
                $category = Mage::getModel('catalog/category')->load($categoryId);
                $product->setCategory($category);
                Mage::register('current_category', $category);
            }
        }
        Mage::register('current_product', $product);
        Mage::register('product', $product);

        try {
            Mage::dispatchEvent('catalog_controller_product_init', array('product'=>$product));
            Mage::dispatchEvent('catalog_controller_product_init_after', array('product'=>$product, 'controller_action' => $this));
        } catch (Mage_Core_Exception $e) {
            Mage::logException($e);
            return false;
        }

        return $product;
    }


}

my cross sell products block,

template path :

app\design\frontend\default\theme-name\template\checkout\cart

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2013 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php
/**
 * Cart cross sell items template
 *
 * @see Mage_Checkout_Block_Cart_Crosssell
 */
?>
<?php if($this->getItemCount()): ?>
<div class="crosssell">
    <h2><?php echo $this->__('Based on your selection, you may be interested in the following items:') ?></h2>
    <ul id="crosssell-products-list">
    <?php foreach ($this->getItems() as $_item): ?>
        <li class="item">
            <a class="product-image" href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->escapeHtml($_item->getName()) ?>"><img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(110); ?>" alt="<?php echo $this->escapeHtml($_item->getName()) ?>" /></a>
            <div class="product-details">
                <h3 class="product-name"><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $this->escapeHtml($_item->getName()) ?></a></h3>
                <?php echo $this->getPriceHtml($_item, true) ?>
                <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_item) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                <ul class="add-to-links">
                    <?php if ($this->helper('wishlist')->isAllow()) : ?>
                        <li><a href="<?php echo $this->getAddToWishlistUrl($_item) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                    <?php endif; ?>
                    <?php if($_compareUrl=$this->getAddToCompareUrl($_item)): ?>
                        <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                    <?php endif; ?>
                </ul>
            </div>
        </li>
    <?php endforeach; ?>
    </ul>
    <script type="text/javascript">decorateList('crosssell-products-list', 'none-recursive')</script>
</div>
<?php endif; ?>


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

magento default path :

app\design\frontend\base\default\template\checkout\cart

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php
/**
 * Cart cross sell items template
 *
 * @see Mage_Checkout_Block_Cart_Crosssell
 */
?>
<?php if($this->getItemCount()): ?>
<div class="crosssell">
    <h2><?php echo $this->__('Based on your selection, you may be interested in the following items:') ?></h2>
    <ul id="crosssell-products-list">
    <?php foreach ($this->getItems() as $_item): ?>
        <li class="item">
            <a class="product-image" href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->escapeHtml($_item->getName()) ?>"><img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(75); ?>" width="75" height="75" alt="<?php echo $this->escapeHtml($_item->getName()) ?>" /></a>
            <div class="product-details">
                <h3 class="product-name"><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $this->escapeHtml($_item->getName()) ?></a></h3>
                <?php echo $this->getPriceHtml($_item, true) ?>
                <button type="button" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Add to Cart')) ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_item) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                <ul class="add-to-links">
                    <?php if ($this->helper('wishlist')->isAllow()) : ?>
                        <li><a href="<?php echo $this->getAddToWishlistUrl($_item) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                    <?php endif; ?>
                    <?php if($_compareUrl=$this->getAddToCompareUrl($_item)): ?>
                        <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                    <?php endif; ?>
                </ul>
            </div>
        </li>
    <?php endforeach; ?>
    </ul>
    <script type="text/javascript">decorateList('crosssell-products-list', 'none-recursive')</script>
</div>
<?php endif; ?>

enter image description here

Best Answer

Hi You can do that by adding crosssell block html in you ajax response in your controller ajax action.

$this->loadLayout();
$block = $this->getLayout()->createBlock('checkout/cart_crosssell')->setTemplate('checkout/cart/crosssell.phtml');
$response['crosssell'] = $block->toHtml();
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));

Now you can access this response crosssell block html in Ajax success block and can add it in popup html.

I Hope this will be helpful to you.

Related Topic