Answers:
尝试这个:
<?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();
?>
要么
在您的阻止文件中添加下面的代码。
例如 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');
}
}
?>
在模板(custom.phtml)文件中打印当前产品数据
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();
?>
这对我有用。
。
你可以试试 $product = $this->abstractProduct->getProduct();
与 \Magento\Catalog\Block\Product\AbstractProduct $abstractProduct
为我工作:)
尝试这个:
<?php
$productId = 8;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$currentproduct = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
echo $currentproduct->getName();
?>