66
77use Omikron \FactFinder \Shopware6 \Export \Data \Entity \ProductEntity as ExportProductEntity ;
88use Shopware \Core \Framework \DataAbstractionLayer \Search \Criteria ;
9- use Shopware \Core \Framework \DataAbstractionLayer \Search \Filter \EqualsFilter ;
109use Shopware \Core \System \SalesChannel \Entity \SalesChannelRepository ;
1110use Shopware \Core \System \SalesChannel \SalesChannelContext ;
1211
@@ -35,7 +34,27 @@ public function getByContext(SalesChannelContext $context, int $batchSize = 100)
3534 break ;
3635 }
3736
37+ $ parentIds = [];
38+ // 1. Zbieramy unikalne ID rodziców z bieżącej paczki
3839 foreach ($ products ->getElements () as $ product ) {
40+ if ($ product ->getParentId () !== null ) {
41+ $ parentIds [$ product ->getParentId ()] = true ;
42+ }
43+ }
44+
45+ $ parents = [];
46+ if (!empty ($ parentIds )) {
47+ $ parentCriteria = new Criteria (array_keys ($ parentIds ));
48+ $ parentCriteria ->addAssociation ('categories ' );
49+ $ parentCriteria ->addAssociation ('categoriesRo ' );
50+ $ parents = $ this ->productRepository ->search ($ parentCriteria , $ context )->getElements ();
51+ }
52+
53+ foreach ($ products ->getElements () as $ product ) {
54+ if ($ product ->getParentId () !== null && isset ($ parents [$ product ->getParentId ()])) {
55+ $ product ->setParent ($ parents [$ product ->getParentId ()]);
56+ }
57+
3958 yield $ product ;
4059 }
4160
@@ -71,6 +90,8 @@ private function getCriteria(int $batchSize, int $offset): Criteria
7190 $ criteria = new Criteria ();
7291 $ criteria ->setLimit ($ batchSize );
7392 $ criteria ->setOffset ($ offset );
93+ $ criteria ->setTotalCountMode (Criteria::TOTAL_COUNT_MODE_NONE );
94+
7495 $ criteria ->addAssociation ('categories ' );
7596 $ criteria ->addAssociation ('categoriesRo ' );
7697 $ criteria ->addAssociation ('manufacturer ' );
@@ -87,10 +108,6 @@ private function getCriteria(int $batchSize, int $offset): Criteria
87108 $ criteria ->addAssociation ($ association );
88109 }
89110
90- // UWAGA: Usunęliśmy addFilter(new EqualsFilter('parentId', null));
91- // Chcemy eksportować płasko wszystko: zarówno rodziców jak i warianty,
92- // więc nie filtrujemy tutaj po parentId!
93-
94111 return $ criteria ;
95112 }
96113}
0 commit comments