0

I've created the below code, but it looks like I'm missing something. Can you help please?

<?php

require __DIR__ . '/../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\ObjectManager\ObjectManager $objectManager */
$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$registry = $objectManager->get('Magento\Framework\Registry');
$pageFactory = $objectManager->get('Magento\Framework\View\Result\PageFactory');

$productFactory = $objectManager->get('Magento\Catalog\Model\ProductFactory');
$product = $productFactory->create()->load(143767);
$registry->register('product', $product);
$resultRedirect = $pageFactory->create();

echo $resultRedirect->getLayout()
    ->createBlock("Magento\Catalog\Block\Product\ProductList\Related")
    ->setTemplate("Magento_Catalog::product/list/items.phtml")
    ->setData('type', 'related')
    ->toHtml();

(Magento version is 2.4.2). The problem is:

Fatal error:  Uncaught TypeError: Argument 2 passed to Magento\Catalog\Block\Product\ImageFactory::getLabel() must be of the type string, null given, called in /var/www/html/app/code/Magento/Catalog/Block/Product/ImageFactory.php on line 168 and defined in /var/www/html/app/code/Magento/Catalog/Block/Product/ImageFactory.php:117
Stack trace:
#0 /var/www/html/app/code/Magento/Catalog/Block/Product/ImageFactory.php(168): Magento\Catalog\Block\Product\ImageFactory-&gt;getLabel()
#1 /var/www/html/generated/code/Magento/Catalog/Block/Product/ImageFactory/Interceptor.php(23): Magento\Catalog\Block\Product\ImageFactory-&gt;create()
#2 /var/www/html/app/code/Magento/Catalog/Block/Product/ImageBuilder.php(135): Magento\Catalog\Block\Product\ImageFactory\Interceptor-&gt;create()
#3 /var/www/html/app/code/Magento/Catalog/Block/Product/AbstractProduct.php(518): Magento\Catalog\Block\Product\ImageBuilder-&gt;create()
#4 /var/www/html/app/code/Magento/Catalog/view/frontend/templates/product/list/items.phtml(216): Magento\Catalog\Block\Product\Abstr in /var/www/html/app/code/Magento/Catalog/Block/Product/ImageFactory.php on line 117
2
  • It is likely not successfully loading the product in the line $product = $productFactory->create()->load(143767);. Are you sure you have a product with this id?
    – joeybab3
    Jul 8 at 22:37
  • Yes, I'm absolutely sure I have a product with this id because I can get any attributes of the product: $product = $productFactory->create()->load(143767); echo $product->getSku(); // G08333300145G
    – Pontorez
    Jul 8 at 22:47
0

Fixed using https://magento.stackexchange.com/a/329169/81352 . The below code works fine.

<?php

require __DIR__ . '/../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\ObjectManager\ObjectManager $objectManager */
$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$areaList = $objectManager->get('Magento\Framework\App\AreaList');
$areaList->getArea(Magento\Framework\App\Area::AREA_FRONTEND)->load(Magento\Framework\App\Area::PART_DESIGN);

$registry = $objectManager->get('Magento\Framework\Registry');
$pageFactory = $objectManager->get('Magento\Framework\View\Result\PageFactory');

$productFactory = $objectManager->get('Magento\Catalog\Model\ProductFactory');
$product = $productFactory->create()->load(143767);
$registry->register('product', $product);
$resultRedirect = $pageFactory->create();

echo $resultRedirect->getLayout()
    ->createBlock("Magento\Catalog\Block\Product\ProductList\Related")
    ->setTemplate("Magento_Catalog::product/list/items.phtml")
    ->setData('type', 'related')
    ->toHtml();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.