Skip to content

Commit

Permalink
Merge pull request #3 from Shape-and-Shift/svs-001/enhancements
Browse files Browse the repository at this point in the history
SVS-001 - Enhancements issue #1 and #2
  • Loading branch information
ChristopherDosin authored Mar 15, 2022
2 parents 9104a70 + 32d45ca commit ad4a1c1
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG_de-DE.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG_en-GB.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
</a>

## 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
Expand All @@ -21,9 +20,9 @@

## Requirements

| Version | Requirements |
|--------- |---------------------------- |
| 1.0.0 | Shopware 6.4 >= |
| Version | Requirements |
|------------|---------------------------- |
| 1.1.0 | Shopware 6.4 >= |

## License

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/app/storefront/src/scss/_product-box.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
.custom-select {
width: auto;
}

&-d-none {
display: none!important;
}
}

.sas-product-configurator-group-title {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
{% if product.extensions.groups %}
<div data-wrapper-id="sas-product-configurator-group-wrapper-{{ product.id }}" data-variant-hover-switch="true" data-variant-hover-switch-options="{{ variantHoverSwitchOptions|json_encode }}">
{% 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 %}
<div class="sas-product-configurator-group">
<div class="sas-product-configurator-group {% if hideOnListing %}sas-product-configurator-group-d-none{% endif %}">
{% if group.displayType == 'select' %}
{% sw_include '@Storefront/storefront/component/product/card/group/box-card-group-select.html.twig' %}
{% else %}
Expand All @@ -27,7 +29,6 @@
</div>
{% endif %}
{% endblock %}

{% endblock %}
{% endif %}
{% endblock %}
66 changes: 43 additions & 23 deletions src/Storefront/Page/ProductListingConfigurationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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()];

Expand All @@ -75,6 +75,7 @@ public function loadListing(ProductCollection $products, SalesChannelContext $co

continue;
}

$option->setGroup(null);

$option->setCombinable($combinable);
Expand Down Expand Up @@ -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;
Expand All @@ -146,23 +146,36 @@ 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();
}))
);

$criteria->addAssociation('option.group')
->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;
}
Expand All @@ -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 = [];

Expand Down Expand Up @@ -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');
Expand All @@ -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;
}

Expand Down

0 comments on commit ad4a1c1

Please sign in to comment.