From 0bd042797d58616a56f764940663c19d0689fcf4 Mon Sep 17 00:00:00 2001 From: Bojan Date: Tue, 22 Aug 2023 11:26:30 +0200 Subject: [PATCH 1/6] Added the fix for variants export regarding properties split --- src/Export/Adapters/ExportItemAdapter.php | 37 ++++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/Export/Adapters/ExportItemAdapter.php b/src/Export/Adapters/ExportItemAdapter.php index 4070818..a07e433 100644 --- a/src/Export/Adapters/ExportItemAdapter.php +++ b/src/Export/Adapters/ExportItemAdapter.php @@ -139,11 +139,40 @@ public function adaptVariant(Item $item, ProductEntity $product): ?Item foreach ($this->adapterFactory->getOrderNumbersAdapter()->adapt($product) as $orderNumber) { $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) { $item->addProperty($property); } From e7316cc3a58586ffa04ecb674e888a09eca53447 Mon Sep 17 00:00:00 2001 From: Bojan Date: Tue, 22 Aug 2023 11:34:08 +0200 Subject: [PATCH 2/6] Code tidy up --- src/Export/Adapters/ExportItemAdapter.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Export/Adapters/ExportItemAdapter.php b/src/Export/Adapters/ExportItemAdapter.php index a07e433..fd3c5e3 100644 --- a/src/Export/Adapters/ExportItemAdapter.php +++ b/src/Export/Adapters/ExportItemAdapter.php @@ -153,13 +153,17 @@ public function adaptVariant(Item $item, ProductEntity $product): ?Item if (!$attribute['expressionForListings']) { $optionAttributes = $product->options->getElements(); - $matchingOptionAttributes = array_filter($optionAttributes, function ($optionAttribute) use ($attribute) { + $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) { + $matchingOriginalAttributes = array_filter( + $originalAttributes, + function ($originalAttribute) use ($matchingOptionAttributes) { $optionAttributeNames = array_map(function ($optionAttribute) { return $optionAttribute->name; }, $matchingOptionAttributes); From a135cec5ab516bd079a1a52b4096cc8aa45fb238 Mon Sep 17 00:00:00 2001 From: Bojan Date: Tue, 22 Aug 2023 11:38:19 +0200 Subject: [PATCH 3/6] Restored empty rows --- src/Export/Adapters/ExportItemAdapter.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Export/Adapters/ExportItemAdapter.php b/src/Export/Adapters/ExportItemAdapter.php index fd3c5e3..038bcd3 100644 --- a/src/Export/Adapters/ExportItemAdapter.php +++ b/src/Export/Adapters/ExportItemAdapter.php @@ -139,9 +139,10 @@ public function adaptVariant(Item $item, ProductEntity $product): ?Item foreach ($this->adapterFactory->getOrderNumbersAdapter()->adapt($product) as $orderNumber) { $item->addOrdernumber($orderNumber); } - /**Only in case the export is set to "Main/Parent product" - * we merge all the variants specifications"* - */ + + /**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); @@ -177,6 +178,7 @@ function ($originalAttribute) use ($matchingOptionAttributes) { } } } + foreach ($this->adapterFactory->getShopwarePropertiesAdapter()->adapt($product) as $property) { $item->addProperty($property); } From d9f0e1b7726159dd27f8b9c1a9ff1940e801a7f1 Mon Sep 17 00:00:00 2001 From: Bojan Date: Fri, 22 Mar 2024 15:55:50 +0100 Subject: [PATCH 4/6] Adapt tests --- .../Export/Adapters/AttributeAdapterTest.php | 2 +- tests/Export/Adapters/OptionAdapterTest.php | 39 +++++++++++++++++++ .../VariantConfigurationAdapterTest.php | 38 ++++++++++++++++++ tests/Traits/AdapterHelper.php | 14 +++++++ tests/Traits/ProductHelper.php | 1 + tests/Traits/ServicesHelper.php | 1 + 6 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tests/Export/Adapters/OptionAdapterTest.php create mode 100644 tests/Export/Adapters/VariantConfigurationAdapterTest.php 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/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)); } From 1025c4b3988e9990f6ba3025508e8279afe7e0c8 Mon Sep 17 00:00:00 2001 From: Radomir Butacevic Date: Tue, 16 Jul 2024 23:13:10 +0200 Subject: [PATCH 5/6] Test --- tests/Export/Adapters/ExportItemAdapterTest.php | 2 ++ 1 file changed, 2 insertions(+) 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); } From c61d669657e3d16a11c1c6a4a95d612a85cac087 Mon Sep 17 00:00:00 2001 From: Bojan Date: Thu, 26 Sep 2024 15:49:03 +0200 Subject: [PATCH 6/6] Test --- src/Export/Adapters/AttributeAdapter.php | 2 ++ 1 file changed, 2 insertions(+) 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 } }