daniel8115Avatar border
TS
daniel8115
Declare the command to get product id and sku in Magento
You will use a block class of the module 

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
GuestAvatar border
Guest
Tulis komentar menarik atau mention replykgpt untuk ngobrol seru
Mari bergabung, dapatkan informasi dan teman baru!
Website, Webmaster, Webdeveloper
Website, Webmaster, WebdeveloperKASKUS Official
23.3KThread4.3KAnggota
GuestAvatar border
Guest
Tulis komentar menarik atau mention replykgpt untuk ngobrol seru
Ikuti KASKUS di
© 2023 KASKUS, PT Darta Media Indonesia. All rights reserved.