Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. class Index extends MagentoFrameworkAppActionAction
  2. {
  3. protected $resultPageFactory;
  4. protected $session;
  5. private $productRepository;
  6. public function __construct(
  7. FormKey $formKey,
  8. Cart $cart,
  9. Product $product,
  10. MagentoFrameworkAppActionContext $context,
  11. MagentoCustomerModelSession $customerSession,
  12. MagentoCatalogApiProductRepositoryInterface $productRepository,
  13. PageFactory $resultPageFactory
  14. )
  15. {
  16. $this->session = $customerSession;
  17. $this->formKey = $formKey;
  18. $this->cart = $cart;
  19. $this->productRepository = $productRepository;
  20. $this->product = $product;
  21. parent::__construct($context);
  22. $this->resultPageFactory = $resultPageFactory;
  23. }
  24. public function execute()
  25. {
  26. if (!$this->session->isLoggedIn())
  27. {
  28. $resultRedirect = $this->resultRedirectFactory->create();
  29. $resultRedirect->setPath('customer/account/login');
  30. return $resultRedirect;
  31. }
  32. else
  33. {
  34. $resultPage = $this->resultPageFactory->create();
  35. $resultPage->getConfig()->getTitle()->set(__('My Wallet'));
  36. $sku = '';
  37.  
  38. $vat_exempt_name = $this->getRequest()->getPost('vat_exempt_name');
  39. //...........Load Product...............//
  40. $product= $this->loadMyProduct();
  41. $ID = $product->getId();
  42.  
  43. //.............. For Add to Cart .........//
  44. $productId =$ID;
  45. $params = array(
  46. 'form_key' => $this->formKey->getFormKey(),
  47. 'product' => $productId,
  48. 'qty' =>1
  49. );
  50. $product = $this->product->load($productId);
  51. $this->cart->addProduct($product, $params);
  52. $this->cart->save();
  53. return $resultPage;
  54. }
  55. }
  56. public function loadMyProduct()
  57. {
  58. if (empty($sku)){
  59. $this->createProduct();
  60. }
  61. else{
  62. return $this->productRepository->get($sku);
  63. }
  64. }
  65. public function createProduct(){
  66. $posted = $this->getRequest()->getParam('vat_exempt_name');
  67. $objectManager = MagentoFrameworkAppObjectManager::getInstance(); // instance of object manager
  68. $product = $objectManager->create('MagentoCatalogModelProduct');
  69. $product->setSku('my-sku44'); // I need this SKU to be set for global variable $sku
  70. $product->setName('Wallet Amount'); // Name of Product
  71. $product->setAttributeSetId(4); // Attribute set id
  72. $product->setStatus(1); // Status on product enabled/ disabled 1/0
  73. $product->setWebsiteIds(array(1));
  74. $product->setWeight(10); // weight of product
  75. $product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually)
  76. $product->setTaxClassId(0); // Tax class id
  77. $product->setTypeId('simple'); // type of product (simple/virtual/downloadable/configurable)
  78. $product->setPrice(199); // price of product
  79. $product->setStockData(
  80. array(
  81. 'use_config_manage_stock' => 0,
  82. 'manage_stock' => 1,
  83. 'is_in_stock' => 1,
  84. 'qty' => 99999
  85. )
  86. );
  87. $product = $product->save();
  88. $ID = $product->getId();
  89. //global $sku;
  90. $sku = 'my-sku45'; // here global variable $sku should update
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement