Skip to content

Magento 2.4.4 add to cart very slow (php 8.1)

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 MagentoCheckoutCustomerDataDirectoryData 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 MagentoDirectoryModelCountry $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.