0

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:

enter image description here

1 Answer 1

0

Replace the below code in your delivery-time.phtml file:

Replace the below code in your **delivery-time.phtml** file:

<?php

/* @var $block \Magento\Catalog\Block\Product\View */

$_product = $block->getProduct();

?>

<div class="delivery-time__wrapper">
    <span class="lb-icon-rocket"></span>
    <span><?= $block->escapeHtml($category) ?></span>
    <?php if (in_array("6",$product->getCategoryIds())): ?>
        <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>
3
  • Same error. Here is a full log: pastebin.com/SvTpp4tC
    – DeadSec
    Aug 6, 2021 at 10:08
  • Any other ideas?
    – DeadSec
    Aug 7, 2021 at 18:02
  • @DeadSec code updated... please try again with updated code. Aug 10, 2021 at 5:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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