From e56425e6b745c2dd007286bbc26292f20ba92f81 Mon Sep 17 00:00:00 2001 From: Aleksander Laurowski Date: Wed, 26 May 2021 15:52:57 +0200 Subject: [PATCH 1/6] FFWEB-2049 Price export based on cart rules --- src/Export/Field/Price.php | 23 ++++++++++++++++++++--- src/Export/SalesChannelService.php | 21 ++++++++++++++------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/Export/Field/Price.php b/src/Export/Field/Price.php index dc8fb7fe..975a7c63 100644 --- a/src/Export/Field/Price.php +++ b/src/Export/Field/Price.php @@ -5,6 +5,8 @@ namespace Omikron\FactFinder\Shopware6\Export\Field; use Omikron\FactFinder\Shopware6\Export\Formatter\NumberFormatter; +use Omikron\FactFinder\Shopware6\Export\SalesChannelService; +use Shopware\Core\Content\Product\SalesChannel\Price\ProductPriceCalculator; use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity as Product; class Price implements FieldInterface @@ -12,9 +14,20 @@ class Price implements FieldInterface /** @var NumberFormatter */ private $numberFormatter; - public function __construct(NumberFormatter $numberFormatter) - { + /** @var ProductPriceCalculator */ + private $priceCalculator; + + /** @var SalesChannelService */ + private $channelService; + + public function __construct( + NumberFormatter $numberFormatter, + ProductPriceCalculator $priceCalculator, + SalesChannelService $channelService + ) { $this->numberFormatter = $numberFormatter; + $this->priceCalculator = $priceCalculator; + $this->channelService = $channelService; } public function getName(): string @@ -24,6 +37,10 @@ public function getName(): string public function getValue(Product $product): string { - return $this->numberFormatter->format((float) $product->getCalculatedPrice()->getTotalPrice()); + $this->priceCalculator->calculate([$product], $this->channelService->getSalesChannelContext()); + if ($product->getCalculatedPrices()->count() === 0) { + return $this->numberFormatter->format((float)$product->getCalculatedPrice()->getTotalPrice()); + } + return $this->numberFormatter->format((float)$product->getCalculatedPrices()->first()->getUnitPrice()); } } diff --git a/src/Export/SalesChannelService.php b/src/Export/SalesChannelService.php index d57e9f8f..e394063c 100644 --- a/src/Export/SalesChannelService.php +++ b/src/Export/SalesChannelService.php @@ -5,12 +5,14 @@ namespace Omikron\FactFinder\Shopware6\Export; use Omikron\FactFinder\Shopware6\BackwardCompatibility\Extension\SalesChannelContextFactoryInterface; +use Shopware\Core\Checkout\Cart\CartRuleLoader; use Shopware\Core\Defaults; use Shopware\Core\Framework\Api\Context\SystemSource; use Shopware\Core\Framework\Context; use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; +use Shopware\Core\Framework\Util\Random; use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService; use Shopware\Core\System\SalesChannel\SalesChannelContext; use Shopware\Core\System\SalesChannel\SalesChannelEntity; @@ -23,27 +25,32 @@ class SalesChannelService /** @var SalesChannelContextFactoryInterface */ private $channelContextFactory; + /** @var CartRuleLoader */ + private $cartRuleLoader; + /** @var SalesChannelContext|null */ - private $cachedSalesChannel; + private $cachedSalesChannelContext; public function __construct( SalesChannelContextFactoryInterface $channelContextFactory, - EntityRepositoryInterface $channelRepository + EntityRepositoryInterface $channelRepository, + CartRuleLoader $cartRuleLoader ) { $this->channelRepository = $channelRepository; $this->channelContextFactory = $channelContextFactory; + $this->cartRuleLoader = $cartRuleLoader; } public function getSalesChannelContext(SalesChannelEntity $salesChannel = null, $languageId = null): SalesChannelContext { - if (!$this->cachedSalesChannel) { - $usedChannel = $salesChannel ?: $this->getDefaultStoreFrontSalesChannel(); - $this->cachedSalesChannel = $this->channelContextFactory->create('', $usedChannel->getId(), [ + if (!$this->cachedSalesChannelContext) { + $usedChannel = $salesChannel ?: $this->getDefaultStoreFrontSalesChannel(); + $this->cachedSalesChannelContext = $this->channelContextFactory->create('', $usedChannel->getId(), [ SalesChannelContextService::LANGUAGE_ID => $languageId ?: $usedChannel->getLanguageId(), ]); } - - return $this->cachedSalesChannel; + $this->cartRuleLoader->loadByToken($this->cachedSalesChannelContext, Random::getAlphanumericString(32)); + return $this->cachedSalesChannelContext; } private function getDefaultStoreFrontSalesChannel(): SalesChannelEntity From 18d7983ec2421efb7eee2ba26452ba034be03f85 Mon Sep 17 00:00:00 2001 From: Aleksander Laurowski Date: Thu, 27 May 2021 07:21:25 +0200 Subject: [PATCH 2/6] fixup! FFWEB-2049 Price export based on cart rules --- spec/Export/Field/PriceSpec.php | 25 +++++++++++++++++++++++-- src/Export/Field/Price.php | 17 +---------------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/spec/Export/Field/PriceSpec.php b/spec/Export/Field/PriceSpec.php index 80dfebba..031f0204 100644 --- a/spec/Export/Field/PriceSpec.php +++ b/spec/Export/Field/PriceSpec.php @@ -8,6 +8,7 @@ use Omikron\FactFinder\Shopware6\Export\Formatter\NumberFormatter; use PhpSpec\ObjectBehavior; use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice; +use Shopware\Core\Checkout\Cart\Price\Struct\PriceCollection; use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity as Product; class PriceSpec extends ObjectBehavior @@ -27,11 +28,31 @@ function it_has_a_name() $this->getName()->shouldReturn('Price'); } - function it_gets_the_product_price(Product $product, CalculatedPrice $price, NumberFormatter $numberFormatter) - { + function it_gets_the_calculated_product_price_if_no_calculated_prices( + Product $product, + CalculatedPrice $price, + PriceCollection $priceCollection, + NumberFormatter $numberFormatter + ) { + $product->getCalculatedPrices()->willReturn($priceCollection); + $priceCollection->count()->willReturn(0); $product->getCalculatedPrice()->willReturn($price); $price->getTotalPrice()->willReturn(pi()); $numberFormatter->format(pi())->willReturn('3.14'); $this->getValue($product)->shouldReturn('3.14'); } + + function it_gets_the_first_calculated_price_if_exist( + Product $product, + PriceCollection $priceCollection, + CalculatedPrice $price, + NumberFormatter $numberFormatter + ) { + $priceCollection->count()->willReturn(1); + $product->getCalculatedPrices()->willReturn($priceCollection); + $priceCollection->first()->willReturn($price); + $price->getUnitPrice()->willReturn(pi()); + $numberFormatter->format(pi())->willReturn('3.14'); + $this->getValue($product)->shouldReturn('3.14'); + } } diff --git a/src/Export/Field/Price.php b/src/Export/Field/Price.php index 975a7c63..e319e6a8 100644 --- a/src/Export/Field/Price.php +++ b/src/Export/Field/Price.php @@ -5,8 +5,6 @@ namespace Omikron\FactFinder\Shopware6\Export\Field; use Omikron\FactFinder\Shopware6\Export\Formatter\NumberFormatter; -use Omikron\FactFinder\Shopware6\Export\SalesChannelService; -use Shopware\Core\Content\Product\SalesChannel\Price\ProductPriceCalculator; use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity as Product; class Price implements FieldInterface @@ -14,20 +12,8 @@ class Price implements FieldInterface /** @var NumberFormatter */ private $numberFormatter; - /** @var ProductPriceCalculator */ - private $priceCalculator; - - /** @var SalesChannelService */ - private $channelService; - - public function __construct( - NumberFormatter $numberFormatter, - ProductPriceCalculator $priceCalculator, - SalesChannelService $channelService - ) { + public function __construct(NumberFormatter $numberFormatter) { $this->numberFormatter = $numberFormatter; - $this->priceCalculator = $priceCalculator; - $this->channelService = $channelService; } public function getName(): string @@ -37,7 +23,6 @@ public function getName(): string public function getValue(Product $product): string { - $this->priceCalculator->calculate([$product], $this->channelService->getSalesChannelContext()); if ($product->getCalculatedPrices()->count() === 0) { return $this->numberFormatter->format((float)$product->getCalculatedPrice()->getTotalPrice()); } From 4003c0741adeb6d4e3675e08cea2ff9c633390d6 Mon Sep 17 00:00:00 2001 From: Aleksander Laurowski Date: Thu, 27 May 2021 07:22:13 +0200 Subject: [PATCH 3/6] fixup! FFWEB-2049 Price export based on cart rules --- src/Export/Field/Price.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Export/Field/Price.php b/src/Export/Field/Price.php index e319e6a8..665fc519 100644 --- a/src/Export/Field/Price.php +++ b/src/Export/Field/Price.php @@ -24,8 +24,8 @@ public function getName(): string public function getValue(Product $product): string { if ($product->getCalculatedPrices()->count() === 0) { - return $this->numberFormatter->format((float)$product->getCalculatedPrice()->getTotalPrice()); + return $this->numberFormatter->format((float) $product->getCalculatedPrice()->getTotalPrice()); } - return $this->numberFormatter->format((float)$product->getCalculatedPrices()->first()->getUnitPrice()); + return $this->numberFormatter->format((float) $product->getCalculatedPrices()->first()->getUnitPrice()); } } From be57e51dbb61ee80907efb0c7d786080fbdcf55e Mon Sep 17 00:00:00 2001 From: Aleksander Laurowski Date: Thu, 27 May 2021 07:23:46 +0200 Subject: [PATCH 4/6] fixup! FFWEB-2049 Price export based on cart rules --- src/Export/Field/Price.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Export/Field/Price.php b/src/Export/Field/Price.php index 665fc519..cbde7ffc 100644 --- a/src/Export/Field/Price.php +++ b/src/Export/Field/Price.php @@ -12,7 +12,8 @@ class Price implements FieldInterface /** @var NumberFormatter */ private $numberFormatter; - public function __construct(NumberFormatter $numberFormatter) { + public function __construct(NumberFormatter $numberFormatter) + { $this->numberFormatter = $numberFormatter; } From 855b356d1f4414f1765b09b255fe7b78526b88e5 Mon Sep 17 00:00:00 2001 From: Aleksander Laurowski Date: Thu, 27 May 2021 07:26:42 +0200 Subject: [PATCH 5/6] fixup! FFWEB-2049 Price export based on cart rules --- src/Export/SalesChannelService.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Export/SalesChannelService.php b/src/Export/SalesChannelService.php index e394063c..7c99cb1b 100644 --- a/src/Export/SalesChannelService.php +++ b/src/Export/SalesChannelService.php @@ -29,7 +29,7 @@ class SalesChannelService private $cartRuleLoader; /** @var SalesChannelContext|null */ - private $cachedSalesChannelContext; + private $cachedContext; public function __construct( SalesChannelContextFactoryInterface $channelContextFactory, @@ -43,14 +43,14 @@ public function __construct( public function getSalesChannelContext(SalesChannelEntity $salesChannel = null, $languageId = null): SalesChannelContext { - if (!$this->cachedSalesChannelContext) { - $usedChannel = $salesChannel ?: $this->getDefaultStoreFrontSalesChannel(); - $this->cachedSalesChannelContext = $this->channelContextFactory->create('', $usedChannel->getId(), [ + if (!$this->cachedContext) { + $usedChannel = $salesChannel ?: $this->getDefaultStoreFrontSalesChannel(); + $this->cachedContext = $this->channelContextFactory->create('', $usedChannel->getId(), [ SalesChannelContextService::LANGUAGE_ID => $languageId ?: $usedChannel->getLanguageId(), ]); } - $this->cartRuleLoader->loadByToken($this->cachedSalesChannelContext, Random::getAlphanumericString(32)); - return $this->cachedSalesChannelContext; + $this->cartRuleLoader->loadByToken($this->cachedContext, Random::getAlphanumericString(32)); + return $this->cachedContext; } private function getDefaultStoreFrontSalesChannel(): SalesChannelEntity From 3332f54687823a1dfbd8de6265336f0084a3b3ee Mon Sep 17 00:00:00 2001 From: Aleksander Laurowski Date: Thu, 27 May 2021 07:28:21 +0200 Subject: [PATCH 6/6] fixup! FFWEB-2049 Price export based on cart rules --- src/Export/SalesChannelService.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Export/SalesChannelService.php b/src/Export/SalesChannelService.php index 7c99cb1b..d30af5b2 100644 --- a/src/Export/SalesChannelService.php +++ b/src/Export/SalesChannelService.php @@ -17,6 +17,9 @@ use Shopware\Core\System\SalesChannel\SalesChannelContext; use Shopware\Core\System\SalesChannel\SalesChannelEntity; +/** + * @SuppressWarnings(PHPMD.StaticAccess) + */ class SalesChannelService { /** @var EntityRepositoryInterface */