Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Analytics throws Fatal Error #3435

Closed
alexh-swdev opened this issue Aug 9, 2023 · 3 comments · Fixed by alexh-swdev/magento-lts#1
Closed

Google Analytics throws Fatal Error #3435

alexh-swdev opened this issue Aug 9, 2023 · 3 comments · Fixed by alexh-swdev/magento-lts#1

Comments

@alexh-swdev
Copy link
Contributor

alexh-swdev commented Aug 9, 2023

Preconditions (*)

  1. OM 20.1.0
  2. Multishop
  3. Product in mulitple categories in multiple shops
  4. PHP Version 8.1.16 (in docker)

Steps to reproduce (*)

  1. Add such a product to cart

Expected result (*)

  1. Product is added to cart without errors

Actual result (*)

Fatal error: Uncaught TypeError: Mage_GoogleAnalytics_Helper_Data::getLastCategoryName(): Return value must be of type string, null returned in /app/app/code/core/Mage/GoogleAnalytics/Helper/Data.php:186
Stack trace:
#0 /app/app/code/core/Mage/GoogleAnalytics/Model/Observer.php(88): Mage_GoogleAnalytics_Helper_Data->getLastCategoryName()

#1 /app/app/code/core/Mage/Core/Model/App.php(1441): Mage_GoogleAnalytics_Model_Observer->processItemsAddedOrRemovedFromCart()
#2 /app/app/code/core/Mage/Core/Model/App.php(1419): Mage_Core_Model_App->_callObserverMethod()
#3 /app/app/Mage.php(509): Mage_Core_Model_App->dispatchEvent()
#4 /app/app/code/core/Mage/Core/Model/Abstract.php(509): Mage::dispatchEvent()
#5 /app/app/code/core/Mage/Sales/Model/Quote/Item.php(788): Mage_Core_Model_Abstract->_afterSave()
#6 /app/app/code/core/Mage/Core/Model/Abstract.php(380): Mage_Sales_Model_Quote_Item->_afterSave()
#7 /app/app/code/core/Mage/Sales/Model/Quote/Item.php(771): Mage_Core_Model_Abstract->save()
#8 /app/app/code/core/Mage/Core/Model/Resource/Db/Collection/Abstract.php(643): Mage_Sales_Model_Quote_Item->save()
#9 /app/app/code/core/Mage/Sales/Model/Quote.php(380): Mage_Core_Model_Resource_Db_Collection_Abstract->save()
#10 /app/app/code/core/Mage/Core/Model/Abstract.php(380): Mage_Sales_Model_Quote->_afterSave()
#11 /app/app/code/core/Mage/Sales/Model/Quote.php(2087): Mage_Core_Model_Abstract->save()
#12 /app/app/code/core/Mage/Checkout/Model/Cart.php(450): Mage_Sales_Model_Quote->save()
#13 /app/app/code/core/Mage/Checkout/controllers/CartController.php(229): Mage_Checkout_Model_Cart->save()
#14 /app/app/code/core/Mage/Core/Controller/Varien/Action.php(421): Mage_Checkout_CartController->addAction()
#15 /app/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(255): Mage_Core_Controller_Varien_Action->dispatch()
#16 /app/app/code/core/Mage/Core/Controller/Varien/Front.php(181): Mage_Core_Controller_Varien_Router_Standard->match()
#17 /app/app/code/core/Mage/Core/Model/App.php(358): Mage_Core_Controller_Varien_Front->dispatch()
#18 /app/app/Mage.php(743): Mage_Core_Model_App->run()
#19 /app/index.php(71): Mage::run()
#20 {main} thrown in /app/app/code/core/Mage/GoogleAnalytics/Helper/Data.php on line 186

Possible Fix

The problem is, that the _categorieIds contains ALL its categories, of all its subshops. The poped category is not from the subshop from where it is added to the cart. Probably that's why the category is not loaded and its name is null.

These are just examples on the top of my head, but you are more into the whole OM code and its workflow
Adjust

Mage_GoogleAnalytics_Helper_Data::getLastCategoryName($product): string

to either allow null:

Adjust Mage_GoogleAnalytics_Helper_Data::getLastCategoryName($product): ?string

or loop through the categories until a valid one is found from

    /**
     * Returns last category name
     *
     * @param Mage_Catalog_Model_Product $product
     * @return string
     */
    public function getLastCategoryName($product): string
    {
        $_categoryIds = $product->getCategoryIds();
        if ($_categoryIds) {
            $_lastCat = array_pop($_categoryIds);
            $_cat = Mage::getModel('catalog/category')->load($_lastCat);
            return $_cat->getName();
        }
        return '';
    }

to

$_categoryIds = $product-getCategoryIds();
if ($_categoryIds) {
	$catName = "";
	do {
		$_lastCat = array_pop($_categoryIds);
		$_cat = Mage::getModel('catalog/category')-load($_lastCat);
		$catName = $_cat-getName();
	} while(empty($catName));
	return $catName ?? "";
}
return '';
@fballiano
Copy link
Contributor

I like the last implementation, @daboss84 would you create a PR?

@alexh-swdev
Copy link
Contributor Author

Good morning @fballiano , I created your requested pull request.

@fballiano
Copy link
Contributor

Thanks @daboss84 , I’m out for a few days but I’ll check it out asap!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants