Skip to content

Commit

Permalink
feature #13081 [CatalogPromotion][API] Apply catalog promotion in pro…
Browse files Browse the repository at this point in the history
…per channels (GSadee)

This PR was merged into the 1.11-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | no
| New feature?    | yes
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | Sylius/Sylius#13009
| License         | MIT

<!--
 - Bug fixes must be submitted against the 1.9 or 1.10 branch (the lowest possible)
 - Features and deprecations must be submitted against the master branch
 - Make sure that the correct base branch is set

 To be sure you are not breaking any Backward Compatibilities, check the documentation:
 https://docs.sylius.com/en/latest/book/organization/backward-compatibility-promise.html
-->


Commits
-------

f438c80efcd7751fa80addb52cc9ca3de1227c18 [CatalogPromotion][API] Add scenarios for applying catalog promotion in proper channels
6ffdba14b29013c9292083cea4770e79d3211a6f [CatalogPromotion][API] Improve and implement scenarios for applying catalog promotion in proper channels
bda8906104d790c2aca2ef12a2be80105a8a14ab [CatalogPromotion][API] Apply catalog promotion in proper channels
8f3c9d1187dcf1ff17d1874219f99b142dd1fdf5 [CatalogPromotion][Behat] Remove unused step definition
8305c485a38ab44e2581e92ac3d606e79d574a68 [CatalogPromotion][Fixtures] Remove unnecessary normalizing rules and actions in example factory
  • Loading branch information
AdamKasp authored Sep 10, 2021
2 parents e95b297 + 611967a commit f9adadf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 45 deletions.
14 changes: 7 additions & 7 deletions Applicator/CatalogPromotionApplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
}
}
}
14 changes: 0 additions & 14 deletions Fixture/Factory/CatalogPromotionExampleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
;
}

Expand Down
7 changes: 3 additions & 4 deletions Provider/CatalogPromotionVariantsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
39 changes: 19 additions & 20 deletions spec/Applicator/CatalogPromotionApplicatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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]);
Expand All @@ -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);
}
Expand Down

0 comments on commit f9adadf

Please sign in to comment.