Skip to content

Commit 46895a1

Browse files
committed
Override the default product variant prices calculator to use our implementation instead
1 parent 021ea95 commit 46895a1

File tree

5 files changed

+34
-9
lines changed

5 files changed

+34
-9
lines changed

src/Checker/PreQualification/CompositePreQualificationChecker.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<?php
2-
declare(strict_types=1);
32

3+
declare(strict_types=1);
44

55
namespace Setono\SyliusCatalogPromotionPlugin\Checker\PreQualification;
66

7-
87
use Setono\CompositeCompilerPass\CompositeService;
98
use Setono\SyliusCatalogPromotionPlugin\Model\ProductInterface;
109
use Setono\SyliusCatalogPromotionPlugin\Model\PromotionInterface;
@@ -14,7 +13,6 @@
1413
*/
1514
final class CompositePreQualificationChecker extends CompositeService implements PreQualificationCheckerInterface
1615
{
17-
1816
public function isPreQualified(ProductInterface $product, PromotionInterface $catalogPromotion): bool
1917
{
2018
foreach ($this->services as $service) {

src/Checker/PreQualification/RulesPreQualificationChecker.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
2-
declare(strict_types=1);
32

3+
declare(strict_types=1);
44

55
namespace Setono\SyliusCatalogPromotionPlugin\Checker\PreQualification;
66

7-
87
use Setono\SyliusCatalogPromotionPlugin\Checker\PreQualification\Rule\RuleCheckerInterface;
98
use Setono\SyliusCatalogPromotionPlugin\Model\ProductInterface;
109
use Setono\SyliusCatalogPromotionPlugin\Model\PromotionInterface;
1110
use Setono\SyliusCatalogPromotionPlugin\Model\PromotionRuleInterface;
1211
use Sylius\Component\Registry\ServiceRegistryInterface;
12+
use Webmozart\Assert\Assert;
1313

1414
final class RulesPreQualificationChecker implements PreQualificationCheckerInterface
1515
{
@@ -34,8 +34,9 @@ public function isPreQualified(ProductInterface $product, PromotionInterface $ca
3434

3535
private function isEligibleToRule(ProductInterface $product, PromotionRuleInterface $rule): bool
3636
{
37-
/** @var RuleCheckerInterface $checker */
38-
$checker = $this->ruleRegistry->get($rule->getType());
37+
/** @var RuleCheckerInterface|object $checker */
38+
$checker = $this->ruleRegistry->get((string) $rule->getType());
39+
Assert::isInstanceOf($checker, RuleCheckerInterface::class);
3940

4041
return $checker->isEligible($product, $rule->getConfiguration());
4142
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusCatalogPromotionPlugin\DependencyInjection\Compiler;
6+
7+
use Setono\SyliusCatalogPromotionPlugin\Calculator\ProductVariantPricesCalculator;
8+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9+
use Symfony\Component\DependencyInjection\ContainerBuilder;
10+
11+
/**
12+
* This plugin has its own implementation of the \Sylius\Component\Core\Calculator\ProductVariantPricesCalculatorInterface
13+
* and hence we will override the default service with our own implementation.
14+
*/
15+
final class OverrideProductVariantPricesCalculatorPass implements CompilerPassInterface
16+
{
17+
public function process(ContainerBuilder $container): void
18+
{
19+
if (!$container->has('sylius.calculator.product_variant_price')) {
20+
return;
21+
}
22+
23+
$container->setAlias('sylius.calculator.product_variant_price', ProductVariantPricesCalculator::class);
24+
}
25+
}

src/SetonoSyliusCatalogPromotionPlugin.php

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Setono\CompositeCompilerPass\CompositeCompilerPass;
88
use Setono\SyliusCatalogPromotionPlugin\Checker\PreQualification\CompositePreQualificationChecker;
99
use Setono\SyliusCatalogPromotionPlugin\Checker\Runtime\CompositeRuntimeChecker;
10+
use Setono\SyliusCatalogPromotionPlugin\DependencyInjection\Compiler\OverrideProductVariantPricesCalculatorPass;
1011
use Setono\SyliusCatalogPromotionPlugin\DependencyInjection\Compiler\RegisterRulesAndRuleCheckersPass;
1112
use Sylius\Bundle\CoreBundle\Application\SyliusPluginTrait;
1213
use Sylius\Bundle\ResourceBundle\AbstractResourceBundle;
@@ -29,6 +30,7 @@ public function build(ContainerBuilder $container): void
2930
parent::build($container);
3031

3132
$container->addCompilerPass(new RegisterRulesAndRuleCheckersPass());
33+
$container->addCompilerPass(new OverrideProductVariantPricesCalculatorPass());
3234
$container->addCompilerPass(new CompositeCompilerPass(CompositePreQualificationChecker::class, 'setono_sylius_catalog_promotion.pre_qualification_checker'));
3335
$container->addCompilerPass(new CompositeCompilerPass(CompositeRuntimeChecker::class, 'setono_sylius_catalog_promotion.runtime_checker'));
3436
}

tests/Application/config/packages/_sylius.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ sylius_shop:
1313

1414
sylius_api:
1515
enabled: true
16-
17-
16+
1817
sylius_product:
1918
resources:
2019
product:

0 commit comments

Comments
 (0)