2

I am trying to add multiple variations configurable product to cart at one time but it shows following error

jquery.js:9666 POST http://127.0.0.1/magento216/checkout/cart/add/uenc/aHR0cDovLzEyNy4wLjAuMS9tYWdlbnRvMjE2LzI1MDMtbWVucy1zaGlydHMuaHRtbA,,/product/62/ 500 (Internal Server Error)

My code is:

  public function execute()
{
    if (!$this->_formKeyValidator->validate($this->getRequest())) {
        return $this->resultRedirectFactory->create()->setPath('*/*/');
    }
    //$params = $this->getRequest()->getParams();
    $eparams =    array ('uenc' => 'aHR0cDovLzEyNy4wLjAuMS9tYWdlbnRvMjE2LzI1MDMtbWVucy1zaGlydHMuaHRtbA,,',
        'product' => '62',
        'selected_configurable_option' => '',
        'related_product' => '',
        'form_key' => 'ZTLmXAr9YrxjJM9U',
        'super_attribute' =>
            array (
                93 => '5',
                199 =>
                    array (
                        1 => '11',
                        2 => '10',
                        3 => '9',
                        4 => '8',
                    ),
            ),
        'qty' => '1',
    );

    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/step1.log');
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);
    $logger->info($eparams['super_attribute']['199']);

    foreach($eparams['super_attribute']['199'] as $item){

        $params =  array ('uenc' => 'aHR0cDovLzEyNy4wLjAuMS9tYWdlbnRvMjE2LzI1MDMtbWVucy1zaGlydHMuaHRtbA,,',
            'product' => '62',
            'selected_configurable_option' => '',
            'related_product' => '',
            'form_key' => 'ZTLmXAr9YrxjJM9U',
            'super_attribute' =>
                array (
                    93 => '5',
                    199 => $item
                ),
            'qty' => '1',
        );

        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/step2.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info($params);

        if (isset($params['qty'])) {
            $filter = new \Zend_Filter_LocalizedToNormalized(
                ['locale' => $this->_objectManager->get('Magento\Framework\Locale\ResolverInterface')->getLocale()]
            );
            $params['qty'] = $filter->filter($params['qty']);
        }

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

        /**
         * Check product availability */

        if (!$product) {
            return $this->goBack();
        }

        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info($params);

        $this->cart->addProduct($product, $params);

    }

    if (!empty($related)) {
        $this->cart->addProductsByIds(explode(',', $related));
    }
    //$this->newProduct();

    $this->cart->save();
    //exit;

    /**
     * @todo remove wishlist observer \Magento\Wishlist\Observer\AddToCart
     */
    $this->_eventManager->dispatch(
        'checkout_cart_add_product_complete',
        ['product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse()]
    );

    if (!$this->_checkoutSession->getNoCartRedirect(true)) {
        if (!$this->cart->getQuote()->getHasError()) {
            $message = __(
                'You added %1 to your shopping cart.',
                $product->getName()
            );
            $this->messageManager->addSuccessMessage($message);
        }
        $this->newProduct();
        return $this->goBack(null, $product);
    }
}
2

You have to define each time new product object to add multiple product at one time,

In above your code, each time product generate from $product = $this->_initProduct(); line.

So you have to create new product object instead of those line, you can just comment out those line and keep below code,

 $storeId = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getId();                
   //$product = $this->initProduct();
 $product = $this->_objectManager->create('Magento\Catalog\Model\Product')->setStoreId($storeId)->load($params['product']);

Using above way each time new Product Object creates and add multiple product for configurable.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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