How to get a product by ID in Magento 2
If you want to get a product by ID in Magento 2, the platform offers several different methods to do that. And since it is one of the most frequently used operations in a programmer’s routine, we describe every approach below. Each method is unique and suitable for different use cases. You can utilize them to get products in third-party modules, scripts, or phtml files. Therefore, let’s see how to get products by ID in Magento 2. More coding tips are available here: Magento 2 Developer’s Cookbook.
Table of contents
How to get a product by ID in Magento 2
In the following article, we describe three different ways of getting products by ID in Magento 2:
- By factory;
- By object;
- By repository.
By factory
The default way of getting products and other models in Magento 2 is on factories, which represent a new design pattern implemented in the shop software. Use the following code snippet to get a product by a factory in Magento 2:
1 2 3 4 5 6 7 8 9 10 |
protected $_productFactory; ... public function __construct( ... \Magento\Catalog\Model\ProductFactory $productFactory ) { ... $this->_productFactory = $productFactory; ... } |
To get a product by ID, utilize the following command:
1 |
$product = $this->_productloader->create()->load($id); |
By object
The second option, which has been introduced in the time of Magento 1, is to get a product by ID using an object method. You can get an object manager instance from everywhere since it is a singleton class. It is recommended to save this instance in your __construct method. Thus, you will reduce the number of code lines. To get a product by ID in Magento 2 via the object method, use the following code:
1 2 |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id); |
By repository
Another approach that is not represented in 1.x is based on repositories. They introduce another design pattern that can be utilized to get objects. Use the following code snippet to get product objects:
1 2 3 4 5 6 7 8 9 10 |
protected $_productRepository; ... public function __construct( ... \Magento\Catalog\Api\ProductRepositoryInterface $productRepository ) { ... $this->_productRepository = $productRepository; ... } |
Next, you can get a product by ID via the following command:
1 |
$product = $this->_productRepository->getById($id); |
Alternatively, it is possible to get a product by SKU. You can utilize the command below:
1 |
$product = $this->_productRepository->get($sku); |
For further information, follow this link:
How to import products in Magento 2
Before getting any products in Magento 2, you may have to import them to your store. Below, we shed light on how to do that in the most user-friendly manner.
Automated Import & Export
Our module lets you automate product import and export as well as other data transfers via schedules or event-based triggers.
Schedules
The Improved Import & Export Magento 2 extension uses cron, allowing you to create any custom schedules of updates. At the same time, you can select one of the predefined values transferring products to your store automatically. Note that the preset intervals are customizable as well.
Import and export jobs with no schedules represent another opportunity provided by our module. Also, note that it is possible to launch every scheduled profile whenever you want. As for the configuration process, it is as simple as the following gif illustrates:
Events
Alternatively, the Improved Import & Export Magento 2 extension provides the ability to create event-based triggers. It is a quite handy feature when it comes to import and export processes. For instance, you can transfer order data to an ERP system every time a new order is placed automatically. Follow the link below for more information regarding triggers: How to Run Magento 2 Import or Export After Specific System Event or Process.
Advanced Mapping Features
Advanced mapping opportunities is another great feature that does our module better than proposed solutions. When you import products from Magento 1 or other external systems, our module lets you replace third-party attributes with ones used in Magento 2 to enable the import procedure. There is no need to do anything in data files.
Mapping Presets
Mapping presets introduce the most straightforward way of importing/exporting any data with the Improved Import & Export extension. The mechanism behind them is quite intuitive: you select a preset of a third-party system you are going to connect to, and the module replaces all the unsupported attributes automatically. Take a look at the following gif image to see how simple this process is:
Matching Interface
Alternatively, you can match attributes manually in a corresponding section of an import or export profile. Select a third-party value and specify an internal one in front of it. You can also add a default attribute value which is provided to all items related to the attribute. You can see the illustration of this feature below:
Attribute Values Mapping
In addition to attributes, the Improved Import & Export Magento 2 extension provides the ability to map their values, simplifying product import processes even more. The algorithm is the same as the one described above. For more details, read this article: Attribute Values Mapping.
Attribute Values Editing
If the previous feature is not enough, our module lets you edit attribute values in bulk. The Improved Import & Export extension supports the following commands:
- Add a prefix to multiple attribute values;
- Add a suffix to numerous attribute values;
- Split various attribute values;
- Merge many attribute values.
You can combine them and create conditions increasing the efficiency of product data import. Check this article for further information: How to Modify Attribute Values During Import and Export in Magento 2.
Category Mapping
Category mapping dramatically simplifies product import processes. Our module lets you match external categories to ones used internally, reducing the number of difficulties that usually occur when products are transferred from other platforms to Magento 2. The Improved Import & Export module allows mapping external product categories to ones used in your catalog as follows:
To create new categories right in the import job, choose a parent category, and specify a new one that will be generated automatically. You can find more information about this feature here: Category Mapping.
Attributes On The Fly
And if a product data file provided from an external system lacks attributes, the Improved Import & Export Magento 2 extension lets you create them on the fly via the following general form:
Attribute|attribute_property_name:attribute_property_value|…
The feature is described here in more detail: Product attributes import.
Extended Connectivity Options
Another fundamental difference between Improved Import & Export and the default data transfer tool of Magento 2 or even third-party solutions is the ability to work with numerous file sources.
Multiple File Standards
Firstly, our module supports various file standards. In addition to the default CSV, you can import products using XML, JSON, ODS, and Excel file formats. The module also works with file archives!
Multiple File Sources
Support for numerous sources of data files is another benefit of the Improved Import & Export Magento 2 extension. Our module lets you import products from:
- FTP/SFTP. Transfer data files using a local or remote server.
- Dropbox. Use a Dropbox account to establish a connection between the two systems. Alternatively, you can rely on Box, OneDrive, Google Drive, iCloud, and Amazon Drive.
- URL. A direct URL can be used to import a data file as well.
Alternative Ways of Import & Export
Note that the following alternative ways of import and export are not represented in Magento 2 by default. The Improved Import & Export extension provides the ability to leverage REST, SOAP, and GraphQL APIs to import product data from external systems. However, you can transfer any information between your store and ERPs, accounting platforms, CRMs, etc. Note that all API connections support other extension’s features: mapping, schedules, and attributes on the fly while transferring data via API.
It is also possible to use Google Sheets, Office 365 Excel, and Zoho Sheet to transfer product information to your e-commerce store. The following image shows how to use Google Sheets to move data to Magento 2:
And of course, the Improved Import & Export extension opens completely new possibilities with support for WSDL and WADL.
The video below displays other details of our extension:
Final Words
As you can see, Magento 2 provides a wide range of options to get a product by ID. Leverage the approach that suits your needs. And if you are looking for the most powerful and at the same time simple way to import products and other entities to Magento 2, follow this link:
Get Improved Import & Export Magento 2 Extension