Saturday 14 July 2012

Magento 1.7 to 1.9 Add To Cart Multi Products

<!--  copy this file and past your list.phtml in catalog/product   -->

<?php
    $_productCollection=$this->getLoadedProductCollection();
    $_helper = $this->helper('catalog/output');
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
    <?php echo $this->getToolbarHtml() ?>

 <!--  <button class="button btn-allcart" type="button" title="Add all to cart" onclick="AddAllToCart();"> -->

    <?php // List mode ?>
    <?php if($this->getMode()!='grid'): ?>
    <?php $_iterator = 0; ?>
    <ol class="products-list" id="products-list">
    <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="AddAllToCart();"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
          <button onclick="AddAllToCart();" class="button btn-cart" type="button" title="Add all to cart">
<span>
<span>Add all to cart</span>
</span>
</button>

    <?php foreach ($_productCollection as $_product): ?>
    <?php echo $_product->getId();?>
        <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
            <?php // Product Image ?>
            <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(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
            <?php // Product description ?>
            <div class="product-shop">
                <div class="f-fix">
                    <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
                    <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h2>
                    <?php if($_product->getRatingSummary()): ?>
                    <?php echo $this->getReviewsSummaryHtml($_product) ?>
                    <?php endif; ?>
                    <?php echo $this->getPriceHtml($_product, true) ?>
                    <?php if($_product->isSaleable()): ?>
                 
                    <fieldset class="add-to-cart-box">
                        <input type="hidden" name="products[]" value="<?php echo $_product->getId() ?>" />
                        <legend><?php echo $this->__('Add Items to Cart') ?></legend>
                        <span class="qty-box"><label for="qtybox_id_<?php echo $_product->getId() ?>"><?php echo $this->__('Qty') ?>:</label>
                        <!--<input name="qty<?php echo $_product->getId() ?>" type="text" class="input-text qty" id="qtybox_id_<?php echo $_product->getId() ?>" maxlength="12" value="" />--></span>
                       <!--<input type="checkbox" class="input-checkbox add" name="add_<?php echo $_iterator; ?>" id="add_<?php echo $_iterator; ?>" />-->
                     

<!------------------------------------------------- Start 1st part Add to cart Multiple products ------------------------------------------------->

<input type="hidden" class="add" name="url_<?php echo $_iterator; ?>" id="url_<?php echo $_iterator; ?>" value="<?php echo $this->getAddToCartUrl($_product) ?>" />
<?php if(!$_product->isGrouped()): ?>
     <input type="text" class="input-text qty" name="qty_<?php echo $_iterator; ?>" id="qty_<?php echo $_iterator; ?>" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" />
<!----------------------------------------------- End 1st part Add to cart Multiple products ------------------------------------------------------->


<?php endif; ?>

                    </fieldset>
                       <!-- <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
                       -->
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    <div class="desc std">
                        <?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                        <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped ?>" class="link-learn"><?php echo $this->__('Learn More') ?></a>
                    </div>
                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                            <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>
            </div>
        </li>
     
<!------------------------------------------------- Start 2nd part Add to cart Multiple products ------------------------------------------------->
     
        <script type="text/javascript">
    function processNext(urls, i) {
        var next = i + 1;
        $('processing-text').update('Processing item ' + next);
        if (next < urls.size()) {
            new Ajax.Request(urls[i], {
              method: 'get',
              onComplete: function(transport) {
                processNext(urls, next);
              }
            });
        } else {
            new Ajax.Request(urls[i], {
              method: 'get',
              onComplete: function(transport) {
                window.location.href='checkout/cart'
              }
            });
        }
    }

    function addItemsToCart() {
        $('add-items-to-cart').hide();
        $('waiting').show();

        var addToCartUrls = [];
        $$('input.add').each(function(e){
var id = e.readAttribute('id').split('_')[1];
//alert(id);
var qty = Number($('qty_' + id).value);
            if(qty > 0) {
//alert(qty);
                addToCartUrls.push($('url_' + id).value + 'qty/' + qty);
            }
        });

        if (addToCartUrls.size() > 0) {
            processNext(addToCartUrls, 0);
        } else {
            $('add-items-to-cart').show();
            $('waiting').hide();
            alert('Please add quantity for product then click add to cart button');
        }
    }
</script>
<!----------------------------------------------- End 2nd part Add to cart Multiple products ------------------------------------------------------->

    <?php endforeach; ?>
<!------------------------------------------------- Start 3rd part Add to cart Multiple products ------------------------------------------------->
 
    <div style="margin-bottom:5px; text-align:right;">
    <button id="add-items-to-cart" class="form-button" onclick="addItemsToCart()"><span><?php echo $this->__('Add Items to Cart') ?></span></button>
    <div id="waiting" style="height:22px; display:none; line-height:22px;"><span id="processing-text">Processing...</span> <img src="<?php echo $this->getSkinUrl().'ajaxlog_loader.gif'; ?>" width="22" height="22" style="display:inline; vertical-align:middle;"/></div></div>

    </ol>
    <script type="text/javascript">decorateList('shopping-cart-table')</script>
 
<!----------------------------------------------- End 3rd part Add to cart Multiple products ------------------------------------------------------->
 
<button onclick="AddAllToCart();" class="button btn-allcart" title="Add all to cart" type="button"><span><span>Add all to cart</span></span></button>



    <?php else: ?>

    <?php // Grid Mode ?>

    <?php $_collectionSize = $_productCollection->count() ?>
    <?php $_columnCount = $this->getColumnCount(); ?>
    <?php $i=0; foreach ($_productCollection as $_product): ?>
        <?php if ($i++%$_columnCount==0): ?>
        <ul class="products-grid">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <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(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
                <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php endif; ?>
                <?php echo $this->getPriceHtml($_product, true) ?>
                <div class="actions">
                    <?php if($_product->isSaleable()): ?>
                        <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                            <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 if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
        </ul>
        <?php endif ?>
        <?php endforeach ?>
        <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
    <?php endif; ?>

    <div class="toolbar-bottom">
        <?php echo $this->getToolbarHtml() ?>
    </div>
</div>
<?php endif; ?>

    <!--
    <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="AddAllToCart();"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
          <button onclick="AddAllToCart();" class="button btn-cart" type="button" title="Add all to cart">
         <button class="button btn-allcart" type="button" title="Add all to cart" onclick="AddAllToCart();"> -->

8 comments:

  1. Is there any other way to post the item at one time? As if i have 10 product. It will post 10 times. It will be too long for user to wait.

    ReplyDelete
  2. Where is AddAllToCart() function?

    ReplyDelete
    Replies
    1. Can you check this file in your template??

      Delete
    2. //start script//
      function processNext(urls, i) {
      var next = i + 1;
      $('processing-text').update('Processing item ' + next);
      if (next < urls.size()) {
      new Ajax.Request(urls[i], {
      method: 'get',
      onComplete: function(transport) {
      processNext(urls, next);
      }
      });
      } else {
      new Ajax.Request(urls[i], {
      method: 'get',
      onComplete: function(transport) {
      window.location.href='checkout/cart'
      }
      });
      }
      }

      function addItemsToCart() {
      $('add-items-to-cart').hide();
      $('waiting').show();

      var addToCartUrls = [];
      $$('input.add').each(function(e){
      var id = e.readAttribute('id').split('_')[1];
      //alert(id);
      var qty = Number($('qty_' + id).value);
      if(qty > 0) {
      //alert(qty);
      addToCartUrls.push($('url_' + id).value + 'qty/' + qty);
      }
      });

      if (addToCartUrls.size() > 0) {
      processNext(addToCartUrls, 0);
      } else {
      $('add-items-to-cart').show();
      $('waiting').hide();
      alert('Please add quantity for product then click add to cart button');
      }
      }
      //end script//

      Delete
  3. I am unable to find AddAllToCart() function

    ReplyDelete
    Replies

    1. //start script//
      function processNext(urls, i) {
      var next = i + 1;
      $('processing-text').update('Processing item ' + next);
      if (next < urls.size()) {
      new Ajax.Request(urls[i], {
      method: 'get',
      onComplete: function(transport) {
      processNext(urls, next);
      }
      });
      } else {
      new Ajax.Request(urls[i], {
      method: 'get',
      onComplete: function(transport) {
      window.location.href='checkout/cart'
      }
      });
      }
      }

      function addItemsToCart() {
      $('add-items-to-cart').hide();
      $('waiting').show();

      var addToCartUrls = [];
      $$('input.add').each(function(e){
      var id = e.readAttribute('id').split('_')[1];
      //alert(id);
      var qty = Number($('qty_' + id).value);
      if(qty > 0) {
      //alert(qty);
      addToCartUrls.push($('url_' + id).value + 'qty/' + qty);
      }
      });

      if (addToCartUrls.size() > 0) {
      processNext(addToCartUrls, 0);
      } else {
      $('add-items-to-cart').show();
      $('waiting').hide();
      alert('Please add quantity for product then click add to cart button');
      }
      }
      //end script//

      Delete
  4. no need to AddAllToCart() function in this file because that code is on comment

    ReplyDelete