Skip to content

Commit 2796781

Browse files
pradeepkanakaPradeep KanakaPradeep Kanaka
authored
Fix SQL error when empty categories are assigned to products (#781)
* Fix empty categories assigned issue * Fix PHPUnit test failure --------- Co-authored-by: Pradeep Kanaka <[email protected]> Co-authored-by: Pradeep Kanaka <[email protected]>
1 parent 1836071 commit 2796781

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

app/code/Meta/Conversion/Model/Tracker/InitiateCheckout.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,28 @@ private function getCartCategories(CartInterface $quote): string
112112
}
113113

114114
$items = $quote->getAllVisibleItems();
115+
$categoryIds = [];
115116
foreach ($items as $item) {
116117
$product = $item->getProduct();
117-
$categoryIds = $product->getCategoryIds();
118-
$categories = $this->categoryCollection->create()
119-
->addAttributeToSelect('*')
120-
->addAttributeToFilter('entity_id', $categoryIds);
121-
$categoryNames = [];
122-
foreach ($categories as $category) {
123-
$categoryNames[] = $category->getName();
118+
if ($product->getCategoryIds()) {
119+
$categoryIds[] = $product->getCategoryIds();
124120
}
125121
}
122+
123+
/** Handle products without categories assigned */
124+
if (empty($categoryIds)) {
125+
return '';
126+
}
127+
$categoryIds = array_merge(...$categoryIds);
128+
129+
$categoryNames = [];
130+
$categories = $this->categoryCollection->create()
131+
->addAttributeToSelect('*')
132+
->addAttributeToFilter('entity_id', ['in' => $categoryIds]);
133+
foreach ($categories as $category) {
134+
$categoryNames[] = $category->getName();
135+
}
136+
126137
return implode(',', $categoryNames); /** @phpstan-ignore-line */
127138
}
128139

0 commit comments

Comments
 (0)