so I'm trying to check if the category ID is 6 to change the text that appears in delivery time, but when I try to call the $_product
, it says that it's null
, and I can't figure out why.
Any help would really be appreciated.
delivery-time.phtml:
<?php
/* @var $block \Magento\Catalog\Block\Product\View */
$_product = $block->getProduct();
$category = $_product->getCategoryId();
?>
<div class="delivery-time__wrapper">
<span class="lb-icon-rocket"></span>
<span><?= $block->escapeHtml($category) ?></span>
<?php if ($category && $category == 6): ?>
<span class="delivery-time__text">
<?= __("Delivery in 6-12 weeks"); ?>
</span>
<?php else: ?>
<span class="delivery-time__text">
<?= __("Delivery in 2-3 business days"); ?>
</span>
<?php endif; ?>
</div>
Erro:
Error: Call to a member function getCategoryId() on null
Delivery Time Block:
<block class="Magento\Catalog\Block\Product\View" name="product.info.delivery.time" template="Magento_Catalog::product/view/delivery-time.phtml"/>
catalog_product_view.xml:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<remove src="mage/gallery/gallery.css"/>
</head>
<body>
<referenceContainer name="content">
<block class="Tmg\Breadcrumbs\Block\Breadcrumbs"
name="breadcrumbs.mobile" as="breadcrumbs.mobile"
template="Magento_Theme::html/breadcrumbs.phtml"
before="-">
<arguments>
<argument name="viewModel" xsi:type="object">Magento\Catalog\ViewModel\Product\Breadcrumbs</argument>
</arguments>
</block>
<container name="product.top" htmlClass="pdp__top group" htmlTag="div" after="breadcrumbs.mobile"/>
<block class="Magento\Catalog\Block\Product\View" name="product.info.delivery.time" template="Magento_Catalog::product/view/delivery-time.phtml"/>
<block class="Magento\Catalog\Block\Product\ProductList\Crosssell" name="catalog.product.crosssell" template="Magento_Catalog::product/list/items.phtml">
<arguments>
<argument name="type" xsi:type="string">crosssell</argument>
</arguments>
</block>
<block class="Tmg\Breadcrumbs\Block\Breadcrumbs" name="breadcrumbs" template="Magento_Theme::html/breadcrumbs.phtml" before="-" />
<block name="product.info.sizeguide" class="Magento\Catalog\Block\Product\View" template="Magento_Catalog::product/view/sizeguide.phtml"/>
<block name="product.info.color" class="Magento\Catalog\Block\Product\View" template="Magento_Catalog::product/view/color-info.phtml"/>
</referenceContainer>
</body>
</page>
File directory layout: