3

I have in my view.phtml some products and one submit button for each product and all works fine. Now i want to submit all products with one button(submit multiple products), I tried this solution and the problem that I can't get the products params in my controller.

view.phtml

<?php foreach ($_productCollection as $prod) : ?>
<?php if($prod->isSaleable()): ?>
    <?php //echo $this->getChildHtml('addtocart') ?>
    <form action="<?php echo $this->getAddToCartUrl($prod) ?>" method="post" id="product_addtocart_form_<?php echo $prod->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
        <?php if(!$prod->isGrouped()): ?>
            <?php $productId = $prod->getId(); ?>
            <?php $productCartQty = $this->getCartQty($prod); ?>
            <div class="border-new-cart-button">
                <span class="plus qty-add" style="float:right;" onclick="plusQty('qty-<?php echo $productId ?>')"><i class="fa fa-plus"></i></span>
                <input type="text" class="input-qty-product number-control" title="Qty" value="<?php echo /*$this->getProductDefaultQty() * */ 0 ?>" name="qty" id="qty-<?php echo $productId ?>"/>
                <span class="moins qty-remove" style="float:right;" onclick="minusQty('qty-<?php echo $productId ?>')"><i class="fa fa-minus"></i></span>
            </div>
        <?php endif; ?>
        <button type="button" class="btn add-to-cart" onclick="this.form.submit()"><span><?php echo $this->__('Valider') ?></span></button>
        <input type="hidden" name="products[]" value="<?php echo $_product->getId() ?>" />
     </form>
<?php endif;?>
<?php endforeach; ?>

/*My one add button*/
 <form action="<?php echo Mage::getUrl('checkout/cart/addmultiple'); ?>" method="post" id="products_addtocart_form" enctype="multipart/form-data">
    <input type="hidden" name="multiple_products" id="multiple_products" value="" />
    <button class="btn add-to-cart" onclick="this.form.submit()"><span><?php echo $this->__('Add all products') ?></span></button>
</form>

/*JS PART*/
<script type="text/javascript">
jQuery("#products_addtocart_form").on('submit',function(e){
        var products = {};
        jQuery('input[name="products[]"]').each(function() {
            var productId = jQuery(this).val();
            var productqty = jQuery('input[qtyproduct='+ productId +']').val();
            if(parseInt(productqty)>0){
                products[productId]= productqty;
            }
        });
        jQuery('#multiple_products').val(JSON.stringify(products));
        return;

    });
</script>

Checkout/CartController.php

require_once 'Mage/Checkout/controllers/CartController.php';

class Compagny_Catalog_Checkout_CartController extends Mage_Checkout_CartController {

    public function addmultipleAction() {
        $productIds = $this->getRequest()->getParam('multiple_products');
        Mage::log($productIds); // I get an empty array
        $productIds = json_decode($productIds,true);
        Mage::log($productIds); // I get also an empty array
         if (!is_array($productIds)) {
             Mage::log('product parameter not found !');
             $this->_goBack();
             return;
         }

         foreach( $productIds as $prodId => $prodQty) {
             try {
                 $prodId = intval($prodId);
                 $qty = intval($prodQty);
                 if($qty>0)continue;
                 $cart = $this->_getCart();
                 $cart->init();
                 $product = Mage::getModel('catalog/product')
                     ->setStoreId(Mage::app()->getStore()->getId())
                     ->load($prodId);
                 $eventArgs = array(
                     'product' => $product,
                     'qty' => $qty,
                     'additional_ids' => array(),
                     'request' => $this->getRequest(),
                     'response' => $this->getResponse(),
                 );
                 if(!$product){
                     continue;
                 }

                 Mage::dispatchEvent('checkout_cart_before_add', $eventArgs);

                 $cart->addProduct($product, $qty);

                 Mage::dispatchEvent('checkout_cart_after_add', $eventArgs);

                 $cart->save();

                 Mage::dispatchEvent('checkout_cart_add_product', array('product'=>$product));

                 $message = $this->__('%s was successfully added to your shopping cart.', $product->getName());
                 Mage::getSingleton('checkout/session')->addSuccess($message);
             }
             catch (Mage_Core_Exception $e) {
                 if (Mage::getSingleton('checkout/session')->getUseNotice(true)) {
                     Mage::getSingleton('checkout/session')->addNotice($product->getName() . ': ' . $e->getMessage());
                 }
                 else {
                     Mage::getSingleton('checkout/session')->addError($product->getName() . ': ' . $e->getMessage());
                 }
             }
             catch (Exception $e) {
                 Mage::getSingleton('checkout/session')->addException($e, $this->__('Can not add item to shopping cart'));
             }
         }
         $this->_goBack();
     }
}
1
  • try merging both form
    – Piyush
    Jul 21, 2017 at 8:03

1 Answer 1

2

Since nobody was able to solve the issue, I debugged the code and I solved the problem, so for those who want to use the solution here is it:

view.phtml

<form action="<?php echo Mage::getUrl('checkout/cart/addmultiple')?>" method="post" id="products_addtocart_form" enctype="multipart/form-data">
    <?php foreach ($_productCollection as $prod) : ?>
        <div class="product-form">
            <input type="hidden" name="productId[]" value="<?php echo $productId ?>" />
            <input type="hidden" name="productName[]" value="<?php echo $prod->getName() ?>" />
            <input type="text" name="qty[]" class="input-qty-product number-control" title="Qty" value="<?php echo /*$this->getProductDefaultQty() * */ 0 ?>"  id="qty-<?php echo $productId ?>"/>
        </div>
    <?php endforeach; ?>
    <button id="one_submit_all_products" class="btn add-to-cart" onclick="this.form.submit()"><span><?php echo $this->__('Ajouter les produits au panier') ?></span></button>
</form>

app/code/local/Compagny/Catalog/controllers/Checkout/CartController.php

<?php
require_once 'Mage/Checkout/controllers/CartController.php';

class Compagny_Catalog_Checkout_CartController extends Mage_Checkout_CartController {

    /**
     * @object add multiple products with one submit button in view.phtml
     */

    public function addmultipleAction() {
        $productIds = $this->getRequest()->getParam('productId');
        $productQtys = $this->getRequest()->getParam('qty');
        $productIdQty = (array_combine($productIds,$productQtys));
         if (!is_array($productIds)) {
             Mage::log('product parameter not found !');
             $this->_goBack();
             return;
         }
         foreach($productIdQty as $prod=> $productQtys) {

                 try {
                     $prodId = intval($prod);
                     $qty = intval($productQtys);
                     if($qty<=0)continue;
                     $cart = $this->_getCart();
                     $cart->init();
                     $product = Mage::getModel('catalog/product')
                         ->setStoreId(Mage::app()->getStore()->getId())
                         ->load($prodId);
                     $eventArgs = array(
                         'product' => $product,
                         'qty' => $qty,
                         'additional_ids' => array(),
                         'request' => $this->getRequest(),
                         'response' => $this->getResponse(),
                     );
                     if (!$product) {
                         continue;
                     }
                     Mage::dispatchEvent('checkout_cart_before_add', $eventArgs);
                     $cart->addProduct($product->getId(), $qty); //the product_id, qty
                     Mage::dispatchEvent('checkout_cart_after_add', $eventArgs);
                     $cart->save();
                     Mage::dispatchEvent('checkout_cart_add_product', array('product' => $product));
                     $message = $this->__('The product %s has been added to cart.', $product->getName());
                     Mage::getSingleton('checkout/session')->addSuccess($message);
                 } catch (Mage_Core_Exception $e) {
                     if (Mage::getSingleton('checkout/session')->getUseNotice(true)) {
                         Mage::getSingleton('checkout/session')->addNotice($product->getName() . ': ' . $e->getMessage());
                     } else {
                         Mage::getSingleton('checkout/session')->addError($product->getName() . ': ' . $e->getMessage());
                     }
                 } catch (Exception $e) {
                     Mage::getSingleton('checkout/session')->addException($e, $this->__('Can not add item to shopping cart'));
                 }
         }
         $this->_goBack();
     }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.