Closed
Description
Preconditions (*)
- Any version of Magento, any version of PHP.
Steps to reproduce (*)
- Purchase an item that isn't in any category.
Expected result (*)
- Checkout/conversion should work correctly
Actual result (*)
- 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
Labels
No labels