Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Index extends MagentoFrameworkAppActionAction
- {
- protected $resultPageFactory;
- protected $session;
- private $productRepository;
- public function __construct(
- FormKey $formKey,
- Cart $cart,
- Product $product,
- MagentoFrameworkAppActionContext $context,
- MagentoCustomerModelSession $customerSession,
- MagentoCatalogApiProductRepositoryInterface $productRepository,
- PageFactory $resultPageFactory
- )
- {
- $this->session = $customerSession;
- $this->formKey = $formKey;
- $this->cart = $cart;
- $this->productRepository = $productRepository;
- $this->product = $product;
- parent::__construct($context);
- $this->resultPageFactory = $resultPageFactory;
- }
- public function execute()
- {
- if (!$this->session->isLoggedIn())
- {
- $resultRedirect = $this->resultRedirectFactory->create();
- $resultRedirect->setPath('customer/account/login');
- return $resultRedirect;
- }
- else
- {
- $resultPage = $this->resultPageFactory->create();
- $resultPage->getConfig()->getTitle()->set(__('My Wallet'));
- $sku = '';
- $vat_exempt_name = $this->getRequest()->getPost('vat_exempt_name');
- //...........Load Product...............//
- $product= $this->loadMyProduct();
- $ID = $product->getId();
- //.............. For Add to Cart .........//
- $productId =$ID;
- $params = array(
- 'form_key' => $this->formKey->getFormKey(),
- 'product' => $productId,
- 'qty' =>1
- );
- $product = $this->product->load($productId);
- $this->cart->addProduct($product, $params);
- $this->cart->save();
- return $resultPage;
- }
- }
- public function loadMyProduct()
- {
- if (empty($sku)){
- $this->createProduct();
- }
- else{
- return $this->productRepository->get($sku);
- }
- }
- public function createProduct(){
- $posted = $this->getRequest()->getParam('vat_exempt_name');
- $objectManager = MagentoFrameworkAppObjectManager::getInstance(); // instance of object manager
- $product = $objectManager->create('MagentoCatalogModelProduct');
- $product->setSku('my-sku44'); // I need this SKU to be set for global variable $sku
- $product->setName('Wallet Amount'); // Name of Product
- $product->setAttributeSetId(4); // Attribute set id
- $product->setStatus(1); // Status on product enabled/ disabled 1/0
- $product->setWebsiteIds(array(1));
- $product->setWeight(10); // weight of product
- $product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually)
- $product->setTaxClassId(0); // Tax class id
- $product->setTypeId('simple'); // type of product (simple/virtual/downloadable/configurable)
- $product->setPrice(199); // price of product
- $product->setStockData(
- array(
- 'use_config_manage_stock' => 0,
- 'manage_stock' => 1,
- 'is_in_stock' => 1,
- 'qty' => 99999
- )
- );
- $product = $product->save();
- $ID = $product->getId();
- //global $sku;
- $sku = 'my-sku45'; // here global variable $sku should update
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement