Skip to content

Potential errors and performance issues in Tracker/InitiateCheckout #72

Closed
@snoop0x7b

Description

@snoop0x7b

Preconditions (*)

  1. Any version of Magento, any version of PHP.

Steps to reproduce (*)

  1. Purchase an item that isn't in any category.

Expected result (*)

  1. Checkout/conversion should work correctly

Actual result (*)

  1. SQL error.

Basically what's going on is

` private function getCartCategories(CartInterface $quote): string
{
if (!$quote) {
return '';
}

    $items = $quote->getAllVisibleItems();
    foreach ($items as $item) {
        $product = $item->getProduct();
        $categoryIds = $product->getCategoryIds();
        $categories = $this->categoryCollection->create()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('entity_id', $categoryIds);
        $categoryNames = [];
        foreach ($categories as $category) {
            $categoryNames[] = $category->getName();
        }
    }
    return implode(',', $categoryNames); /** @phpstan-ignore-line */
}`

In this code -

It's assumed that the quote item is in a category. This should be checked prior to doing this query.

Second, this is a query in a loop. You could easily aggregate the category IDs and do the query after.

Third, you're selecting ALL attributes even though you only use name. It's a best practice in Magento to only fetch the attributes you actually need.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions