Unable to add multiple Products to cart in magento2Add multiple items to cart in Magento2Unit Test for overwrite collection class in magento2main.CRITICAL: Plugin class doesn't existSet custom price of product when adding to cart code not workingMagento 2: How to override newsletter Subscriber modelMagento 2.1 Create a filter in the product grid by new attributeMagento 2: Add a product to the cart programmaticallyMagento 2: I Want to add multiple product using checkboxMagento 2.2: What controllers are called when a product is added to the cart?Magento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 Can't view module's front end page output?

How could an armless race establish civilization?

Is it okay to submit a paper from a master's thesis without informing the advisor?

Converting Geographic Coordinates into Lambert2008 coordinates

If two black hole event horizons overlap (touch) can they ever separate again?

Can European countries bypass the EU and make their own individual trade deal with the U.S.?

Do home values typically rise and fall consistently across different price ranges?

Can I use Alchemist's fire to turn my sword into a virtual Flame Blade?

Why wasn't EBCDIC designed with contiguous alphanumeric characters?

How can I deal with extreme temperatures in a hotel room?

How do I create a new column in a dataframe from an existing column using conditions?

How do ohm meters measure high resistances?

Reusable spacecraft: why still have fairings detach, instead of open/close?

Checkmate in 1 on a Tangled Board

Is it okay to fade a human face just to create some space to place important content over it?

The Football Squad

I just started should I accept a farewell lunch for a coworker I don't know?

Can purchasing tickets for high holidays services count as maaser?

Are Valenar elves and Aereni elves different races of elves?

Active wildlife outside the window- Good or Bad for Cat psychology?

Is ALTER TABLE ... DROP COLUMN really a metadata only operation?

Why was p[:] designed to work differently in these two situations?

The Lucas argument vs the theorem-provers--who wins and why?

Copy group of files (Filename*) to backup (Filename*.bak)

How can I know if a PDF file was created via LaTeX or XeLaTeX?



Unable to add multiple Products to cart in magento2


Add multiple items to cart in Magento2Unit Test for overwrite collection class in magento2main.CRITICAL: Plugin class doesn't existSet custom price of product when adding to cart code not workingMagento 2: How to override newsletter Subscriber modelMagento 2.1 Create a filter in the product grid by new attributeMagento 2: Add a product to the cart programmaticallyMagento 2: I Want to add multiple product using checkboxMagento 2.2: What controllers are called when a product is added to the cart?Magento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 Can't view module's front end page output?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4















I have used below code in my controller.



use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;

class Addtocart extends MagentoFrameworkAppActionAction


protected $formKey;
protected $cart;
protected $product;
protected $_resource;
protected $customerSession;
private $productRepository;
private $logger;
protected $groupRepository;
protected $_productCollection;
public $selection_array;
protected $checkoutSession;
protected $resultRedirect;
public $_storeManager;
protected $group;
protected $messageManager;
protected $_urlInterface;
protected $_taxCalculationService;
protected $_taxCalculation;
protected $_scopeConfig;

public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkDataFormFormKey $formKey,
MagentoCheckoutModelCart $cart,
MagentoCatalogModelProductFactory $product,
MagentoFrameworkAppResourceConnection $resource,
MagentoCustomerModelSession $customerSession,
MagentoCatalogApiProductRepositoryInterface $productRepository,
PsrLogLoggerInterface $logger,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCustomerApiGroupRepositoryInterface $groupRepository,
MagentoCatalogModelProduct $productCollection,
MagentoCheckoutModelSession $checkoutSession,
MagentoFrameworkControllerResultFactory $result,
MagentoCustomerModelGroup $group,
MagentoFrameworkMessageManagerInterface $messageManager,
MagentoFrameworkUrlInterface $urlInterface,
MagentoTaxApiTaxCalculationInterface $taxCalculationService,
MagentoTaxModelCalculation $taxCalculation,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
array $data = []
)
$this->formKey = $formKey;
$this->cart = $cart;
$this->resultRedirect = $result;
$this->_storeManager = $storeManager;
$this->product = $product;
$this->_resource = $resource;
$this->customerSession = $customerSession;
$this->productRepository = $productRepository;
$this->logger = $logger;
$this->groupRepository = $groupRepository;
$this->_productCollection = $productCollection;
$this->checkoutSession = $checkoutSession;
$this->group = $group;
$this->messageManager = $messageManager;
$this->_urlInterface = $urlInterface;
$this->_taxCalculationService = $taxCalculationService;
$this->_taxCalculation = $taxCalculation;
$this->_scopeConfig = $scopeConfig;
parent::__construct($context);

public function execute()
//$selectedItems = $this->getRequest()->getPost('selectedItems');
$selectedItems = '31,63'; //these are product Ids
$selectedItems = explode(",",$selectedItems);
try
$connection = $this->getConnection();
foreach ($selectedItems as $selectedItem)
$productSku = $this->getProductSkubyId($selectedItem);
$vairantSku = $this->getVariantSku($productSku);
$variantId = $this->getProductIdBySku($vairantSku);
$params = array(
'form_key' => $this->formKey->getFormKey(),
'product_id' => $variantId, //product Id
'qty' =>1 //quantity of product
);
$_product = $this->product->create()->load($variantId);
$item = $this->cart->getQuote()->getItemByProduct($_product);
if($item)
$this->cart->updateItem($item->getId(), array('qty' => 1));
else
$this->cart->addProduct($_product , $params);

$customPrice = 1000;
//$customPrice = $this->getCustomPrice($referenceSku,$customerGroupId);
$productItem = $this->getProductQuote($_product);
$productItem->setCustomPrice($customPrice);
$productItem->setOriginalCustomPrice($customPrice);
$productItem->getProduct()->setIsSuperMode(true);
$quote = $this->cart->getQuote();
$quoteId = $quote->getId();

$this->cart->save();

$status = 1;

catch (MagentoFrameworkExceptionLocalizedException $e)
$this->messageManager->addException($e,__('%1', $e->getMessage()));
$status = 0;
catch (Exception $e)
$this->messageManager->addException($e, __('error.'));
$status = 0;

$result = array();
$result['status'] = $status;
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$resultJson->setData($result);
return $resultJson;



public function getConnection()

$connection = $this->_resource->getConnection(MagentoFrameworkAppResourceConnection::DEFAULT_CONNECTION);
return $connection;

public function getProductQuote($product)
$quote = $this->checkoutSession->getQuote();
$cartItems = $quote->getItemByProduct($product);
return $cartItems;

public function getProductSkubyId($productId)
$product = $this->productRepository->getById($productId);
return $product->getSku();


public function loadProduct($sku)

return $this->productRepository->get($sku);


public function getVariantSku($productSku)
$connection = $this->getConnection();
$unitSkuBind = array('parent_sku' => $productSku);
$unitSkuQuery = "select * from product_map where parent_sku =:parent_sku";
$resultProduct = $connection->query($unitSkuQuery,$unitSkuBind);
$resultQuery = $resultProduct->fetchAll();
if(!empty($resultQuery))
return $variant_sku = $resultQuery[0]['sku'];

return;


public function getProductIdBySku($sku)
$productId = $this->_productCollection->getIdBySku($sku);
return $productId;





In above code i am adding Two products by loading the product object from product id.



That is adding only one product to the cart instead of two.



Please anyone help me where i am doing wrong.



Note: I need to add two product with custom price.



Thanks










share|improve this question
























  • you cant add more than one product at a time for magento2

    – Ketan Borada
    Jun 21 at 6:23











  • @KetanBorada, yes i used above code, but its adding only one product to the cart

    – jafar pinjar
    Jun 21 at 6:25











  • In magento2 you can not add multiple item like you did in for loop, only one item can be add at a time

    – Ketan Borada
    Jun 21 at 6:27











  • @KetanBorada, yes but i have requirement like that, I am getting 2 post parameters as Ids, need to add them to cart, is any possibility to achieve it?

    – jafar pinjar
    Jun 21 at 6:28












  • magento.stackexchange.com/questions/258679/…

    – Ketan Borada
    Jun 21 at 6:32

















4















I have used below code in my controller.



use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;

class Addtocart extends MagentoFrameworkAppActionAction


protected $formKey;
protected $cart;
protected $product;
protected $_resource;
protected $customerSession;
private $productRepository;
private $logger;
protected $groupRepository;
protected $_productCollection;
public $selection_array;
protected $checkoutSession;
protected $resultRedirect;
public $_storeManager;
protected $group;
protected $messageManager;
protected $_urlInterface;
protected $_taxCalculationService;
protected $_taxCalculation;
protected $_scopeConfig;

public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkDataFormFormKey $formKey,
MagentoCheckoutModelCart $cart,
MagentoCatalogModelProductFactory $product,
MagentoFrameworkAppResourceConnection $resource,
MagentoCustomerModelSession $customerSession,
MagentoCatalogApiProductRepositoryInterface $productRepository,
PsrLogLoggerInterface $logger,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCustomerApiGroupRepositoryInterface $groupRepository,
MagentoCatalogModelProduct $productCollection,
MagentoCheckoutModelSession $checkoutSession,
MagentoFrameworkControllerResultFactory $result,
MagentoCustomerModelGroup $group,
MagentoFrameworkMessageManagerInterface $messageManager,
MagentoFrameworkUrlInterface $urlInterface,
MagentoTaxApiTaxCalculationInterface $taxCalculationService,
MagentoTaxModelCalculation $taxCalculation,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
array $data = []
)
$this->formKey = $formKey;
$this->cart = $cart;
$this->resultRedirect = $result;
$this->_storeManager = $storeManager;
$this->product = $product;
$this->_resource = $resource;
$this->customerSession = $customerSession;
$this->productRepository = $productRepository;
$this->logger = $logger;
$this->groupRepository = $groupRepository;
$this->_productCollection = $productCollection;
$this->checkoutSession = $checkoutSession;
$this->group = $group;
$this->messageManager = $messageManager;
$this->_urlInterface = $urlInterface;
$this->_taxCalculationService = $taxCalculationService;
$this->_taxCalculation = $taxCalculation;
$this->_scopeConfig = $scopeConfig;
parent::__construct($context);

public function execute()
//$selectedItems = $this->getRequest()->getPost('selectedItems');
$selectedItems = '31,63'; //these are product Ids
$selectedItems = explode(",",$selectedItems);
try
$connection = $this->getConnection();
foreach ($selectedItems as $selectedItem)
$productSku = $this->getProductSkubyId($selectedItem);
$vairantSku = $this->getVariantSku($productSku);
$variantId = $this->getProductIdBySku($vairantSku);
$params = array(
'form_key' => $this->formKey->getFormKey(),
'product_id' => $variantId, //product Id
'qty' =>1 //quantity of product
);
$_product = $this->product->create()->load($variantId);
$item = $this->cart->getQuote()->getItemByProduct($_product);
if($item)
$this->cart->updateItem($item->getId(), array('qty' => 1));
else
$this->cart->addProduct($_product , $params);

$customPrice = 1000;
//$customPrice = $this->getCustomPrice($referenceSku,$customerGroupId);
$productItem = $this->getProductQuote($_product);
$productItem->setCustomPrice($customPrice);
$productItem->setOriginalCustomPrice($customPrice);
$productItem->getProduct()->setIsSuperMode(true);
$quote = $this->cart->getQuote();
$quoteId = $quote->getId();

$this->cart->save();

$status = 1;

catch (MagentoFrameworkExceptionLocalizedException $e)
$this->messageManager->addException($e,__('%1', $e->getMessage()));
$status = 0;
catch (Exception $e)
$this->messageManager->addException($e, __('error.'));
$status = 0;

$result = array();
$result['status'] = $status;
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$resultJson->setData($result);
return $resultJson;



public function getConnection()

$connection = $this->_resource->getConnection(MagentoFrameworkAppResourceConnection::DEFAULT_CONNECTION);
return $connection;

public function getProductQuote($product)
$quote = $this->checkoutSession->getQuote();
$cartItems = $quote->getItemByProduct($product);
return $cartItems;

public function getProductSkubyId($productId)
$product = $this->productRepository->getById($productId);
return $product->getSku();


public function loadProduct($sku)

return $this->productRepository->get($sku);


public function getVariantSku($productSku)
$connection = $this->getConnection();
$unitSkuBind = array('parent_sku' => $productSku);
$unitSkuQuery = "select * from product_map where parent_sku =:parent_sku";
$resultProduct = $connection->query($unitSkuQuery,$unitSkuBind);
$resultQuery = $resultProduct->fetchAll();
if(!empty($resultQuery))
return $variant_sku = $resultQuery[0]['sku'];

return;


public function getProductIdBySku($sku)
$productId = $this->_productCollection->getIdBySku($sku);
return $productId;





In above code i am adding Two products by loading the product object from product id.



That is adding only one product to the cart instead of two.



Please anyone help me where i am doing wrong.



Note: I need to add two product with custom price.



Thanks










share|improve this question
























  • you cant add more than one product at a time for magento2

    – Ketan Borada
    Jun 21 at 6:23











  • @KetanBorada, yes i used above code, but its adding only one product to the cart

    – jafar pinjar
    Jun 21 at 6:25











  • In magento2 you can not add multiple item like you did in for loop, only one item can be add at a time

    – Ketan Borada
    Jun 21 at 6:27











  • @KetanBorada, yes but i have requirement like that, I am getting 2 post parameters as Ids, need to add them to cart, is any possibility to achieve it?

    – jafar pinjar
    Jun 21 at 6:28












  • magento.stackexchange.com/questions/258679/…

    – Ketan Borada
    Jun 21 at 6:32













4












4








4








I have used below code in my controller.



use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;

class Addtocart extends MagentoFrameworkAppActionAction


protected $formKey;
protected $cart;
protected $product;
protected $_resource;
protected $customerSession;
private $productRepository;
private $logger;
protected $groupRepository;
protected $_productCollection;
public $selection_array;
protected $checkoutSession;
protected $resultRedirect;
public $_storeManager;
protected $group;
protected $messageManager;
protected $_urlInterface;
protected $_taxCalculationService;
protected $_taxCalculation;
protected $_scopeConfig;

public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkDataFormFormKey $formKey,
MagentoCheckoutModelCart $cart,
MagentoCatalogModelProductFactory $product,
MagentoFrameworkAppResourceConnection $resource,
MagentoCustomerModelSession $customerSession,
MagentoCatalogApiProductRepositoryInterface $productRepository,
PsrLogLoggerInterface $logger,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCustomerApiGroupRepositoryInterface $groupRepository,
MagentoCatalogModelProduct $productCollection,
MagentoCheckoutModelSession $checkoutSession,
MagentoFrameworkControllerResultFactory $result,
MagentoCustomerModelGroup $group,
MagentoFrameworkMessageManagerInterface $messageManager,
MagentoFrameworkUrlInterface $urlInterface,
MagentoTaxApiTaxCalculationInterface $taxCalculationService,
MagentoTaxModelCalculation $taxCalculation,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
array $data = []
)
$this->formKey = $formKey;
$this->cart = $cart;
$this->resultRedirect = $result;
$this->_storeManager = $storeManager;
$this->product = $product;
$this->_resource = $resource;
$this->customerSession = $customerSession;
$this->productRepository = $productRepository;
$this->logger = $logger;
$this->groupRepository = $groupRepository;
$this->_productCollection = $productCollection;
$this->checkoutSession = $checkoutSession;
$this->group = $group;
$this->messageManager = $messageManager;
$this->_urlInterface = $urlInterface;
$this->_taxCalculationService = $taxCalculationService;
$this->_taxCalculation = $taxCalculation;
$this->_scopeConfig = $scopeConfig;
parent::__construct($context);

public function execute()
//$selectedItems = $this->getRequest()->getPost('selectedItems');
$selectedItems = '31,63'; //these are product Ids
$selectedItems = explode(",",$selectedItems);
try
$connection = $this->getConnection();
foreach ($selectedItems as $selectedItem)
$productSku = $this->getProductSkubyId($selectedItem);
$vairantSku = $this->getVariantSku($productSku);
$variantId = $this->getProductIdBySku($vairantSku);
$params = array(
'form_key' => $this->formKey->getFormKey(),
'product_id' => $variantId, //product Id
'qty' =>1 //quantity of product
);
$_product = $this->product->create()->load($variantId);
$item = $this->cart->getQuote()->getItemByProduct($_product);
if($item)
$this->cart->updateItem($item->getId(), array('qty' => 1));
else
$this->cart->addProduct($_product , $params);

$customPrice = 1000;
//$customPrice = $this->getCustomPrice($referenceSku,$customerGroupId);
$productItem = $this->getProductQuote($_product);
$productItem->setCustomPrice($customPrice);
$productItem->setOriginalCustomPrice($customPrice);
$productItem->getProduct()->setIsSuperMode(true);
$quote = $this->cart->getQuote();
$quoteId = $quote->getId();

$this->cart->save();

$status = 1;

catch (MagentoFrameworkExceptionLocalizedException $e)
$this->messageManager->addException($e,__('%1', $e->getMessage()));
$status = 0;
catch (Exception $e)
$this->messageManager->addException($e, __('error.'));
$status = 0;

$result = array();
$result['status'] = $status;
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$resultJson->setData($result);
return $resultJson;



public function getConnection()

$connection = $this->_resource->getConnection(MagentoFrameworkAppResourceConnection::DEFAULT_CONNECTION);
return $connection;

public function getProductQuote($product)
$quote = $this->checkoutSession->getQuote();
$cartItems = $quote->getItemByProduct($product);
return $cartItems;

public function getProductSkubyId($productId)
$product = $this->productRepository->getById($productId);
return $product->getSku();


public function loadProduct($sku)

return $this->productRepository->get($sku);


public function getVariantSku($productSku)
$connection = $this->getConnection();
$unitSkuBind = array('parent_sku' => $productSku);
$unitSkuQuery = "select * from product_map where parent_sku =:parent_sku";
$resultProduct = $connection->query($unitSkuQuery,$unitSkuBind);
$resultQuery = $resultProduct->fetchAll();
if(!empty($resultQuery))
return $variant_sku = $resultQuery[0]['sku'];

return;


public function getProductIdBySku($sku)
$productId = $this->_productCollection->getIdBySku($sku);
return $productId;





In above code i am adding Two products by loading the product object from product id.



That is adding only one product to the cart instead of two.



Please anyone help me where i am doing wrong.



Note: I need to add two product with custom price.



Thanks










share|improve this question
















I have used below code in my controller.



use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;

class Addtocart extends MagentoFrameworkAppActionAction


protected $formKey;
protected $cart;
protected $product;
protected $_resource;
protected $customerSession;
private $productRepository;
private $logger;
protected $groupRepository;
protected $_productCollection;
public $selection_array;
protected $checkoutSession;
protected $resultRedirect;
public $_storeManager;
protected $group;
protected $messageManager;
protected $_urlInterface;
protected $_taxCalculationService;
protected $_taxCalculation;
protected $_scopeConfig;

public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkDataFormFormKey $formKey,
MagentoCheckoutModelCart $cart,
MagentoCatalogModelProductFactory $product,
MagentoFrameworkAppResourceConnection $resource,
MagentoCustomerModelSession $customerSession,
MagentoCatalogApiProductRepositoryInterface $productRepository,
PsrLogLoggerInterface $logger,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCustomerApiGroupRepositoryInterface $groupRepository,
MagentoCatalogModelProduct $productCollection,
MagentoCheckoutModelSession $checkoutSession,
MagentoFrameworkControllerResultFactory $result,
MagentoCustomerModelGroup $group,
MagentoFrameworkMessageManagerInterface $messageManager,
MagentoFrameworkUrlInterface $urlInterface,
MagentoTaxApiTaxCalculationInterface $taxCalculationService,
MagentoTaxModelCalculation $taxCalculation,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
array $data = []
)
$this->formKey = $formKey;
$this->cart = $cart;
$this->resultRedirect = $result;
$this->_storeManager = $storeManager;
$this->product = $product;
$this->_resource = $resource;
$this->customerSession = $customerSession;
$this->productRepository = $productRepository;
$this->logger = $logger;
$this->groupRepository = $groupRepository;
$this->_productCollection = $productCollection;
$this->checkoutSession = $checkoutSession;
$this->group = $group;
$this->messageManager = $messageManager;
$this->_urlInterface = $urlInterface;
$this->_taxCalculationService = $taxCalculationService;
$this->_taxCalculation = $taxCalculation;
$this->_scopeConfig = $scopeConfig;
parent::__construct($context);

public function execute()
//$selectedItems = $this->getRequest()->getPost('selectedItems');
$selectedItems = '31,63'; //these are product Ids
$selectedItems = explode(",",$selectedItems);
try
$connection = $this->getConnection();
foreach ($selectedItems as $selectedItem)
$productSku = $this->getProductSkubyId($selectedItem);
$vairantSku = $this->getVariantSku($productSku);
$variantId = $this->getProductIdBySku($vairantSku);
$params = array(
'form_key' => $this->formKey->getFormKey(),
'product_id' => $variantId, //product Id
'qty' =>1 //quantity of product
);
$_product = $this->product->create()->load($variantId);
$item = $this->cart->getQuote()->getItemByProduct($_product);
if($item)
$this->cart->updateItem($item->getId(), array('qty' => 1));
else
$this->cart->addProduct($_product , $params);

$customPrice = 1000;
//$customPrice = $this->getCustomPrice($referenceSku,$customerGroupId);
$productItem = $this->getProductQuote($_product);
$productItem->setCustomPrice($customPrice);
$productItem->setOriginalCustomPrice($customPrice);
$productItem->getProduct()->setIsSuperMode(true);
$quote = $this->cart->getQuote();
$quoteId = $quote->getId();

$this->cart->save();

$status = 1;

catch (MagentoFrameworkExceptionLocalizedException $e)
$this->messageManager->addException($e,__('%1', $e->getMessage()));
$status = 0;
catch (Exception $e)
$this->messageManager->addException($e, __('error.'));
$status = 0;

$result = array();
$result['status'] = $status;
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$resultJson->setData($result);
return $resultJson;



public function getConnection()

$connection = $this->_resource->getConnection(MagentoFrameworkAppResourceConnection::DEFAULT_CONNECTION);
return $connection;

public function getProductQuote($product)
$quote = $this->checkoutSession->getQuote();
$cartItems = $quote->getItemByProduct($product);
return $cartItems;

public function getProductSkubyId($productId)
$product = $this->productRepository->getById($productId);
return $product->getSku();


public function loadProduct($sku)

return $this->productRepository->get($sku);


public function getVariantSku($productSku)
$connection = $this->getConnection();
$unitSkuBind = array('parent_sku' => $productSku);
$unitSkuQuery = "select * from product_map where parent_sku =:parent_sku";
$resultProduct = $connection->query($unitSkuQuery,$unitSkuBind);
$resultQuery = $resultProduct->fetchAll();
if(!empty($resultQuery))
return $variant_sku = $resultQuery[0]['sku'];

return;


public function getProductIdBySku($sku)
$productId = $this->_productCollection->getIdBySku($sku);
return $productId;





In above code i am adding Two products by loading the product object from product id.



That is adding only one product to the cart instead of two.



Please anyone help me where i am doing wrong.



Note: I need to add two product with custom price.



Thanks







magento2 addtocart controllers quote






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 21 at 6:40







jafar pinjar

















asked Jun 21 at 6:18









jafar pinjarjafar pinjar

1,0264 silver badges21 bronze badges




1,0264 silver badges21 bronze badges












  • you cant add more than one product at a time for magento2

    – Ketan Borada
    Jun 21 at 6:23











  • @KetanBorada, yes i used above code, but its adding only one product to the cart

    – jafar pinjar
    Jun 21 at 6:25











  • In magento2 you can not add multiple item like you did in for loop, only one item can be add at a time

    – Ketan Borada
    Jun 21 at 6:27











  • @KetanBorada, yes but i have requirement like that, I am getting 2 post parameters as Ids, need to add them to cart, is any possibility to achieve it?

    – jafar pinjar
    Jun 21 at 6:28












  • magento.stackexchange.com/questions/258679/…

    – Ketan Borada
    Jun 21 at 6:32

















  • you cant add more than one product at a time for magento2

    – Ketan Borada
    Jun 21 at 6:23











  • @KetanBorada, yes i used above code, but its adding only one product to the cart

    – jafar pinjar
    Jun 21 at 6:25











  • In magento2 you can not add multiple item like you did in for loop, only one item can be add at a time

    – Ketan Borada
    Jun 21 at 6:27











  • @KetanBorada, yes but i have requirement like that, I am getting 2 post parameters as Ids, need to add them to cart, is any possibility to achieve it?

    – jafar pinjar
    Jun 21 at 6:28












  • magento.stackexchange.com/questions/258679/…

    – Ketan Borada
    Jun 21 at 6:32
















you cant add more than one product at a time for magento2

– Ketan Borada
Jun 21 at 6:23





you cant add more than one product at a time for magento2

– Ketan Borada
Jun 21 at 6:23













@KetanBorada, yes i used above code, but its adding only one product to the cart

– jafar pinjar
Jun 21 at 6:25





@KetanBorada, yes i used above code, but its adding only one product to the cart

– jafar pinjar
Jun 21 at 6:25













In magento2 you can not add multiple item like you did in for loop, only one item can be add at a time

– Ketan Borada
Jun 21 at 6:27





In magento2 you can not add multiple item like you did in for loop, only one item can be add at a time

– Ketan Borada
Jun 21 at 6:27













@KetanBorada, yes but i have requirement like that, I am getting 2 post parameters as Ids, need to add them to cart, is any possibility to achieve it?

– jafar pinjar
Jun 21 at 6:28






@KetanBorada, yes but i have requirement like that, I am getting 2 post parameters as Ids, need to add them to cart, is any possibility to achieve it?

– jafar pinjar
Jun 21 at 6:28














magento.stackexchange.com/questions/258679/…

– Ketan Borada
Jun 21 at 6:32





magento.stackexchange.com/questions/258679/…

– Ketan Borada
Jun 21 at 6:32










0






active

oldest

votes














Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f279110%2funable-to-add-multiple-products-to-cart-in-magento2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f279110%2funable-to-add-multiple-products-to-cart-in-magento2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form