Trying to make certain category names dynamic results in 100% server memory usage

by Kaascroissant   Last Updated January 13, 2018 00:09 AM

My custom module has certain categories that are linked to certain customers and should return that customer's company name instead of the category name, I have written a plugin like this:

public function afterGetName(Category $subject, $result) {
    $customerCollection = $this->customerCollectionFactory
        ->addFieldToFilter('custom_category_id', $subject->getId())

    if ($customerCollection->getSize()) {
        return $this->customerRepository->getById($customerCollection->getFirstItem()->getId())->getAddresses()[0]->getCompany();

    return $result;

This works perfectly, but when I clear the cache the server starts using 100% memory and will slow to a crawl. I have narrowed it down to the $this->customerRepository->getById call, but I don't see how this seemingly results in an infinite recursion. I have tried using an AddressRepository instead, but that somehow breaks all $customer->getCustomAttribute calls.

Related Questions

Magento customer save memory leak

Updated May 17, 2015 21:04 PM

Magento2: Intercept category moved inside tree

Updated August 24, 2018 17:09 PM

Use of __desctruct() in classes to free memory

Updated July 20, 2017 13:09 PM

Memory usage high for specific time?

Updated September 06, 2018 11:09 AM