How to get product details in CMS Block using product Id or SKU in Magento 2.
Magento2 CMS Block – Get Product Details Using Product ID or SKU
cms-blockmagento2productsku
Related Solutions
Out of the box you cannot retrieve the block title.
What you'll have to do is:
First, create your own block class that extends Magento\Cms\Block\Block
Optional: if you want the getTitle
method to be available for every static block you can setup a preference by creating a etc/di.xml
with the following content:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="Magento\Cms\Block\Block" type="Vendor\Module\Block\Cms\Block" />
</config>
Then, change your code with the following:
$myCmsBlock = $block->getLayout()->createBlock('Vendor\Module\Block\Cms\Block')->setBlockId('footer_impressum');
$blockTitle = $myCmsBlock->getTitle();
echo $myCmsBlock->toHtml();?>
Where Vendor\Module\Block\Cms\Block
is the class you have created to rewrite the original class.
Then create your Vendor\Module\Block\Cms\Block
class with the following content:
<?php
namespace Vendor\Module\Block\Cms;
class Block extends \Magento\Cms\Block\Block
{
protected $_blockRepository;
public function __construct(
\Magento\Framework\View\Element\Context $context,
\Magento\Cms\Model\Template\FilterProvider $filterProvider,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Cms\Model\BlockFactory $blockFactory,
\Magento\Cms\Api\BlockRepositoryInterface $blockRepository,
array $data = []
) {
parent::__construct($context, $filterProvider, $storeManager, $blockFactory, $data);
$this->_blockRepository = $blockRepository;
}
public function getTitle()
{
$blockId = $this->getBlockId();
$title = "";
if ($blockId) {
$block = $this->_blockRepository->getById($blockId);
$title = $block->getTitle();
}
return $title;
}
}
You need to create folder in root of your system and create one php file inside those folder,
Keep below content in your file and run from browser,
<?php
// MAGENTO START
include('app/bootstrap.php');
use Magento\Framework\App\Bootstrap;
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('sku', '24-MB01');
$collection->addAttributeToSelect('*');
foreach ($collection as $product)
{
$product->setData('short_description', 'test description');
$product->save();
echo "Product Updated". " ";
}
Best Answer
You can create a custom Widget and let widget field have an option called product Id, get the product Id into your template and display the details in the with widget .phtml.
You can add that widget into your cms static block.
We have a possibility to load product by creating custom module and pass the variables like this
{{block class="Vendor\Module\Block\Customproductload" product_id="1" template="Vendor_Module::customproductload.phtml"}}