0

I have a simple product with custom options. After popup, I select custom options of this product, and click add to cart.

I get params like that: product=167&options%5B4%5D=10&qty=1.

Now I want to add product to cart programmatically, how can I do it?

2 Answers 2

2

please check the Mage_Checkout_CartController and addAction() inside, you will find everything you need ;)

$cart   = $this->_getCart();
    $params = $this->getRequest()->getParams();
    try {
        if (isset($params['qty'])) {
            $filter = new Zend_Filter_LocalizedToNormalized(
                array('locale' => Mage::app()->getLocale()->getLocaleCode())
            );
            $params['qty'] = $filter->filter($params['qty']);
        }

        $product = $this->_initProduct();
        $related = $this->getRequest()->getParam('related_product');

        /**
         * Check product availability
         */
        if (!$product) {
            $this->_goBack();
            return;
        }

        $cart->addProduct($product, $params);
        if (!empty($related)) {
            $cart->addProductsByIds(explode(',', $related));
        }

        $cart->save();

        $this->_getSession()->setCartWasUpdated(true);
        /**
         * @todo remove wishlist observer processAddToCart
         */
        Mage::dispatchEvent('checkout_cart_add_product_complete',
            array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
        );

        if (!$this->_getSession()->getNoCartRedirect(true)) {
            if (!$cart->getQuote()->getHasError()){
                $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->htmlEscape($product->getName()));
                $this->_getSession()->addSuccess($message);
            }
            $this->_goBack();
        }
1

Haven't tested the code below so might need some debugging but I guess it would be something like this

$productid = Mage::app()->getRequest()->getParam('product');
$qty = Mage::app()->getRequest()->getParam('qty');
$options = array();
foreach (Mage::app()->getRequest()->getParams() as $key => $value) {
   if (!strstr($key, 'options')) continue;
   $options[preg_replace('/([^0-9]+)/', '', $key)] = Mage::app()->getRequest()->getParam($key);
}

product=167&options[4]=10&qty=1


$_product = Mage::getModel('catalog/product')->load($productid);

$_cart = Mage::getModel('checkout/cart');
$_cart->init();

$_cart->addProduct($_product, array(
   'product' => $productid,
   'qty' => $qty,
   'options' => $options
));

$_cart->save();

Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
1
  • why do you need $_GET & Mage::app()->getRequest()->getParam($key); separately ? :) use $getParams = Mage::app()->getRequest()->getParams() instead
    – dsitovs
    Commented Dec 22, 2014 at 13:25

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.