diff --git a/Applicator/CatalogPromotionApplicator.php b/Applicator/CatalogPromotionApplicator.php index f57b7d061..f3c000c21 100644 --- a/Applicator/CatalogPromotionApplicator.php +++ b/Applicator/CatalogPromotionApplicator.php @@ -15,7 +15,6 @@ use Sylius\Bundle\CoreBundle\Formatter\AppliedPromotionInformationFormatterInterface; use Sylius\Component\Core\Model\CatalogPromotionInterface; -use Sylius\Component\Core\Model\ChannelPricingInterface; use Sylius\Component\Core\Model\ProductVariantInterface; use Sylius\Component\Promotion\Model\CatalogPromotionActionInterface; @@ -44,17 +43,18 @@ private function applyDiscountFromAction( ): void { $discount = $action->getConfiguration()['amount']; - /** @var ChannelPricingInterface $channelPricing */ - foreach ($variant->getChannelPricings() as $channelPricing) { + foreach ($catalogPromotion->getChannels() as $channel) { + $channelPricing = $variant->getChannelPricingForChannel($channel); + if ($channelPricing === null) { + continue; + } + if ($channelPricing->getOriginalPrice() === null) { $channelPricing->setOriginalPrice($channelPricing->getPrice()); } $channelPricing->setPrice((int) ($channelPricing->getPrice() - ($channelPricing->getPrice() * $discount))); - - $channelPricing->addAppliedPromotion( - $this->appliedPromotionInformationFormatter->format($catalogPromotion) - ); + $channelPricing->addAppliedPromotion($this->appliedPromotionInformationFormatter->format($catalogPromotion)); } } } diff --git a/Fixture/Factory/CatalogPromotionExampleFactory.php b/Fixture/Factory/CatalogPromotionExampleFactory.php index ce43f2b99..604f29903 100644 --- a/Fixture/Factory/CatalogPromotionExampleFactory.php +++ b/Fixture/Factory/CatalogPromotionExampleFactory.php @@ -132,21 +132,7 @@ protected function configureOptions(OptionsResolver $resolver): void ->setAllowedTypes('channels', 'array') ->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code')) ->setDefined('rules') - ->setNormalizer('rules', function (Options $options, array $rules): array { - if (empty($rules)) { - return [[]]; - } - - return $rules; - }) ->setDefined('actions') - ->setNormalizer('actions', function (Options $options, array $actions): array { - if (empty($actions)) { - return [[]]; - } - - return $actions; - }) ; } diff --git a/Provider/CatalogPromotionVariantsProvider.php b/Provider/CatalogPromotionVariantsProvider.php index 8c1c5bb9c..6f5392187 100644 --- a/Provider/CatalogPromotionVariantsProvider.php +++ b/Provider/CatalogPromotionVariantsProvider.php @@ -14,7 +14,6 @@ namespace Sylius\Bundle\CoreBundle\Provider; use Sylius\Component\Core\Model\CatalogPromotionInterface; -use Sylius\Component\Core\Model\ProductInterface; use Sylius\Component\Core\Model\ProductVariantInterface; use Sylius\Component\Core\Provider\CatalogPromotionVariantsProviderInterface; use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; @@ -39,17 +38,17 @@ public function provideEligibleVariants(CatalogPromotionInterface $catalogPromot /** We can do that for now, as we have only one rule */ if (array_key_exists('variants', $configuration)) { - $variants = $this->getVariantsProducts($configuration, $variants); + $variants = $this->getVariants($configuration['variants'], $variants); } } return $variants; } - private function getVariantsProducts(array $configuration, array $variants): array + private function getVariants(array $configuration, array $variants): array { /** @var string $variantCode */ - foreach ($configuration['variants'] as $variantCode) { + foreach ($configuration as $variantCode) { /** @var ProductVariantInterface|null $variant */ $variant = $this->productVariantRepository->findOneBy(['code' => $variantCode]); if ($variant === null) { diff --git a/spec/Applicator/CatalogPromotionApplicatorSpec.php b/spec/Applicator/CatalogPromotionApplicatorSpec.php index dac883aff..1ddea8e3d 100644 --- a/spec/Applicator/CatalogPromotionApplicatorSpec.php +++ b/spec/Applicator/CatalogPromotionApplicatorSpec.php @@ -19,10 +19,10 @@ use Sylius\Bundle\CoreBundle\Applicator\CatalogPromotionApplicatorInterface; use Sylius\Bundle\CoreBundle\Formatter\AppliedPromotionInformationFormatterInterface; use Sylius\Component\Core\Model\CatalogPromotionInterface; +use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\ChannelPricingInterface; use Sylius\Component\Core\Model\ProductVariantInterface; use Sylius\Component\Promotion\Model\CatalogPromotionActionInterface; -use Sylius\Component\Promotion\Model\CatalogPromotionTranslationInterface; final class CatalogPromotionApplicatorSpec extends ObjectBehavior { @@ -41,17 +41,19 @@ function it_applies_percentage_discount_on_all_products_variants( ProductVariantInterface $variant, CatalogPromotionInterface $catalogPromotion, CatalogPromotionActionInterface $catalogPromotionAction, + ChannelInterface $firstChannel, + ChannelInterface $secondChannel, ChannelPricingInterface $firstChannelPricing, ChannelPricingInterface $secondChannelPricing ): void { - $catalogPromotion->getActions()->willReturn(new ArrayCollection([ - $catalogPromotionAction->getWrappedObject(), + $catalogPromotion->getActions()->willReturn(new ArrayCollection([$catalogPromotionAction->getWrappedObject()])); + $catalogPromotion->getChannels()->willReturn(new ArrayCollection([ + $firstChannel->getWrappedObject(), + $secondChannel->getWrappedObject(), ])); - $variant->getChannelPricings()->willReturn(new ArrayCollection([ - $firstChannelPricing->getWrappedObject(), - $secondChannelPricing->getWrappedObject(), - ])); + $variant->getChannelPricingForChannel($firstChannel)->willReturn($firstChannelPricing); + $variant->getChannelPricingForChannel($secondChannel)->willReturn($secondChannelPricing); $appliedPromotionInformationFormatter->format($catalogPromotion)->willReturn(['winter_sale' => ['name' => 'Winter sale']]); $catalogPromotionAction->getConfiguration()->willReturn(['amount' => 0.3]); @@ -76,25 +78,22 @@ function it_does_not_set_original_price_during_application_if_its_already_there( ProductVariantInterface $variant, CatalogPromotionInterface $catalogPromotion, CatalogPromotionActionInterface $catalogPromotionAction, - CatalogPromotionTranslationInterface $translation, - ChannelPricingInterface $firstChannelPricing + ChannelInterface $channel, + ChannelPricingInterface $channelPricing ): void { - $variant->getChannelPricings()->willReturn(new ArrayCollection([ - $firstChannelPricing->getWrappedObject(), - ])); + $catalogPromotion->getActions()->willReturn(new ArrayCollection([$catalogPromotionAction->getWrappedObject()])); + $catalogPromotion->getChannels()->willReturn(new ArrayCollection([$channel->getWrappedObject()])); - $catalogPromotion->getActions()->willReturn(new ArrayCollection([ - $catalogPromotionAction->getWrappedObject(), - ])); + $variant->getChannelPricingForChannel($channel)->willReturn($channelPricing); $appliedPromotionInformationFormatter->format($catalogPromotion)->willReturn(['winter_sale' => ['name' => 'Winter sale']]); $catalogPromotionAction->getConfiguration()->willReturn(['amount' => 0.5]); - $firstChannelPricing->getPrice()->willReturn(1000); - $firstChannelPricing->getOriginalPrice()->willReturn(2000); - $firstChannelPricing->setOriginalPrice(Argument::any())->shouldNotBeCalled(); - $firstChannelPricing->setPrice(500)->shouldBeCalled(); - $firstChannelPricing->addAppliedPromotion(['winter_sale' => ['name' => 'Winter sale']])->shouldBeCalled(); + $channelPricing->getPrice()->willReturn(1000); + $channelPricing->getOriginalPrice()->willReturn(2000); + $channelPricing->setOriginalPrice(Argument::any())->shouldNotBeCalled(); + $channelPricing->setPrice(500)->shouldBeCalled(); + $channelPricing->addAppliedPromotion(['winter_sale' => ['name' => 'Winter sale']])->shouldBeCalled(); $this->applyCatalogPromotion($variant, $catalogPromotion); }