Question

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.

Was it helpful?

Solution

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');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top