From 32d45caea4e855bb690d3be59d63dcd04b9e5771 Mon Sep 17 00:00:00 2001 From: vienthuong Date: Sat, 5 Mar 2022 05:44:58 +0700 Subject: [PATCH] SVS-001 - Enhancements issue #1 and #2 --- CHANGELOG_de-DE.md | 4 ++ CHANGELOG_en-GB.md | 4 ++ README.md | 7 +- composer.json | 2 +- .../app/storefront/src/scss/_product-box.scss | 4 ++ .../product/card/box-standard.html.twig | 5 +- .../ProductListingConfigurationLoader.php | 66 ++++++++++++------- 7 files changed, 62 insertions(+), 30 deletions(-) diff --git a/CHANGELOG_de-DE.md b/CHANGELOG_de-DE.md index 2862b1d..e00a8b9 100644 --- a/CHANGELOG_de-DE.md +++ b/CHANGELOG_de-DE.md @@ -1,3 +1,7 @@ +# 1.1.0 - Erweiterung +- [Issue #1](https://github.com/Shape-and-Shift/shopware-variant-switch/issues/1) Zeige auch Varianten, die nicht auf Lager sind +- [Issue #2](https://github.com/Shape-and-Shift/shopware-variant-switch/issues/2) Ausblenden von geteilten Varianten + # 1.0.0 - Erste Veröffentlichung - Variantenschalter auf der Produktliste anzeigen - Variantenschalter beim Hovern über eine Produkteigenschaft auf der Produktliste diff --git a/CHANGELOG_en-GB.md b/CHANGELOG_en-GB.md index 50c212c..02a2ce1 100644 --- a/CHANGELOG_en-GB.md +++ b/CHANGELOG_en-GB.md @@ -1,3 +1,7 @@ +# 1.1.0 - Enhancement +- [Issue #1](https://github.com/Shape-and-Shift/shopware-variant-switch/issues/1) Show also variants which are out of stock +- [Issue #2](https://github.com/Shape-and-Shift/shopware-variant-switch/issues/2) Hide split variants + # 1.0.0 - First release - Show variant switch on product listing card - Variant switch when hovering a variant property on product listing diff --git a/README.md b/README.md index c6c8b79..8b50644 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,6 @@ ## Features - - Show variant switch on product listing card - Variant switch when hovering a variant property on product listing - Show variant switch on off-canvas cart @@ -21,9 +20,9 @@ ## Requirements -| Version | Requirements | -|--------- |---------------------------- | -| 1.0.0 | Shopware 6.4 >= | +| Version | Requirements | +|------------|---------------------------- | +| 1.1.0 | Shopware 6.4 >= | ## License diff --git a/composer.json b/composer.json index 6d9553a..98b2f21 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description":"Show a variant switch within the listing and cart", "type":"shopware-platform-plugin", "license":"MIT", - "version":"1.0.0", + "version":"1.1.0", "keywords":[ "variant", "shopware" diff --git a/src/Resources/app/storefront/src/scss/_product-box.scss b/src/Resources/app/storefront/src/scss/_product-box.scss index 7235fcb..fccc24d 100644 --- a/src/Resources/app/storefront/src/scss/_product-box.scss +++ b/src/Resources/app/storefront/src/scss/_product-box.scss @@ -19,6 +19,10 @@ .custom-select { width: auto; } + + &-d-none { + display: none!important; + } } .sas-product-configurator-group-title { diff --git a/src/Resources/views/storefront/component/product/card/box-standard.html.twig b/src/Resources/views/storefront/component/product/card/box-standard.html.twig index d1caa8c..6c06bcd 100644 --- a/src/Resources/views/storefront/component/product/card/box-standard.html.twig +++ b/src/Resources/views/storefront/component/product/card/box-standard.html.twig @@ -13,9 +13,11 @@ {% if product.extensions.groups %}
{% for group in product.extensions.groups %} + {% set hideOnListing = group.hideOnListing is defined and group.hideOnListing == true %} + {% set groupIdentifier = [product.id, group.id]|join('-') %} {% block page_product_detail_configurator_group %} -
+
{% if group.displayType == 'select' %} {% sw_include '@Storefront/storefront/component/product/card/group/box-card-group-select.html.twig' %} {% else %} @@ -27,7 +29,6 @@
{% endif %} {% endblock %} - {% endblock %} {% endif %} {% endblock %} diff --git a/src/Storefront/Page/ProductListingConfigurationLoader.php b/src/Storefront/Page/ProductListingConfigurationLoader.php index f6c53dc..47fe6f5 100644 --- a/src/Storefront/Page/ProductListingConfigurationLoader.php +++ b/src/Storefront/Page/ProductListingConfigurationLoader.php @@ -3,10 +3,10 @@ namespace SasVariantSwitch\Storefront\Page; use Doctrine\DBAL\Connection; +use Shopware\Core\Content\Product\Aggregate\ProductConfiguratorSetting\ProductConfiguratorSettingCollection; use Shopware\Core\Content\Product\Aggregate\ProductConfiguratorSetting\ProductConfiguratorSettingEntity; use Shopware\Core\Content\Product\ProductCollection; use Shopware\Core\Content\Product\SalesChannel\Detail\AvailableCombinationResult; -use Shopware\Core\Content\Product\SalesChannel\Detail\ProductConfiguratorLoader; use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity; use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionCollection; use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionEntity; @@ -36,11 +36,7 @@ public function __construct( public function loadListing(ProductCollection $products, SalesChannelContext $context): void { - $productSettings = $this->loadSettings($products, $context); - - if (empty($productSettings)) { - return; - } + $settings = $this->fetchSettings($products, $context->getContext()); $productIds = array_filter($products->map(function (SalesChannelProductEntity $product) { return $product->getParentId() ?? $product->getId(); @@ -50,13 +46,17 @@ public function loadListing(ProductCollection $products, SalesChannelContext $co /** @var SalesChannelProductEntity $product */ foreach ($products as $product) { + $productSettings = $this->loadSettings(clone $settings); + if ($product->getConfiguratorSettings() !== null || !$product->getParentId() || empty($productSettings[$product->getParentId()])) { $product->addExtension('groups', new PropertyGroupCollection()); continue; } - $groups = $this->sortSettings($productSettings[$product->getParentId()], $product); + $productSetting = $productSettings[$product->getParentId()]; + + $groups = $this->sortSettings($productSetting, $product); $combinations = $allCombinations[$product->getParentId()]; @@ -75,6 +75,7 @@ public function loadListing(ProductCollection $products, SalesChannelContext $co continue; } + $option->setGroup(null); $option->setCombinable($combinable); @@ -126,18 +127,17 @@ public function loadCombinations(array $productIds, Context $context): array } foreach ($allCombinations as $parentId => $groupedCombinations) { - $available = []; + $result = new AvailableCombinationResult(); foreach ($groupedCombinations as $combination) { - $combination['options'] = json_decode($combination['options'], true); + $available = (bool) $combination['available']; - $available[] = $combination; - } - - $result = new AvailableCombinationResult(); + $options = json_decode($combination['options'], true); + if ($options === false) { + continue; + } - foreach ($available as $combination) { - $result->addCombination($combination['options']); + $result->addCombination($options, $available); } $allCombinations[$parentId] = $result; @@ -146,12 +146,11 @@ public function loadCombinations(array $productIds, Context $context): array return $allCombinations; } - private function loadSettings(ProductCollection $products, SalesChannelContext $context): ?array + private function fetchSettings(ProductCollection $products, Context $context): ProductConfiguratorSettingCollection { - $allSettings = []; $criteria = (new Criteria())->addFilter( new EqualsAnyFilter('productId', $products->map(function (SalesChannelProductEntity $product) { - return $product->getParentId() ?? $product->getId(); + return $product->getParentId() ?? $product->getId(); })) ); @@ -159,10 +158,24 @@ private function loadSettings(ProductCollection $products, SalesChannelContext $ ->addAssociation('option.media') ->addAssociation('media'); + /** + * @var ProductConfiguratorSettingCollection $settings + */ $settings = $this->configuratorRepository - ->search($criteria, $context->getContext()) + ->search($criteria, $context) ->getEntities(); + if ($settings->count() <= 0) { + return new ProductConfiguratorSettingCollection(); + } + + return $settings; + } + + private function loadSettings(ProductConfiguratorSettingCollection $settings): ?array + { + $allSettings = []; + if ($settings->count() <= 0) { return null; } @@ -172,13 +185,12 @@ private function loadSettings(ProductCollection $products, SalesChannelContext $ $productId = $setting->getProductId(); if (\array_key_exists($productId, $allSettings)) { - $allSettings[$productId][] = clone $setting; + $allSettings[$productId][] = ProductConfiguratorSettingEntity::createFrom($setting); } else { - $allSettings[$productId] = [clone $setting]; + $allSettings[$productId] = [ProductConfiguratorSettingEntity::createFrom($setting)]; } } - /** @var ProductConfiguratorSettingEntity $setting */ foreach ($allSettings as $productId => $settings) { $groups = []; @@ -265,6 +277,14 @@ static function (PropertyGroupOptionEntity $a, PropertyGroupOptionEntity $b) use $collection->sortByPositions(); return $collection; + } else if ($product->getMainVariantId() === null) { + foreach ($config as $item) { + if (\array_key_exists('expressionForListings', $item) && $item['expressionForListings'] && $collection->has($item['id'])) { + $collection->get($item['id'])->assign([ + 'hideOnListing' => true, + ]); + } + } } $sortedGroupIds = array_column($config, 'id'); @@ -286,7 +306,7 @@ private function isCombinable( $current[] = $option->getId(); // available with all other current selected options - if ($combinations->hasCombination($current)) { + if ($combinations->hasCombination($current) && $combinations->isAvailable($current)) { return true; }