diff --git a/src/Export/Adapters/AttributeAdapter.php b/src/Export/Adapters/AttributeAdapter.php index 6ed8759..c046544 100644 --- a/src/Export/Adapters/AttributeAdapter.php +++ b/src/Export/Adapters/AttributeAdapter.php @@ -316,5 +316,7 @@ protected function translateBooleanValue(bool $value): string $translationKey = $value ? 'finSearch.general.yes' : 'finSearch.general.no'; return $this->translator->trans($translationKey); + + //test } } diff --git a/src/Export/Adapters/ExportItemAdapter.php b/src/Export/Adapters/ExportItemAdapter.php index 8bfc88e..a266f20 100644 --- a/src/Export/Adapters/ExportItemAdapter.php +++ b/src/Export/Adapters/ExportItemAdapter.php @@ -140,8 +140,43 @@ public function adaptVariant(Item $item, ProductEntity $product): ?Item $item->addOrdernumber($orderNumber); } - foreach ($this->adapterFactory->getAttributeAdapter()->adapt($product) as $attribute) { - $item->addMergedAttribute($attribute); + /**Only in case the export is set to "Main/Parent product" + * we merge all the variants specifications"* + */ + if ($item->getId() == $product->parentId) { + foreach ($this->adapterFactory->getAttributeAdapter()->adapt($product) as $attribute) { + $item->addMergedAttribute($attribute); + } + } + else { + // Include $optionAttributes when export is not set to "Main/Parent product" + foreach ($product->configuratorGroupConfig as $attribute) { + if (!$attribute['expressionForListings']) { + $optionAttributes = $product->options->getElements(); + + $matchingOptionAttributes = array_filter( + $optionAttributes, + function ($optionAttribute) use ($attribute) { + return $attribute['id'] == $optionAttribute->groupId; + }); + + $originalAttributes = $this->adapterFactory->getAttributeAdapter()->adapt($product); + + $matchingOriginalAttributes = array_filter( + $originalAttributes, + function ($originalAttribute) use ($matchingOptionAttributes) { + $optionAttributeNames = array_map(function ($optionAttribute) { + return $optionAttribute->name; + }, $matchingOptionAttributes); + + return in_array($originalAttribute->getValues()[0], $optionAttributeNames); + }); + + foreach ($matchingOriginalAttributes as $originalAttribute) { + $item->addMergedAttribute($originalAttribute); + } + } + } } foreach ($this->adapterFactory->getShopwarePropertiesAdapter()->adapt($product) as $property) { diff --git a/tests/Export/Adapters/AttributeAdapterTest.php b/tests/Export/Adapters/AttributeAdapterTest.php index 7d9fc14..b383200 100644 --- a/tests/Export/Adapters/AttributeAdapterTest.php +++ b/tests/Export/Adapters/AttributeAdapterTest.php @@ -345,7 +345,7 @@ public function testEmptyCategoryNameShouldStillExportCategory(): void $attributes = $this->attributeAdapter->adapt($productEntity); - $this->assertCount(7, $attributes); + $this->assertCount(5, $attributes); $this->assertSame('cat_url', $attributes[0]->getKey()); $catUrls = $attributes[0]->getValues(); diff --git a/tests/Export/Adapters/ExportItemAdapterTest.php b/tests/Export/Adapters/ExportItemAdapterTest.php index 2b57b30..4579c28 100644 --- a/tests/Export/Adapters/ExportItemAdapterTest.php +++ b/tests/Export/Adapters/ExportItemAdapterTest.php @@ -55,6 +55,8 @@ public function testExceptionIsThrownForProductWithNoCategories(): void 'categories' => [], ]); + // Test. + $adapter = $this->getExportItemAdapter(); $adapter->adaptProduct(new XMLItem($id), $product); } diff --git a/tests/Export/Adapters/OptionAdapterTest.php b/tests/Export/Adapters/OptionAdapterTest.php new file mode 100644 index 0000000..d8c15d4 --- /dev/null +++ b/tests/Export/Adapters/OptionAdapterTest.php @@ -0,0 +1,39 @@ +pluginConfig = $this->getPluginConfig(); + $this->optionsAdapter = $this->getOptionsAdapter($this->pluginConfig); + } + + public function testGetOptionAttributes(): void + { + $id = Uuid::randomHex(); + $product = $this->createTestProduct([ + 'id' => $id, + ]); + $attributes = $this->optionsAdapter->adapt($product); + + $this->assertCount(2, $attributes); + $this->assertSame('red', $attributes[0]->getValues()[0]); + $this->assertSame('blue', $attributes[1]->getValues()[0]); + } +} diff --git a/tests/Export/Adapters/VariantConfigurationAdapterTest.php b/tests/Export/Adapters/VariantConfigurationAdapterTest.php new file mode 100644 index 0000000..f7a72c9 --- /dev/null +++ b/tests/Export/Adapters/VariantConfigurationAdapterTest.php @@ -0,0 +1,38 @@ +pluginConfig = $this->getPluginConfig(); + $this->variantConfigurationAdapter = $this->getVariantConfigurationAdapter($this->pluginConfig); + } + + public function testGetVariantAttributes(): void + { + $id = Uuid::randomHex(); + $product = $this->createTestProduct([ + 'id' => $id, + ]); + $attributes = $this->variantConfigurationAdapter->adapt($product); + $this->assertCount(2, $attributes); + $this->assertSame('red', $attributes[0]->getValues()[0]); + $this->assertSame('blue', $attributes[1]->getValues()[0]); + } +} \ No newline at end of file diff --git a/tests/Traits/AdapterHelper.php b/tests/Traits/AdapterHelper.php index cdf2fab..d6e3886 100644 --- a/tests/Traits/AdapterHelper.php +++ b/tests/Traits/AdapterHelper.php @@ -15,6 +15,7 @@ use FINDOLOGIC\Shopware6Common\Export\Adapters\ImagesAdapter; use FINDOLOGIC\Shopware6Common\Export\Adapters\KeywordsAdapter; use FINDOLOGIC\Shopware6Common\Export\Adapters\NameAdapter; +use FINDOLOGIC\Shopware6Common\Export\Adapters\OptionsAdapter; use FINDOLOGIC\Shopware6Common\Export\Adapters\OrderNumberAdapter; use FINDOLOGIC\Shopware6Common\Export\Adapters\OverriddenPriceAdapter; use FINDOLOGIC\Shopware6Common\Export\Adapters\PriceAdapter; @@ -23,6 +24,7 @@ use FINDOLOGIC\Shopware6Common\Export\Adapters\SummaryAdapter; use FINDOLOGIC\Shopware6Common\Export\Adapters\UrlAdapter; use FINDOLOGIC\Shopware6Common\Export\Adapters\GroupsAdapter; +use FINDOLOGIC\Shopware6Common\Export\Adapters\VariantConfigurationAdapter; use FINDOLOGIC\Shopware6Common\Export\Config\PluginConfig; use Monolog\Logger; use Psr\EventDispatcher\EventDispatcherInterface; @@ -47,6 +49,7 @@ public function getAdapterFactory(?PluginConfig $config = null): AdapterFactory $this->getImagesAdapter(), $this->getKeywordsAdapter(), $this->getNameAdapter(), + $this->getOptionsAdapter($config), $this->getOrderNumberAdapter(), $this->getOverriddenPriceAdapter(), $this->getPriceAdapter(), @@ -55,6 +58,7 @@ public function getAdapterFactory(?PluginConfig $config = null): AdapterFactory $this->getSummaryAdapter(), $this->getShopwarePropertiesAdapter(), $this->getUrlAdapter(), + $this->getVariantConfigurationAdapter($config), ); } @@ -118,6 +122,11 @@ public function getNameAdapter(): NameAdapter return new NameAdapter(); } + public function getOptionsAdapter(?PluginConfig $config = null): OptionsAdapter + { + return new OptionsAdapter($config ?? $this->getPluginConfig()); + } + public function getOrderNumberAdapter(): OrderNumberAdapter { return new OrderNumberAdapter(); @@ -180,6 +189,11 @@ public function getUrlAdapter(): UrlAdapter return new UrlAdapter($this->getProductUrlService()); } + public function getVariantConfigurationAdapter(?PluginConfig $config = null): VariantConfigurationAdapter + { + return new VariantConfigurationAdapter($config ?? $this->getPluginConfig()); + } + public function getGroupAdapter(?CustomerGroupCollection $customerGroupCollection = null): GroupsAdapter { return new GroupsAdapter($this->getExportContext($customerGroupCollection)); diff --git a/tests/Traits/ProductHelper.php b/tests/Traits/ProductHelper.php index 4dee710..c274442 100644 --- a/tests/Traits/ProductHelper.php +++ b/tests/Traits/ProductHelper.php @@ -52,6 +52,7 @@ public function createTestProduct( 'description' => 'FINDOLOGIC Description', ], 'streamIds' => [], + 'variantListingConfig' => ['displayParent' => null], ]); $productData = array_merge_recursive( diff --git a/tests/Traits/ServicesHelper.php b/tests/Traits/ServicesHelper.php index 2fd664e..f9f47c3 100644 --- a/tests/Traits/ServicesHelper.php +++ b/tests/Traits/ServicesHelper.php @@ -131,6 +131,7 @@ public function getPluginConfig(?array $overrides = []): PluginConfig return PluginConfig::createFromArray(array_merge([ 'shopkey' => 'ABCDABCDABCDABCDABCDABCDABCDABCD', 'active' => true, + 'mainVariant' => 'default', ], $overrides)); }