0

Adding a product to the cart takes just over a minute even though I have a very decent server. On closer inspection, the customer/section/load/ request is taking over a minute to load.

Debugging it showed the Magento\Checkout\CustomerData\DirectoryData section that's slow loading whilst the other sections like cart, messages, jsdatalayer, load extremely quick.

This is the code that slow:

/**
 * {@inheritdoc}
 */
public function getSectionData()
{
    $output = [];
    $regionsData = $this->directoryHelper->getRegionData();
    /**
     * @var string $code
     * @var \Magento\Directory\Model\Country $data
     */
    foreach ($this->directoryHelper->getCountryCollection() as $code => $data) {
        $output[$code]['name'] = $data->getName();
        if (array_key_exists($code, $regionsData)) {
            foreach ($regionsData[$code] as $key => $region) {
                $output[$code]['regions'][$key]['code'] = $region['code'];
                $output[$code]['regions'][$key]['name'] = $region['name'];
            }
        }
    }
    return $output;
}

As as a quick dirty test I added a return statement just before the $this->directoryHelper->getRegionData(); which made the add to cart action complete in about 1.15s as opposed to over 1 minute.

How can this function be optimized?

Thanks.

2
  • Maybe directoryHelper calls 3rd party system in real time. It's hard to guess, but your above shouldn't take much time.
    – Sohel Rana
    Commented Jun 13, 2023 at 9:13
  • @SohelRana I debugged further and it doesn't call any 3rd party system in real time, but it does have a 3rd party module which adds a cities dropdown for the regions. The module's helper overrides the core Magento helper, and is not very optimized. It loops over the cities for every country when only 1 is needed. I updated the code and reduced the time from 0.9m to 0.04m. Further optimization is possible but it will do for now. Thanks for your input, it helped identify the offending code. Commented Jun 13, 2023 at 14:01

1 Answer 1

0

Please check below parameters in phpinfo

memory_limit 2048M 
max_execution_time 18000 
post_max_size 4096M
max_file_uploads 2000
max_input_time 1000
max_input_vars 1000

and check after disabling third-party modules and theme.

2
  • thanks for the input, but php variable tweaking was not the issue. It was simply bad code in a 3rd party module. Commented Jun 13, 2023 at 14:11
  • You are right, Third-party code impact on the performance, You can disable or rewrite the code
    – MP Raj
    Commented Jun 14, 2023 at 3:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.