- Beranda
- Komunitas
- Tech
- Website, Webmaster, Webdeveloper
Declare the command to get product id and sku in Magento
TS
daniel8115
Declare the command to get product id and sku in Magento
You will use a block class of the module
[size={defaultattr}]
, then possibly inject the object of [/size]
[size={defaultattr}]
class in the constructor of the module’s block class.[/size]
Source: How to get product by id and sku in magento 2 | Mageplaza
PHP Code:
Mageplaza_HelloWorld
[size={defaultattr}]
, then possibly inject the object of [/size]
PHP Code:
MagentoCatalogModelProductRepository
[size={defaultattr}]
class in the constructor of the module’s block class.[/size]
PHP Code:
app/code/Mageplaza/HelloWorld/Block/HelloWorld.php
Code:
[color=#333333][size=2][font=Menlo, Monaco, Consolas, "courier new", monospace][php]<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
protected $_productRepository;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Catalog\Model\ProductRepository $productRepository,
array $data = []
)
{
$this->_productRepository = $productRepository;
parent::__construct($context, $data);
}
public function getProductById($id)
{
return $this->_productRepository->getById($id);
}
public function getProductBySku($sku)
{
return $this->_productRepository->get($sku);
}
}
?>[/php][/font][/size][/color]
Source: How to get product by id and sku in magento 2 | Mageplaza
0
2.5K
0
Tulis komentar menarik atau mention replykgpt untuk ngobrol seru
Tulis komentar menarik atau mention replykgpt untuk ngobrol seru
Komunitas Pilihan