Skip to content

Commit

Permalink
Merge pull request #3 from maximehuran/feature/allow-coupons-after-tax
Browse files Browse the repository at this point in the history
Allow coupons in after tax rules
  • Loading branch information
Kiwikoti authored Dec 14, 2023
2 parents ef1efd0 + de04a9e commit 475d5c6
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Context/PromotionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,27 @@

class PromotionContext implements PromotionContextInterface
{
protected bool $orderProcessor = false;

protected bool $afterTax = false;

public function isAfterTax(): bool
{
return $this->afterTax ?? false;
return $this->afterTax;
}

public function setAfterTax(bool $afterTax): void
{
$this->afterTax = $afterTax;
}

public function isOrderProcessor(): bool
{
return $this->orderProcessor;
}

public function setOrderProcessor(bool $orderProcessor): void
{
$this->orderProcessor = $orderProcessor;
}
}
4 changes: 4 additions & 0 deletions src/Context/PromotionContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ interface PromotionContextInterface
public function isAfterTax(): bool;

public function setAfterTax(bool $afterTax): void;

public function isOrderProcessor(): bool;

public function setOrderProcessor(bool $orderProcessor): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public function isEligible(
PromotionSubjectInterface $promotionSubject,
PromotionInterface $promotion
): bool {
if (!$this->promotionContext->isOrderProcessor()) {
return true;
}

/** @var AfterTaxAwareInterface $promotion */
Assert::isInstanceOf($promotion, AfterTaxAwareInterface::class);

Expand Down
37 changes: 37 additions & 0 deletions src/Promotion/Decorator/PromotionProcessorDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of Monsieur Biz' Advanced Promotion plugin for Sylius.
* (c) Monsieur Biz <[email protected]>
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusAdvancedPromotionPlugin\Promotion\Decorator;

use MonsieurBiz\SyliusAdvancedPromotionPlugin\Context\PromotionContextInterface;
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
use Sylius\Component\Promotion\Processor\PromotionProcessor;
use Sylius\Component\Promotion\Processor\PromotionProcessorInterface;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated;

#[AsDecorator('sylius.promotion_processor')]
final class PromotionProcessorDecorator implements PromotionProcessorInterface
{
public function __construct(
#[AutowireDecorated]
private readonly PromotionProcessor $promotionProcessor,
private readonly PromotionContextInterface $promotionContext,
) {
}

public function process(PromotionSubjectInterface $subject): void
{
$this->promotionContext->setOrderProcessor(true);
$this->promotionContext->setAfterTax(false);
$this->promotionProcessor->process($subject);
}
}
1 change: 1 addition & 0 deletions src/Promotion/Processor/AfterTaxPromotionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function __construct(
public function process(PromotionSubjectInterface $subject): void
{
// Set the promotion context to after cart
$this->promotionContext->setOrderProcessor(true);
$this->promotionContext->setAfterTax(true);

/**
Expand Down
41 changes: 41 additions & 0 deletions src/Resources/config/fixtures/advanced_promotion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,49 @@ sylius_fixtures:
suites:
default:
fixtures:
promotion:
options:
custom:
flash_sales:
code: 'flash_sales'
name: 'Flash sales'
exclusive: false
channels:
- 'FASHION_WEB'
coupon_based: true
coupons:
- code: 'FLASH_SALES'
rules:
- type: 'cart_quantity'
configuration:
count: 0
actions:
- type: 'order_fixed_discount'
configuration:
FASHION_WEB:
amount: 10.00
gift_card:
code: 'gift_card'
name: 'Gift card'
exclusive: false
channels:
- 'FASHION_WEB'
coupon_based: true
coupons:
- code: 'GIFT_CARD'
rules:
- type: 'cart_quantity'
configuration:
count: 0
actions:
- type: 'order_fixed_discount'
configuration:
FASHION_WEB:
amount: 30.00
monsieurbiz_advanced_promotion:
options:
promotion_advanced_configuration:
- code: 'new_year'
after_tax: true
- code: 'gift_card'
after_tax: true
5 changes: 5 additions & 0 deletions src/Resources/config/grids/promotion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ sylius_grid:
label: monsieurbiz_sylius_advanced_promotion.promotion.applied_after_tax
options:
template: "@SyliusUi/Grid/Field/yesNo.html.twig"
channels:
type: twig
label: sylius.ui.channels
options:
template: '@SyliusAdmin/Grid/Field/_channels.html.twig'
filters:
afterTax:
type: boolean
Expand Down

0 comments on commit 475d5c6

Please sign in to comment.