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

Use of __desctruct() in classes to free memory

Updated July 20, 2017 13:09 PM

Magento 2, calculate memory zise need for process

Updated June 29, 2018 03:09 AM

Memory leaks in long-running loop

Updated March 27, 2017 20:09 PM