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 dc8fb7fe..cbde7ffc 100644 --- a/src/Export/Field/Price.php +++ b/src/Export/Field/Price.php @@ -24,6 +24,9 @@ public function getName(): string public function getValue(Product $product): string { - return $this->numberFormatter->format((float) $product->getCalculatedPrice()->getTotalPrice()); + 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..d30af5b2 100644 --- a/src/Export/SalesChannelService.php +++ b/src/Export/SalesChannelService.php @@ -5,16 +5,21 @@ 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; +/** + * @SuppressWarnings(PHPMD.StaticAccess) + */ class SalesChannelService { /** @var EntityRepositoryInterface */ @@ -23,27 +28,32 @@ class SalesChannelService /** @var SalesChannelContextFactoryInterface */ private $channelContextFactory; + /** @var CartRuleLoader */ + private $cartRuleLoader; + /** @var SalesChannelContext|null */ - private $cachedSalesChannel; + private $cachedContext; 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->cachedContext) { + $usedChannel = $salesChannel ?: $this->getDefaultStoreFrontSalesChannel(); + $this->cachedContext = $this->channelContextFactory->create('', $usedChannel->getId(), [ SalesChannelContextService::LANGUAGE_ID => $languageId ?: $usedChannel->getLanguageId(), ]); } - - return $this->cachedSalesChannel; + $this->cartRuleLoader->loadByToken($this->cachedContext, Random::getAlphanumericString(32)); + return $this->cachedContext; } private function getDefaultStoreFrontSalesChannel(): SalesChannelEntity