Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SW-801 Export variants with different attribute values v1 #15

Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d47f590
Added the fix for variants export regarding properties split
bojand-soprex Aug 22, 2023
ade23a8
Test adaptations
bojand-soprex Aug 23, 2023
eec3e5a
Lint fix
bojand-soprex Aug 23, 2023
3f8c9a6
Lint fix for multiline comma
bojand-soprex Aug 23, 2023
2ddb821
Refactor logic for Variant config and tests
bojand-soprex Oct 31, 2023
d93f2ec
Fix lint errors
bojand-soprex Nov 1, 2023
ec95f18
fix lint failed tests
bojand-soprex Nov 1, 2023
a910c0a
Fix tests PHp7.4
bojand-soprex Nov 1, 2023
83bdc28
Resolved PR comments
bojand-soprex Nov 1, 2023
a486585
VariantConfigurations logic refactor
bojand-soprex Nov 8, 2023
8ad3c4f
refactor optionsAdapter
bojand-soprex Nov 8, 2023
48921d8
Refactor variant logic, add AdapterHelper
bojand-soprex Nov 14, 2023
df10b68
Fix lint
bojand-soprex Nov 14, 2023
b23e67a
Refactor and test adaptations
bojand-soprex Nov 17, 2023
7d5d42b
Removed unceccesary check for isVariant
bojand-soprex Dec 15, 2023
d327d42
Refactor logic and adapt tests
bojand-soprex Dec 20, 2023
32c0bc7
Remove as parameter PR comment resolve
bojand-soprex Jan 4, 2024
9290ae8
Resolve PR comments and adaptati the logic and tests
bojand-soprex Feb 8, 2024
effc44d
Remove order number check from adaptProduct method
bojand-soprex Feb 8, 2024
aca53d7
Adapt tests and resolve PR comments
bojand-soprex Feb 20, 2024
0cb977e
Removed ordernumber hotfix
bojand-soprex Feb 26, 2024
e2080f0
Lint fix
bojand-soprex Feb 26, 2024
5748ebf
Remove pluginConfig setup from ExportItemAdapter
bojand-soprex Feb 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Export/Adapters/AdapterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class AdapterFactory

private UserGroupsAdapter $userGroupsAdapter;

private VariantConfigurationAdapter $variantConfigurationAdapter;

private OptionsAdapter $optionsAdapter;

bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
public function __construct(
AttributeAdapter $attributeAdapter,
BonusAdapter $bonusAdapter,
Expand All @@ -54,7 +58,8 @@ public function __construct(
SummaryAdapter $summaryAdapter,
ShopwarePropertiesAdapter $shopwarePropertiesAdapter,
UrlAdapter $urlAdapter,
UserGroupsAdapter $userGroupsAdapter
UserGroupsAdapter $userGroupsAdapter,
VariantConfigurationAdapter $variantConfigurationAdapter
) {
$this->attributeAdapter = $attributeAdapter;
$this->bonusAdapter = $bonusAdapter;
Expand All @@ -72,6 +77,7 @@ public function __construct(
$this->shopwarePropertiesAdapter = $shopwarePropertiesAdapter;
$this->urlAdapter = $urlAdapter;
$this->userGroupsAdapter = $userGroupsAdapter;
$this->variantConfigurationAdapter = $variantConfigurationAdapter;
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
}

public function getAttributeAdapter(): AttributeAdapter
Expand Down Expand Up @@ -153,4 +159,14 @@ public function getUserGroupsAdapter(): UserGroupsAdapter
{
return $this->userGroupsAdapter;
}

public function getVariantConfigurationAdapter(): VariantConfigurationAdapter
{
return $this->variantConfigurationAdapter;
}

public function getOptionsAdapter(): OptionsAdapter
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->optionsAdapter;
}
}
14 changes: 0 additions & 14 deletions src/Export/Adapters/AttributeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,13 @@ public function adapt(ProductEntity $product): array
$categoryAttributes = $this->getCategoryAndCatUrlAttributes($product);
$manufacturerAttributes = $this->getManufacturerAttributes($product);
$propertyAttributes = $this->getPropertyAttributes($product);
$optionAttributes = $this->getOptionAttributes($product);
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
$customFieldAttributes = $this->getCustomFieldAttributes($product);
$additionalAttributes = $this->getAdditionalAttributes($product);

return array_merge(
$categoryAttributes,
$manufacturerAttributes,
$propertyAttributes,
$optionAttributes,
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
$customFieldAttributes,
$additionalAttributes,
);
Expand Down Expand Up @@ -160,18 +158,6 @@ protected function getPropertyAttributes(ProductEntity $product): array
return $this->getPropertyGroupOptionAttributes($product->properties);
}

/**
* @return Attribute[]
*/
protected function getOptionAttributes(ProductEntity $product): array
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
{
if (!$product->options || !$product->options->count()) {
return [];
}

return $this->getPropertyGroupOptionAttributes($product->options);
}

bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
/**
* @return Attribute[]
*/
Expand Down
22 changes: 19 additions & 3 deletions src/Export/Adapters/ExportItemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace FINDOLOGIC\Shopware6Common\Export\Adapters;

use FINDOLOGIC\Export\Data\Item;
use FINDOLOGIC\Shopware6Common\Export\Config\PluginConfig;
use FINDOLOGIC\Shopware6Common\Export\Events\AfterItemAdaptEvent;
use FINDOLOGIC\Shopware6Common\Export\Events\AfterVariantAdaptEvent;
use FINDOLOGIC\Shopware6Common\Export\Events\BeforeItemAdaptEvent;
Expand All @@ -24,15 +25,19 @@ class ExportItemAdapter

private LoggerInterface $logger;

protected PluginConfig $pluginConfig;
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved

private ?EventDispatcherInterface $eventDispatcher;

public function __construct(
AdapterFactory $adapterFactory,
LoggerInterface $logger,
PluginConfig $pluginConfig,
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
?EventDispatcherInterface $eventDispatcher = null
) {
$this->adapterFactory = $adapterFactory;
$this->logger = $logger;
$this->pluginConfig = $pluginConfig;
$this->eventDispatcher = $eventDispatcher;
}

Expand Down Expand Up @@ -142,16 +147,27 @@ public function adaptProduct(Item $item, ProductEntity $product): ?Item
public function adaptVariant(Item $item, ProductEntity $product): ?Item
{
if ($this->eventDispatcher) {
$this->eventDispatcher->dispatch(new BeforeVariantAdaptEvent($product, $item), BeforeVariantAdaptEvent::NAME);
$this->eventDispatcher->dispatch(
new BeforeVariantAdaptEvent($product, $item),
BeforeVariantAdaptEvent::NAME,
);
}

try {
foreach ($this->adapterFactory->getOrderNumbersAdapter()->adapt($product) as $orderNumber) {
$item->addOrdernumber($orderNumber);
}

foreach ($this->adapterFactory->getAttributeAdapter()->adapt($product) as $attribute) {
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
$item->addMergedAttribute($attribute);
if ($this->pluginConfig->getMainVariant() != 'default') {
foreach ($this->adapterFactory->getOptionsAdapter()->getOptionAttributes($product) as $attribute) {
$item->addMergedAttribute($attribute);
}
} else {
$attributes = $this->adapterFactory->getVariantConfigurationAdapter()
->getOptionAttributes($product);
foreach ($attributes as $attribute) {
$item->addMergedAttribute($attribute);
}
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
}

foreach ($this->adapterFactory->getShopwarePropertiesAdapter()->adapt($product) as $property) {
Expand Down
36 changes: 36 additions & 0 deletions src/Export/Adapters/OptionsAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace FINDOLOGIC\Shopware6Common\Export\Adapters;

use FINDOLOGIC\Export\Data\Attribute;
use FINDOLOGIC\Shopware6Common\Export\Config\PluginConfig;
use FINDOLOGIC\Shopware6Common\Export\Utils\Utils;
use Vin\ShopwareSdk\Data\Entity\Product\ProductEntity;
use Vin\ShopwareSdk\Data\Entity\PropertyGroupOption\PropertyGroupOptionCollection;
use Vin\ShopwareSdk\Data\Entity\PropertyGroupOption\PropertyGroupOptionEntity;

class OptionsAdapter
{

bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
protected PluginConfig $pluginConfig;

public function __construct(
PluginConfig $pluginConfig,
) {
$this->pluginConfig = $pluginConfig;
}

/**
* @return Attribute[]
*/
public function getOptionAttributes(ProductEntity $product): array
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
{
if (!$product->options || !$product->options->count()) {
return [];
}

return Utils::getPropertyGroupOptionAttributes($product->options, $this->pluginConfig);
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
}
}
59 changes: 59 additions & 0 deletions src/Export/Adapters/VariantConfigurationAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace FINDOLOGIC\Shopware6Common\Export\Adapters;

use FINDOLOGIC\Export\Data\Attribute;
use FINDOLOGIC\Shopware6Common\Export\Config\MainVariant;
use FINDOLOGIC\Shopware6Common\Export\Config\PluginConfig;
use FINDOLOGIC\Shopware6Common\Export\Utils\Utils;
use Vin\ShopwareSdk\Data\Entity\Product\ProductEntity;
use Vin\ShopwareSdk\Data\Entity\PropertyGroupOption\PropertyGroupOptionEntity;

class VariantConfigurationAdapter
{
protected PluginConfig $pluginConfig;

bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
public function __construct(
PluginConfig $pluginConfig
) {
$this->pluginConfig = $pluginConfig;
}

/**
* @return Attribute[]
*/
public function getOptionAttributes(ProductEntity $product): array
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
{
$options = $product->options;

$isVariant = !is_null($product->parentId);
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved

if (!$options->count()) {
return [];
}

if ($isVariant) {
$variantlisting = array_filter(
$product->variantListingConfig['configuratorGroupConfig'] ?? [],
function (array $listing) {
return $listing['expressionForListings'];
}
);
$variantListingGroupId = array_map(fn($listing) => $listing['id'], $variantlisting);

if (
$this->pluginConfig->getMainVariant() === MainVariant::SHOPWARE_DEFAULT &&
count($variantlisting) &&
!$product->variantListingConfig['displayParent']
) {
$options = $options->filter(function (PropertyGroupOptionEntity $option) use ($variantListingGroupId) {
return !in_array($option->groupId, $variantListingGroupId);
});
}
}

return Utils::getPropertyGroupOptionAttributes($options, $this->pluginConfig);
}
}
65 changes: 65 additions & 0 deletions src/Export/Utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace FINDOLOGIC\Shopware6Common\Export\Utils;

use FINDOLOGIC\Export\Data\Attribute;
use FINDOLOGIC\Shopware6Common\Export\Config\PluginConfig;
use Vin\ShopwareSdk\Data\Entity\PropertyGroupOption\PropertyGroupOptionCollection;
use Vin\ShopwareSdk\Data\Entity\PropertyGroupOption\PropertyGroupOptionEntity;
use Vin\ShopwareSdk\Data\Entity\SalesChannelDomain\SalesChannelDomainCollection;
use Vin\ShopwareSdk\Data\Entity\SalesChannelDomain\SalesChannelDomainEntity;

Expand Down Expand Up @@ -162,4 +166,65 @@ public static function getCurrencyPrice(array $prices, string $currencyId): ?arr

return count($filteredProductPrice) ? $filteredProductPrice[array_key_first($filteredProductPrice)] : null;
}

/**
* @return Attribute[]
*/
public static function getPropertyGroupOptionAttributes(
PropertyGroupOptionCollection $collection,
PluginConfig $pluginConfig
): array {
$attributes = [];

foreach ($collection as $propertyGroupOptionEntity) {
$group = $propertyGroupOptionEntity->group;
if ($group && !$group->filterable) {
continue;
}

$attributes = array_merge($attributes, static::getAttributePropertyAsAttribute(
$propertyGroupOptionEntity,
$pluginConfig
));
}

return $attributes;
}

/**
* @return Attribute[]
*/
protected static function getAttributePropertyAsAttribute(
PropertyGroupOptionEntity $propertyGroupOptionEntity,
PluginConfig $pluginConfig
): array {
$attributes = [];

$group = $propertyGroupOptionEntity->group;
if ($group && $propertyGroupOptionEntity->getTranslation('name') && $group->getTranslation('name')) {
$groupName = static::getAttributeKey($group->getTranslation('name'), $pluginConfig);
$propertyGroupOptionName = $propertyGroupOptionEntity->getTranslation('name');
if (!Utils::isEmpty($groupName) && !Utils::isEmpty($propertyGroupOptionName)) {
$propertyGroupAttrib = new Attribute($groupName);
$propertyGroupAttrib->addValue(Utils::removeControlCharacters($propertyGroupOptionName));

$attributes[] = $propertyGroupAttrib;
}
}

return $attributes;
}

/**
* For API Integrations, we have to remove special characters from the attribute key as a requirement for
* sending data via API.
*/
protected static function getAttributeKey(?string $key, PluginConfig $pluginConfig): ?string
{
if ($pluginConfig->isIntegrationTypeApi()) {
return Utils::removeSpecialChars($key);
}

return $key;
}
}
4 changes: 1 addition & 3 deletions tests/Export/Adapters/AttributeAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public function testEmptyCategoryNameShouldStillExportCategory(): void

$attributes = $this->attributeAdapter->adapt($productEntity);

$this->assertCount(7, $attributes);
$this->assertCount(5, $attributes);
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
$this->assertSame('cat_url', $attributes[0]->getKey());

$catUrls = $attributes[0]->getValues();
Expand Down Expand Up @@ -409,7 +409,6 @@ public function testOnlyUniqueCategoriesAreExported(bool $isParentAssigned, bool
$exportItemAdapter = $this->getExportItemAdapter(null, $config);

$item = $exportItemAdapter->adapt($initialItem, $productEntity);

bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
if ($item === null) {
$item = $initialItem;
}
Expand All @@ -419,7 +418,6 @@ public function testOnlyUniqueCategoriesAreExported(bool $isParentAssigned, bool
$attributes = $reflector->getProperty('attributes');
$attributes->setAccessible(true);
$value = $attributes->getValue($item);

$this->assertArrayHasKey('cat_url', $value);
$categoryUrlAttributeValues = $value['cat_url']->getValues();
$this->assertSame($expectedCatUrls, $categoryUrlAttributeValues);
Expand Down
16 changes: 15 additions & 1 deletion tests/Export/Adapters/ExportItemAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use FINDOLOGIC\Export\XML\XMLItem;
use FINDOLOGIC\Shopware6Common\Export\Adapters\AdapterFactory;
use FINDOLOGIC\Shopware6Common\Export\Adapters\AttributeAdapter;
use FINDOLOGIC\Shopware6Common\Export\Config\PluginConfig;
use FINDOLOGIC\Shopware6Common\Export\Exceptions\Product\ProductHasNoCategoriesException;
use FINDOLOGIC\Shopware6Common\Tests\Traits\AdapterHelper;
use FINDOLOGIC\Shopware6Common\Tests\Traits\ProductHelper;
Expand All @@ -26,6 +27,13 @@ class ExportItemAdapterTest extends TestCase
use ProductHelper;
use ServicesHelper;

public PluginConfig $pluginConfig;
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved

public function setUp(): void
{
$this->pluginConfig = $this->getPluginConfig();
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
}
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved

public function testEventsAreDispatched(): void
{
$xmlItem = new XMLItem(Uuid::randomHex());
Expand All @@ -39,6 +47,12 @@ public function testEventsAreDispatched(): void
/** @var EventDispatcher $eventDispatcherMock */
$adapter = $this->getExportItemAdapter(null, null, null, $eventDispatcherMock);
$adapter->adapt($xmlItem, $product);
$id = Uuid::randomHex();
bojand-soprex marked this conversation as resolved.
Show resolved Hide resolved
$product = $this->createTestProduct([
'id' => $id,
'categories' => [],
]);
$xmlItem = new XMLItem($id);
$adapter->adaptVariant($xmlItem, $product);
}

Expand All @@ -64,7 +78,7 @@ public function testExceptionIsNotThrownForVariantWithNoCategories(): void
'categories' => [],
]);

$adapter = $this->getExportItemAdapter();
$adapter = $this->getExportItemAdapter(null, $this->pluginConfig, null, null);
$item = $adapter->adaptVariant(new XMLItem($id), $product);

$this->assertEquals($id, $item->getId());
Expand Down
Loading
Loading