yireo/Yireo_GoogleTagManager2

3.7.7 Release error

Closed this issue · 5 comments

report.CRITICAL: Exception: Warning: Undefined array key 490 in /app/x/vendor/yireo/magento2-googletagmanager2/Util/CategoryProvider.php on line 112 in /app/x/vendor/magento/framework/App/ErrorHandler.php:62

We do have multiple exceptions that prevent the catalog to load on the frontend since we upgraded the module to 3.7.7

Looks like the category registered on the product could not be loaded?

Maybe introducing a fail-safe try-catch + log error would be better than no products showing up.
I can provide a PR later

Thanks for reporting this. I think all of these warnings are given when products don't have categories. Release 3.7.8 should fix all of these warnings. If in the end, you think something else is at play, please let me know! :)

Hello @jissereitsma

I've checked the error this morning again and it appear that the error occurs when you have a product assigned to a category which does not belongs to the current store category root.
The category is not loaded because the code checks for category for the current root only:

$rootCategoryFilterGroup->setFilters([
            $this->filterBuilder
                ->setField('path')
                ->setConditionType('like')
                ->setValue('1/' . $this->getRootCategoryId() . '/%')
                ->create(),
        ]);

I think it's totally ok because we only want data from the current scope.
Also, when reading the code, it seems that the error could also occurs if the category is disabled. It's ok to not push the disabled categories to GTM, however as it's implemented it throws an exception (array key does not exists):

array_filter($this->loadedCategories, function (CategoryInterface $category) {
            return $category->getIsActive();
        });

This commit would fix the issue but it's not in the release: 772022f?diff=unified&w=1#diff-afa7e788642a6cff6138784d2bdd6a2523312326a1806148802e32a5a39c5277

Also the method getFirstByProduct could be improved to try next categories if the "first" one fails (does not belongs the root, or is disabled)

Here's my changes: #209

In the mean time, I released the fix anyway. But happy to release yet another release with your additional changes :)