??Add New Integration button and fill out anything required
You can access full API documentation for a Magento instance at <BaseURL>/swagger. Make sure to apply the Access Token in the upper right to get full access
We discussed how to create Install Schema and Upgrade Schema in Magento 2. So you can see the tutorial and create Install Schema or Upgrade Schema. Install Schema run only one time for insert data when installing the module. If you need to add some other fields then you can create Upgrade Schema using this tutorial.
We are already learned how to create a basic module in Magento 2. We need to create module.xml, registration.php, routes.xml, and Controller file. You can create module.xml, registration.php, and routes.xml from this tutorial. Now we create controller file Index.php in app/code/Thecoachsmb/Crud/Controller/Index with the following code.
<?php
namespace Thecoachsmb\Crud\Controller\Index;
use Magento\Framework\App\Action\Action;
class Index extends Action
{
protected $_resultPageFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory)
{
$this->_resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
public function execute()
{
echo "Module Cretaed Successfully";
}}
Now, run the command for cache clean and run URL yourdomain/mymodule/index/index in your browser.
Let’s check and follow this tutorial for how to create Models, Resource Models, and Collections to manage data in the table.
Now, add the below code in execute method in the controller file.
public function execute()
{
return $this->_resultPageFactory->create();
}
Create layout file mymodule_index_index.xml in app/code/Thecoachsmb/MyModule/view/frontend/layout folder with the following code.
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceContainer name="content">
<block class="Thecoachsmb\MyModule\Block\Index" name="mymodule_index_index" template="Thecoachsmb_MyModule::index.phtml" />
</referenceContainer>
</page>
Create block file Index.php in app/code/Thecoachsmb/MyModule/Block folder with the following code
<?php
namespace Thecoachsmb\MyModule\Block;
use Magento\Framework\View\Element\Template;
use Thecoachsmb\MyModule\Model\ContactFactory;
class Index extends Template
{
protected $_contactFactory;
public function __construct(
Template\Context $context,
ContactFactory $contactFactory)
{
parent::__construct($context);
$this->_contactFactory = $contactFactory;
}
public function getData()
{
$contact = $this->_contactFactory->create();
$collection = $contact->getCollection();
return $collection;
}}
Create template file index.phtml in app/code/Thecoachsmb/MyModule/view/frontend/templates folder with the following code.
<table>
<tr>
<th><?= __('Name'); ?></th>
<th><?= __('Email'); ?></th>
<th><?= __('Contact No'); ?></th>
<th><?= __('Message'); ?></th>
<th><?= __('Created At'); ?></th>
<th><?= __('Updated At'); ?></th>
<th colspan="2"><?= __('Action'); ?></th>
</tr>
<?php
$collectionData = $block->getData();
foreach($collectionData as $collection):
?>
<tr>
<td><?= $collection->getName(); ?></td>
<td><?= $collection->getEmail(); ?></td>
<td><?= $collection->getPhoneNo(); ?></td>
<td><?= $collection->getMessage(); ?></td>
<td><?= $collection->getCreatedAt(); ?></td>
<td><?= $collection->getUpdatedAt(); ?></td>
<td>
<a href="<?= $block->getUrl('mymodule/index/edit').'id/'.$collection->getId(); ?>" class="action primary">Edit</a>
</td>
<td>
<a href="<?= $block->getUrl('mymodule/index/delete').'?id='.$collection->getId(); ?>" class="action primary">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>
Now. run the command for cache clean and run the URL yourdomain/mymodule/index/index in your browser.
Create controller and action for insert action. We can add the following code to the index.phtml file in app/code/Thecoachsmb/MyModule/view/frontend/templates folder.
<a class="action primary" href="<?= $block->getUrl('mymodule/index/insert'); ?>">Add Record</a>
Now, create controller file Insert.php in app/code/Thecoachsmb/MyModule/Controller/Index folder with the following code
<?php
namespace Thecoachsmb\MyModule\Controller\Index;
class Insert extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory)
{
$this->_pageFactory = $pageFactory;
return parent::__construct($context);
}
public function execute()
{
return $this->_pageFactory->create();
}}
Create layout file mymodule_index_insert.xml in app/code/Thecoachsmb/MyModule/view/frontend/layout folder with the following code.
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceContainer name="content">
<block class="Thecoachsmb\MyModule\Block\Insert" name="mymodule_index_insert" template="Thecoachsmb::insertdata.phtml" />
</referenceContainer>
</page>
Create block file Insert.php in app/code/Thecoachsmb/MyModule/Block folder with the following code.
<?php
namespace Thecoachsmb\MyModule\Block;
class Insert extends \Magento\Framework\View\Element\Template
{
protected $_pageFactory;
protected $_postLoader;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory)
{
$this->_pageFactory = $pageFactory;
return parent::__construct($context);
}
public function execute()
{
return $this->_pageFactory->create();
}}
Create template file insertdata.phtml in app/code/Thecoachsmb/MyModule/view/frontend/templates folder with the following code.
<?php $data = $block->getEditData(); ?>
Now, create a controller for the save action. Create file Save.php in app/code/Thecoachsmb/MyModule/Controller/Index folder with the following code.
<?php
namespace Thecoachsmb\MyModule\Controller\Index;
class Save extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
protected $_contactFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\Thecoachsmb\MyModule\Model\ContactFactory $contactFactory)
{
$this->_pageFactory = $pageFactory;
$this->_contactFactory = $contactFactory;
return parent::__construct($context);
}
public function execute()
{
if ($this->getRequest()->isPost()) {
$input = $this->getRequest()->getPostValue();
$postData = $this->_contactFactory->create();
if($input[‘editId’]){
$postData->load($input[‘editId’]);
$postData->addData($input);
$postData->setId($input[‘editId’]);
$postData->save();
}else{
$postData->setData($input)->save();
}
return $this->_redirect(‘mymodule/index/index’);
}}}
Create a controller for the edit action. Create file Edit.php in app/code/Thecoachsmb/MyModule/Controller/Index folder with the following code.
<?php
namespace Thecoachsmb\MyModule\Controller\Index;
class Edit extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
protected $_request;
protected $_coreRegistry;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\Magento\Framework\App\Request\Http $request,
\Magento\Framework\Registry $coreRegistry)
{
$this->_pageFactory = $pageFactory;
$this->_request = $request;
$this->_coreRegistry = $coreRegistry;
return parent::__construct($context);
}
public function execute()
{
$id = $this->_request->getParam('id');
$this->_coreRegistry->register('editId', $id);
return $this->_pageFactory->create();
}}
Create layout file mymodule_index_edit.xml in app/code/Thecoachsmb/MyModule/view/frontend/layout folder with the following code.
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceContainer name="content">
<block class="Thecoachsmb\MyModule\Block\Edit" name="mymodule_index_edit" template="Thecoachsmb_MyModule::insertData.phtml" />
</referenceContainer>
</page>
Create block file Edit.php in app/code/Thecoachsmb/MyModule/Block folder with the following code.
<?php
namespace Thecoachsmb\MyModule\Block;
class Edit extends \Magento\Framework\View\Element\Template
{
protected $_pageFactory;
protected $_coreRegistry;
protected $_contactLoader;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\Magento\Framework\Registry $coreRegistry,
\Thecoachsmb\MyModule\Model\ContactFactory $contactLoader,
array $data = [])
{
$this->_pageFactory = $pageFactory;
$this->_coreRegistry = $coreRegistry;
$this->_contactLoader = $contactLoader;
return parent::__construct($context,$data);
}
public function execute()
{
return $this->_pageFactory->create();
}
public function getEditData()
{
$id = $this->_coreRegistry->registry('editId');
$postData = $this->_contactLoader->create();
$result = $postData->load($id);
$result = $result->getData();
return $result;
}}
Run the command for cache clean and check the result with URL yourdomain/mymodule/index/index.
Create Controller file for the Delete data. Create Delete.php file in app/code/Thecoachsmb/MyModule/Controller/Index folder with the following code.
<?php
namespace Thecoachsmb\MyModule\Controller\Index;
class Delete extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
protected $_request;
protected $_contactFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\Magento\Framework\App\Request\Http $request,
\Thecoachsmb\MyModule\Model\ContactFactory $contactFactory)
{
$this->_pageFactory = $pageFactory;
$this->_request = $request;
$this->_contactFactory = $contactFactory;
return parent::__construct($context);
}
public function execute()
{
$id = $this->_request->getParam('id');
$postData = $this->_contactFactory->create();
$result = $postData->setId($id);
$result = $result->delete();
return $this->_redirect('mymodule/index/index');
}}
MagentoProduct mProd;
if (!await _mageDB.RancocatalogProductEntities.AsQueryable().AnyAsync(p => p.Sku == prod.Key.Id))
{
// No existing product; Create it.
mProd = new()
{
Attribute_set_id = attributeSetID,
Sku = prod.Key.Id,
Name = prodInfo.StoreName.NullIfWhitespace() ?? prod.Description,
Visibility = 1, // VISIBILITY_NOT_VISIBLE
Status = 2, // STATUS_DISABLED - obviously not written by a C programmer.
Extension_attributes = new()
{
Stock_item = new()
{
Max_sale_qty = 9999,
Min_sale_qty = 1,
},
},
};
await _magentoService.CreateProduct(mProd);
LogInfo($"\tCreated product in Magento named {mProd.Name} with visibility 1 (not visible) and status 2 (disabled)");
}
else
{
mProd = await _magentoService.ReadProduct(prod.Key.Id);
var newShipments = await (await _magentoService.ListShipmentsBy(SearchCriteria.WithAllOf(
new Filter("created_at", Op.GtEq, compareDate.ToMagentoString()),
new Filter("order_id", Op.Eq, "635"))))
.ToAsyncEnumerable()
// Skip outright bad data
.Where(o => o.Customer_id is not null && o.Order_id is not null)
.SelectAwait(async o => (
shipment: o,
cust: await _mageDB.RancocustomerGridFlats.AsQueryable().SingleAsync(c => c.EntityId == o.Customer_id),
order: await _magentoService.ReadOrder((uint)o.Order_id!.Value)))
.ToListAsync(cancellationToken: token)
POST /rest/V1/orders
Phase 2