Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Cart extends MagentoFrameworkAppActionAction
- {
- protected $_cart;
- protected $productRepository;
- protected $formKeyValidator;
- public function __construct(
- MagentoFrameworkAppActionContext $context,
- MagentoCheckoutModelCart $cart,
- MagentoCatalogApiProductRepositoryInterface $productRepository,
- MagentoFrameworkDataFormFormKeyValidator $formKeyValidator
- ) {
- $this->_cart = $cart;
- $this->productRepository = $productRepository;
- $this->_formKeyValidator = $formKeyValidator;
- return parent::__construct($context);
- }
- public function execute()
- {
- if (!$this->_formKeyValidator->validate($this->getRequest())) {
- return $this->resultRedirectFactory->create()->setPath('*/*/');
- }
- $qtys = $this->getRequest()->getParam('qty');
- $childIds = $this->getRequest()->getParam('child_ids');
- $productstoadd = array();
- foreach ($qtys as $parentId => $array) {
- if (is_array($array)) { //configurable products
- foreach ($array as $attributeId => $child) {
- foreach ($child as $childId => $value) {
- if ($value != 0) {
- $productstoadd[$parentId][$attributeId][$childId] = $value;
- }
- }
- }
- } else {
- // simple products
- }
- }
- foreach ($productstoadd as $productId => $data) {
- foreach ($data as $attributeId => $child) {
- foreach ($child as $childId => $qty) {
- //reload product
- $_product = $this->productFactory->create()->setStoreId($storeId)->load($productId);
- $options = array(
- $attributeId => $childId,
- );
- $params = array(
- 'form_key' => $this->getRequest()->getParam('form_key'),
- 'product' => $productId,
- 'super_attribute' => $options,
- 'qty' => $qty,
- 'selected_configurable_option' => $childIds[$productId][$attributeId][$childId],
- );
- $this->_cart->addProduct($_product, $params);
- }
- } }
- $this->_cart->save();
- return $this->resultRedirectFactory->create()->setPath('checkout/cart');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement