Skip to content

Commit d1c9c0d

Browse files
committed
Support master ID
1 parent 1ff9a66 commit d1c9c0d

8 files changed

Lines changed: 37 additions & 23 deletions

File tree

src/Export/Data/Entity/ProductEntity.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,12 @@ public function toArray(): array
9696
{
9797
$cachedProductFieldNames = array_map(fn (FieldInterface $field) => $field->getName(), iterator_to_array($this->cachedProductFields));
9898
$fields = array_filter($this->productFields, fn (FieldInterface $productField) => !in_array($productField->getName(), $cachedProductFieldNames));
99-
$isVariant = $this->product->getId() !== $this->product->getParentId() && isset($this->parent);
100-
$defaultFields = [
99+
$isVariant = $this->product->getParentId() !== null;
100+
$resolvedParent = $this->parent ?? $this->product->getParent();
101+
102+
$defaultFields = [
101103
'ProductNumber' => $this->product->getProductNumber(),
102-
'Master' => $isVariant ? $this->parent->getProductNumber() : $this->product->getProductNumber(),
104+
'Master' => ($isVariant && $resolvedParent) ? $resolvedParent->getProductNumber() : $this->product->getProductNumber(),
103105
'Name' => (string) $this->product->getTranslation('name'),
104106
'FilterAttributes' => $this->getFilterAttributes(),
105107
'CustomFields' => $this->getCustomFields(),
@@ -109,7 +111,7 @@ public function toArray(): array
109111
$fields,
110112
fn (array $fields, FieldInterface $field): array => array_merge(
111113
$fields,
112-
[$field->getName() => ($this->getAdditionalCache($field->getName()) ?? $field->getValue($isVariant ? $this->parent : $this->product))]
114+
[$field->getName() => ($this->getAdditionalCache($field->getName()) ?? $field->getValue($isVariant && $resolvedParent ? $resolvedParent : $this->product))]
113115
),
114116
$defaultFields
115117
);

src/Export/Data/Factory/ProductEntityFactory.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323
PropertyFormatter $propertyFormatter,
2424
FieldsProvider $fieldsProviders,
2525
CurrencyFieldsProvider $currencyFieldsProvider,
26-
\Traversable $variantFields
26+
\Traversable $variantFields,
2727
) {
2828
$this->propertyFormatter = $propertyFormatter;
2929
$this->fieldsProvider = $fieldsProviders;
@@ -44,9 +44,6 @@ public function createEntities(Entity $entity, string $producedType = ProductEnt
4444
$fields = array_merge($this->fieldsProvider->getFields($producedType), $this->currencyFieldsProvider->getCurrencyFields());
4545

4646
if ($entity->getParentId() !== null) {
47-
// To jest WARIANT.
48-
// Tworzymy bazową encję ProductEntity (odpowiednik rodzica), ponieważ wariant w
49-
// Shopware posiada w sobie dziedziczone dane od rodzica (nazwa, opis, producent).
5047
$pseudoParent = new $producedType($entity, new \ArrayIterator($fields), new \ArrayIterator());
5148

5249
yield new VariantEntity(
@@ -56,7 +53,6 @@ public function createEntities(Entity $entity, string $producedType = ProductEnt
5653
iterator_to_array($this->variantFields)
5754
);
5855
} else {
59-
// To jest PRODUKT GŁÓWNY (Rodzic lub samodzielny produkt).
6056
yield new $producedType($entity, new \ArrayIterator($fields), new \ArrayIterator());
6157
}
6258
}

src/Export/ExportProducts.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Omikron\FactFinder\Shopware6\Export\Data\Entity\ProductEntity as ExportProductEntity;
88
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
9-
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
109
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
1110
use 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
}

src/Export/Field/CategoryPath.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ private function getCategories(Entity $entity): CategoryCollection
6161
return new CategoryCollection([$entity]);
6262
}
6363

64-
return $entity->getCategories();
64+
return $entity->getCategories() ?? new CategoryCollection();
6565
}
6666
}

src/Export/Field/FilterAttributes.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,24 @@ public function getName(): string
3232
*/
3333
public function getValue(Entity $entity): string
3434
{
35-
// 1. Bazowe właściwości (dziedziczone lub bezpośrednie)
3635
$properties = $this->applyPropertyGroupsFilter($entity);
3736
$attributes = $properties ? array_map($this->propertyFormatter, $properties) : [];
3837

39-
// 2. Pobieranie opcji bez ładowania całych encji dzieci
4038
if ($entity->getParentId() !== null) {
41-
// Jesteśmy w wariancie - pobieramy jego konkretne opcje
42-
$options = $entity->getOptions() ? $entity->getOptions()->getElements() : [];
39+
$options = $entity->getOptions() ? $entity->getOptions()->getElements() : [];
4340
$attributes = array_merge($attributes, array_map($this->propertyFormatter, $options));
4441
} else {
45-
// Jesteśmy w produkcie głównym - pobieramy agregację opcji ze wszystkich wariantów
4642
$configuratorSettings = $entity->getConfiguratorSettings();
43+
4744
if ($configuratorSettings) {
4845
$options = [];
46+
4947
foreach ($configuratorSettings as $setting) {
5048
if ($setting->getOption()) {
5149
$options[] = $setting->getOption();
5250
}
5351
}
52+
5453
$attributes = array_merge($attributes, array_map($this->propertyFormatter, $options));
5554
}
5655
}

src/Export/Stream/ConsoleOutput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function addEntity(array $entity): void
2323
{
2424
$this->fileResource = $this->fileResource ?? new File('php://output', 'w');
2525
ob_start();
26-
$this->fileResource->fputcsv($entity, $this->delimiter);
26+
$this->fileResource->fputcsv($entity, $this->delimiter, '"', '\\');
2727
$this->output->writeln(rtrim(ob_get_clean()));
2828
}
2929
}

src/Export/Stream/CsvFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public function __construct($fileResource, string $delimiter = ';')
2323

2424
public function addEntity(array $entity): void
2525
{
26-
fputcsv($this->fileResource, $entity, $this->delimiter);
26+
fputcsv($this->fileResource, $entity, $this->delimiter, '"', '\\');
2727
}
2828
}

src/Resources/config/services.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<parameter key="productNumber">ProductNumber</parameter>
4646
</parameter>
4747
<parameter key="factfinder.export.associations" type="collection">
48-
<parameter key="variant_cover">children.media</parameter>
48+
<parameter key="variant_cover">cover.media</parameter>
4949
</parameter>
5050
<parameter key="factfinder.category_page.add_params" type="collection">
5151
</parameter>

0 commit comments

Comments
 (0)