From df920f5c51624ef208bd20583e2014d17afe6cf9 Mon Sep 17 00:00:00 2001 From: Radomir Butacevic Date: Tue, 9 Apr 2024 15:28:09 +0200 Subject: [PATCH 1/2] Add a check for the product visibility --- src/Export/Search/AbstractProductSearcher.php | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Export/Search/AbstractProductSearcher.php b/src/Export/Search/AbstractProductSearcher.php index f32abc0..dd5c17d 100644 --- a/src/Export/Search/AbstractProductSearcher.php +++ b/src/Export/Search/AbstractProductSearcher.php @@ -8,6 +8,7 @@ use FINDOLOGIC\Shopware6Common\Export\Config\PluginConfig; use FINDOLOGIC\Shopware6Common\Export\Utils\Utils; use InvalidArgumentException; +use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition; use Vin\ShopwareSdk\Data\Entity\Product\ProductCollection; use Vin\ShopwareSdk\Data\Entity\Product\ProductEntity; @@ -107,7 +108,9 @@ protected function getCheapestProducts(ProductCollection $products): ProductColl /** @var string[] $productPrice */ $cheapestVariantPrice = Utils::getCurrencyPrice($cheapestVariant->price, $currencyId); - if ($productPrice['gross'] === 0.0) { + if ($productPrice['gross'] === 0.0 + || !$this->isProductVisible($product) + ) { $realCheapestProduct = $cheapestVariant; } else { $realCheapestProduct = $productPrice['gross'] <= $cheapestVariantPrice['gross'] @@ -155,4 +158,23 @@ abstract protected function getCheapestChild(string $productId): ?ProductEntity; abstract protected function getFirstVisibleChildId(string $productId): ?string; abstract protected function getRealMainVariants(array $productIds): ProductCollection; + + protected function isProductVisible(ProductEntity $product): bool + { + $salesChannelId = $this->exportContext->getSalesChannelId(); + + $isVisible = false; + if ($product->active) { + foreach ($product->visibilities->getElements() as $productVisibility) { + if ($productVisibility->salesChannelId === $salesChannelId + && $productVisibility->visibility >= ProductVisibilityDefinition::VISIBILITY_ALL + ) { + $isVisible = true; + break; + } + } + } + + return $isVisible; + } } From 1b9e9f2724bfcc69ed3a2723c28d2885cf377007 Mon Sep 17 00:00:00 2001 From: Radomir Butacevic Date: Wed, 10 Apr 2024 15:55:14 +0200 Subject: [PATCH 2/2] Correct the changes --- src/Export/Constants.php | 1 + src/Export/Search/AbstractProductSearcher.php | 15 ++++++--------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Export/Constants.php b/src/Export/Constants.php index cc2ae14..b5654b3 100644 --- a/src/Export/Constants.php +++ b/src/Export/Constants.php @@ -14,6 +14,7 @@ class Constants 'manufacturer', 'manufacturer.translations', 'cover', + 'visibilities', ]; public const VARIANT_ASSOCIATIONS = [ diff --git a/src/Export/Search/AbstractProductSearcher.php b/src/Export/Search/AbstractProductSearcher.php index dd5c17d..a665421 100644 --- a/src/Export/Search/AbstractProductSearcher.php +++ b/src/Export/Search/AbstractProductSearcher.php @@ -8,12 +8,13 @@ use FINDOLOGIC\Shopware6Common\Export\Config\PluginConfig; use FINDOLOGIC\Shopware6Common\Export\Utils\Utils; use InvalidArgumentException; -use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition; use Vin\ShopwareSdk\Data\Entity\Product\ProductCollection; use Vin\ShopwareSdk\Data\Entity\Product\ProductEntity; abstract class AbstractProductSearcher { + protected const VISIBILITY_ALL = 30; + protected PluginConfig $pluginConfig; protected AbstractProductCriteriaBuilder $productCriteriaBuilder; @@ -108,9 +109,7 @@ protected function getCheapestProducts(ProductCollection $products): ProductColl /** @var string[] $productPrice */ $cheapestVariantPrice = Utils::getCurrencyPrice($cheapestVariant->price, $currencyId); - if ($productPrice['gross'] === 0.0 - || !$this->isProductVisible($product) - ) { + if ($productPrice['gross'] === 0.0 || !$this->isProductVisible($product)) { $realCheapestProduct = $cheapestVariant; } else { $realCheapestProduct = $productPrice['gross'] <= $cheapestVariantPrice['gross'] @@ -163,18 +162,16 @@ protected function isProductVisible(ProductEntity $product): bool { $salesChannelId = $this->exportContext->getSalesChannelId(); - $isVisible = false; if ($product->active) { foreach ($product->visibilities->getElements() as $productVisibility) { if ($productVisibility->salesChannelId === $salesChannelId - && $productVisibility->visibility >= ProductVisibilityDefinition::VISIBILITY_ALL + && $productVisibility->visibility >= self::VISIBILITY_ALL ) { - $isVisible = true; - break; + return true; } } } - return $isVisible; + return false; } }