The scenario is I have multiple if-conditions to create product programmatically or load at controller action. I need a single variable who's value should be updating through different functions.
I have tried custom php ways but are not working. How can I achieve this?
Edit
Here is my class, I want to create product one(first) time and after that each time same product should gets load so that it could be added to cart programmatically. I am loading product using SKU. To read code please focus on $sku variable which I want to make global or static.
class Index extends \Magento\Framework\App\Action\Action
{
protected $resultPageFactory;
protected $session;
protected $sku;
private $productRepository;
public function __construct(
FormKey $formKey,
Cart $cart,
Product $product,
\Magento\Framework\App\Action\Context $context,
\Magento\Customer\Model\Session $customerSession,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
PageFactory $resultPageFactory
)
{
$this->session = $customerSession;
$this->formKey = $formKey;
$this->sku = $sku;
$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'));
$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($this->sku)){
$this->createProduct();
}
else{
return $this->productRepository->get($this->sku);
}
}
public function createProduct(){
$posted = $this->getRequest()->getParam('vat_exempt_name');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$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;
$this->sku = $product->getSku(); // here global variable $sku should update
}
}
Thanks.