Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. class Cart extends MagentoFrameworkAppActionAction
  2. {
  3. protected $_cart;
  4. protected $productRepository;
  5. protected $formKeyValidator;
  6.  
  7. public function __construct(
  8. MagentoFrameworkAppActionContext $context,
  9. MagentoCheckoutModelCart $cart,
  10. MagentoCatalogApiProductRepositoryInterface $productRepository,
  11. MagentoFrameworkDataFormFormKeyValidator $formKeyValidator
  12. ) {
  13.  
  14. $this->_cart = $cart;
  15. $this->productRepository = $productRepository;
  16. $this->_formKeyValidator = $formKeyValidator;
  17.  
  18. return parent::__construct($context);
  19. }
  20.  
  21. public function execute()
  22. {
  23. if (!$this->_formKeyValidator->validate($this->getRequest())) {
  24. return $this->resultRedirectFactory->create()->setPath('*/*/');
  25. }
  26.  
  27. $qtys = $this->getRequest()->getParam('qty');
  28. $childIds = $this->getRequest()->getParam('child_ids');
  29.  
  30. $productstoadd = array();
  31. foreach ($qtys as $parentId => $array) {
  32. if (is_array($array)) { //configurable products
  33. foreach ($array as $attributeId => $child) {
  34. foreach ($child as $childId => $value) {
  35. if ($value != 0) {
  36. $productstoadd[$parentId][$attributeId][$childId] = $value;
  37. }
  38. }
  39. }
  40. } else {
  41. // simple products
  42.  
  43. }
  44. }
  45.  
  46. foreach ($productstoadd as $productId => $data) {
  47. foreach ($data as $attributeId => $child) {
  48. foreach ($child as $childId => $qty) {
  49. //reload product
  50. $_product = $this->productFactory->create()->setStoreId($storeId)->load($productId);
  51. $options = array(
  52. $attributeId => $childId,
  53. );
  54.  
  55. $params = array(
  56. 'form_key' => $this->getRequest()->getParam('form_key'),
  57. 'product' => $productId,
  58. 'super_attribute' => $options,
  59. 'qty' => $qty,
  60. 'selected_configurable_option' => $childIds[$productId][$attributeId][$childId],
  61. );
  62.  
  63. $this->_cart->addProduct($_product, $params);
  64. }
  65. } }
  66. $this->_cart->save();
  67. return $this->resultRedirectFactory->create()->setPath('checkout/cart');
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement