Guest User

Untitled

a guest
Jan 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. <?php
  2. namespace VendorAddallControllerIndex;
  3. use MagentoFrameworkAppActionContext;
  4. class Add extends MagentoFrameworkAppActionAction{
  5. protected $resultJsonFactory;
  6. protected $request;
  7. protected $form_key;
  8. protected $cart;
  9. protected $product;
  10. protected $message_manager;
  11. public function __construct(Context$context,MagentoFrameworkControllerResultJsonFactory $resultJsonFactory,MagentoFrameworkAppRequestInterface $request,MagentoFrameworkDataFormFormKey $form_key,MagentoCheckoutModelCart $cart,MagentoCatalogModelProduct $product,MagentoFrameworkMessageManagerInterface $message_manager) {
  12. $this->request=$request;
  13. $this->resultJsonFactory=$resultJsonFactory;
  14. $this->form_key = $form_key;
  15. $this->cart = $cart;
  16. $this->product = $product;
  17. $this->message_manager = $message_manager;
  18. parent::__construct($context);
  19. }
  20. public function execute() {
  21. $result = $this->resultJsonFactory->create();
  22. $childs_product_arr=json_decode($this->request->getParam('childs_product_arr'));
  23. $parent_product=json_decode($this->request->getParam('parent_product_id'));
  24. $parent_product=$parent_product[0];
  25. try {
  26. $params = [];
  27. foreach ($childs_product_arr as $child_product){
  28. $options = [];
  29. $_product = $this->product->load($parent_product);
  30. $child_product=(array)$child_product;
  31. $param = array(
  32. 'form_key' => $this->form_key->getFormKey(),
  33. 'super_attribute' => $options,
  34. 'qty' => 1,
  35. 'selected_configurable_option' => $child_product['simple_product_id'],
  36. );
  37. $this->cart->addProduct($_product->getId(), $param);
  38.  
  39. }
  40. $this->cart->save();
  41. $msg="You added {$_product->getName()} to your shopping cart";
  42. $this->message_manager->addSuccess(__($msg));
  43. return $result->setData(['success' => true]);
  44. } catch (Exception $e) {
  45. $this->message_manager->addSuccess(__($e->getMessage()));
  46. return $result->setData(['success' => false,'error'=>$e->getMessage()]);
  47. }
  48. }
  49. }
Add Comment
Please, Sign In to add comment