Skip to content

[Magento 2] Add Multiple Products Programatically

protected $formKey;
protected $cart;
protected $product;
protected $session;
public

function __construct(MagentoFrameworkSessionSessionManagerInterface $session, MagentoFrameworkAppActionContext $context, MagentoFrameworkDataFormFormKey $formKey, MagentoCheckoutModelCart $cart, MagentoCatalogModelProduct $product, array $data = [])
	{
	$this->formKey = $formKey;
	$this->cart = $cart;
	$this->product = $product;
	$this->session = $session;
	parent::__construct($context);
	}

public

function execute()
	{
	$array_data = array(
		array(
			'pid' => '596436',
			'qty' => 1
		) ,
		array(
			'pid' => '601933',
			'qty' => 1
		) ,
		array(
			'pid' => '596436',
			'qty' => 1
		) ,
		array(
			'pid' => '596436',
			'qty' => 1
		) ,
		array(
			'pid' => '596625',
			'qty' => 1
		) ,
	);
	$productIds = array();
	foreach($array_data as $item)
		{
		$productIds[] = $item['pid'];
		}

	$this->cart->addProductsByIds($productIds);
	$this->cart->save();
	}

 

Share

Comments are closed.