I am using Magento 2.3.5-p2
I am adding a Google Tag Manager integration code at the Catalog Search, thus this requires me to modify the catalogsearch_result_index.xml
and add a new .phtml file which I called it gtm_body.phtml
this file will contain the GTM code integration
here is the code that I have updated at the
app/design/frontend/MageBig/martfury/layout01/Magento_CatalogSearch/layout/catalogsearch_result_index.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="div.sidebar.main">
<container name="div.sidebar.main.content" htmlTag="div" htmlClass="sidebar-main-content"/>
</referenceContainer>
<move element="categorymenu" destination="catalogsearch.leftnav" before="-"/>
<move element="div.sidebar.main" destination="columns" before="-"/>
<move element="main" destination="columns" after="div.sidebar.main"/>
</body>
<referenceContainer name="after.body.start">
<block class="MagentoCatalogSearchBlockResult" name="gtm.body" before="-" template="Magento_CatalogSearch::html/gtm_body.phtml" />
</referenceContainer>
</page>
here is the content for
app/design/frontend/MageBig/martfury/layout01/Magento_CatalogSearch/templates/html/gtm_body.phtml
<?php
$result = 'no results found';
var_dump($block->getResultCount());die;
if($block->getResultCount()){
$result = 'results found';
}
?>
<script>
dataLayer.push({
'event': 'trackEvent',
'eventDetails.category': 'site search',
'eventDetails.action': '<?php echo $result; ?>',
'eventDetails.label': '<?php echo $this->helper('MagentoCatalogSearchHelperData')->getEscapedQueryText(); ?>'
});
</script>
the $block->getResultCount()
are supposedly to carry the result for amount of product for the search result but error such as below appear
Fatal error: Uncaught Error: Call to a member function getLoadedProductCollection() on bool
in /var/www/ecmc/vendor/magento/module-catalog-search/Block/Result.php:182
Stack trace: #0 /var/www/ecmc/vendor/magento/module-catalog-search/Block/Result.php(206):
MagentoCatalogSearchBlockResult->_getProductCollection()
#1 /var/www/ecmc/app/design/frontend/MageBig/martfury/layout01/Magento_CatalogSearch/templates/html/gtm_body.phtml(5): MagentoCatalogSearchBlockResult->getResultCount()
#2 /var/www/ecmc/vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/var/www/ecmc...')
#3 /var/www/ecmc/vendor/magento/framework/View/Element/Template.php(271): MagentoFrameworkViewTemplateEnginePhp->render(Object(MagentoCatalogSearchBlockResult), '/var/www/ecmc...', Array)
#4 /var/www/ecmc/vendor/magento/framework/View/Element/Template.php(301): MagentoFrameworkViewElementTemplate->fetchView('/var/www/ecmc...')
#5 /var/www/ecmc/vendor/magento/framework/Vi in /var/www/ecmc/vendor/magento/module-catalog-search/Block/Result.php on line 182
I have debug this code by going through the block file at
vendor/magento/module-catalog-search/Block/Result.php
upon var_dump($block->getListBlock());
I noticed that this value return bool(false)
I am not able to understand what is causing this value to be false, the false value is what causing the function _getProductCollection()
unable to get the value for the getLoadedProductCollection
function
do help and explain on why this problem occur and how to solve this problem
Thank you