Create short forms of Magento 2 commands in Linux

During development with Magento 2, we need to run the command repeatedly. And each time we need to type bin/magento command. In this article, I will show you how you can create short forms of Magento 2 commands in Linux. For example, you can clean the cache by only typing c:c without php bin/magento cache:clean…

Continue Reading Create short forms of Magento 2 commands in Linux

Get source by source code Multi Source Inventory(MSI) Magento 2

We can use Repository \Magento\InventoryApi\Api\SourceRepositoryInterface to get the source by code Here is the sample model class <?php namespace Mageprince\Testing\Model; class SourceModel { private $sourceRepository; public function __construct( ... \Magento\InventoryApi\Api\SourceRepositoryInterface $sourceRepository ... ) { $this->sourceRepository = $sourceRepository; } public function getSourcesByCode($sourceCode) { return $this->sourceRepository->get($storeCode); } } Now you can use getSourcesByCode() function to get the…

Continue Reading Get source by source code Multi Source Inventory(MSI) Magento 2

Get source by name Multi Source Inventory(MSI) Magento 2

You can get the source by using the repository \Magento\InventoryApi\Api\SourceRepositoryInterface and filter source name by using \Magento\Framework\Api\SearchCriteriaBuilder Magento has default source with name “Default Source”. You can't delete this source. You can get the source by its name, code, description, latitude, longitude and postcode Here is the example of retrieving the source data <?php namespace…

Continue Reading Get source by name Multi Source Inventory(MSI) Magento 2

Get source item data by source code and SKU Multi Source Inventory(MSI) Magento 2

We can use \Magento\InventoryApi\Api\SourceItemRepositoryInterface class with \Magento\Framework\Api\SearchCriteriaBuilder to get source item data by source code and product SKU. Here are the sample model class <?php namespace Mageprince\Testing\Model; class SourceItemModel { private $searchCriteriaBuilder; private $sourceItemRepository; public function __construct( ... \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, \Magento\InventoryApi\Api\SourceItemRepositoryInterface $sourceItemRepository ... ) { $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->sourceRepository = $sourceRepository; } public function getSourcesItems($souceCode,…

Continue Reading Get source item data by source code and SKU Multi Source Inventory(MSI) Magento 2

Get source item data by source code Multi Source Inventory(MSI) in Magento 2

We can use \Magento\InventoryApi\Api\SourceItemRepositoryInterface class to get source items Here are the sample model class to get the sources item by source code <?php namespace Mageprince\Testing\Model; class SourceItemModel { private $searchCriteriaBuilder; private $sourceItemRepository; public function __construct( ... \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, \Magento\InventoryApi\Api\SourceItemRepositoryInterface $sourceItemRepository ... ) { $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->sourceRepository = $sourceRepository; } public function getSourcesItems($souceCode) {…

Continue Reading Get source item data by source code Multi Source Inventory(MSI) in Magento 2

Get source item data by SKU Multi Source Inventory(MSI) Magento 2

You can get sources item by product SKU by class \Magento\InventoryApi\Api\GetSourceItemsBySkuInterface You can get source_item_id, source_code, sku, quantity and status I have created a sample model to show you how can you get sources item by SKU <?php namespace Mageprince\Testing\Model; class SourceListModel { private $sourceItemsBySku; public function __construct( ... \Magento\InventoryApi\Api\GetSourceItemsBySkuInterface $sourceItemsBySku, ... ) { $this->sourceItemsBySku…

Continue Reading Get source item data by SKU Multi Source Inventory(MSI) Magento 2

How to get list of sources Multi Source Inventory (MSI) programmatically in Magento 2

With the Magento version, 2.3 Multi-source inventory (MSI) Was Introduced. With the default Magento MSI, you can create multiple sources which contain stores data. like source_code, name, warehouse_id, latitude, longitude, etc. You can get all sources which are added by Magento admin by class Magento\InventoryApi\Api\SourceRepositoryInterface Here is the source collection factory class: Magento\Inventory\Model\ResourceModel\Source\CollectionFactory Here are…

Continue Reading How to get list of sources Multi Source Inventory (MSI) programmatically in Magento 2

Get admin URL in Magento 2

We can use \Magento\Backend\Helper\Data class to get Admin Url. /** * @var Magento\Backend\Helper\Data */ private $backendHelper; public function __construct( ... \Magento\Backend\Helper\Data $backendHelper ... ) { ... $this->backendHelper = $backendHelper; ... } public function getAdminUrl() { retrun $this->backendHelper->getHomePageUrl(); } You can get admin URL by getHomePageUrl() function of backend helper class Here you will get admin…

Continue Reading Get admin URL in Magento 2

Add a module in the composer with GitHub repository Magento 2

In this article, I will show you how we can add -magento 2 module in the composer. Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Read more 1. Add module repository into Github First of…

Continue Reading Add a module in the composer with GitHub repository Magento 2