Yanıtlar:
Bunu dene:
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
echo $product->getId();
echo $product->getName();
?>
VEYA
Blok dosyanıza feryat kodu ekleyin.
Örneğin app/code/AR/CustomModule/Block/CustomBlock.php
<?php
namespace AR\CustomModule\Block;
class CustomBlock extends \Magento\Framework\View\Element\Template
{
protected $_registry;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
array $data = []
)
{
$this->_registry = $registry;
parent::__construct($context, $data);
}
public function _prepareLayout()
{
return parent::_prepareLayout();
}
public function getCurrentCategory()
{
return $this->_registry->registry('current_category');
}
public function getCurrentProduct()
{
return $this->_registry->registry('current_product');
}
}
?>
Şablonunuzdaki (custom.phtml) dosyanızdaki geçerli ürün verilerini yazdırma
if ($currentProduct = $block->getCurrentProduct()) {
echo $currentProduct->getName() . '<br />';
echo $currentProduct->getSku() . '<br />';
echo $currentProduct->getId() . '<br />';
}
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
echo $product->getId();
echo $product->getName();
?>
Bu benim için çalışıyor.
.
Deneyebilirsin $product = $this->abstractProduct->getProduct();
ile \Magento\Catalog\Block\Product\AbstractProduct $abstractProduct
benim için çalıştı :)
Bunu dene:
<?php
$productId = 8;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$currentproduct = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
echo $currentproduct->getName();
?>