1

I am creating product programmatically on controller action. I want to send price value from template file. This is my controller:

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')); return $resultPage; $vat_exempt_name = $this->getRequest()->getPost('vat_exempt_name'); //.............To Create Product...........// $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager $product = $objectManager->create('\Magento\Catalog\Model\Product'); $product->setSku('my-sku28'); // Set your sku here $product->setName('Sample Simple Product28'); // 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($vat_exempt_name); // price of product $product->setStockData( array( 'use_config_manage_stock' => 0, 'manage_stock' => 1, 'is_in_stock' => 1, 'qty' => 99999 ) ); $product = $product->save(); } } 

This is my template file:

<table> <tr> <th>Enter Amount to be Added in wallet (USD)</th> </tr> <tr> <td><input type="text" class="input-text watch-keyup" id="vat_exempt_name" name="vat_exempt_name" value="200"></td> </tr> <tr> <td><button type="button">Add Money to Wallet</button></td> </tr> 

How can I achieve it, Thanks.

6
  • 1
    you are so supposed to put it on a form Commented May 27, 2019 at 7:51
  • Is it an ajax request? However you gotta to send the data via post request to pass data to controller Commented May 27, 2019 at 7:51
  • Yes.. I looking for some example to achieve it.. Commented May 27, 2019 at 7:53
  • 1
    check this magento.stackexchange.com/questions/202692/… and this magento.stackexchange.com/questions/149781/… Commented May 27, 2019 at 8:00
  • What you mean by ` send price value from template file` .From where you send from and where to? Commented May 27, 2019 at 9:16

1 Answer 1

2

To send values from phtml file to controller use form in phtml file with controller action

<form action="<?php echo $this->getUrl('path/tothe/controller') ?>" method="post"> <input type="text" class="input-text watch-keyup" id="vat_exempt_name" name="vat_exempt_name" value="200"> <button type="submit">Add Money to Wallet</button> </form> 

And you can get this value in your controller below

public function execute() { $posted = $this->getRequest()->getParam('vat_exempt_name'); } 
2
  • Thanks for simple solution. Commented May 27, 2019 at 10:33
  • Welcome...:-).. Commented May 27, 2019 at 10:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.