Skip to content

Commit a32a263

Browse files
Merge pull request #58 from Setono/new-not-in-taxon-rule
New has_not_taxon rule
2 parents 81c7d11 + f24e3d6 commit a32a263

File tree

14 files changed

+419
-42
lines changed

14 files changed

+419
-42
lines changed

UPGRADE.md

+63
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,66 @@
1414
```
1515
ALTER TABLE setono_sylius_catalog_promotion__promotion CHANGE discount discount NUMERIC(10, 5) DEFAULT '0' NOT NULL;
1616
```
17+
18+
2. Change rules configuration at your `catalog_promotion` fixtures:
19+
20+
- **Taxon**
21+
22+
Replace:
23+
24+
```
25+
rules:
26+
- type: "has_taxon"
27+
configuration:
28+
- "caps"
29+
```
30+
31+
to:
32+
33+
```
34+
rules:
35+
- type: "has_taxon"
36+
configuration:
37+
taxons: # <---
38+
- "caps"
39+
```
40+
41+
- **Product**
42+
43+
Replace:
44+
45+
```
46+
rules:
47+
- type: "contains_product"
48+
configuration: "santa-cap"
49+
```
50+
51+
to:
52+
53+
```
54+
rules:
55+
- type: "contains_product"
56+
configuration:
57+
product: "santa-cap" # <---
58+
```
59+
60+
- **Products**
61+
62+
Replace:
63+
64+
```
65+
rules:
66+
- type: "contains_products"
67+
configuration:
68+
- "santa-cap"
69+
```
70+
71+
to:
72+
73+
```
74+
rules:
75+
- type: "contains_products"
76+
configuration:
77+
products: # <---
78+
- "santa-cap"
79+
```

composer-require-checker.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
"Sylius\\Component\\Core\\Formatter\\StringInflector",
3333
"Sylius\\Component\\Core\\Model\\ChannelInterface",
3434
"Sylius\\Component\\Core\\Model\\ChannelPricingInterface",
35+
"Sylius\\Component\\Core\\Model\\ProductTaxon",
36+
"Sylius\\Component\\Core\\Model\\ProductVariant",
3537
"Sylius\\Component\\Core\\Repository\\ProductRepositoryInterface",
36-
"Sylius\\Component\\Core\\Repository\\ProductVariantRepositoryInterface"
38+
"Sylius\\Component\\Core\\Repository\\ProductVariantRepositoryInterface",
39+
"Sylius\\Component\\Taxonomy\\Repository\\TaxonRepositoryInterface"
3740
]
3841
}

src/Factory/PromotionRuleFactory.php

+16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Setono\SyliusCatalogPromotionPlugin\Model\PromotionRuleInterface;
99
use Setono\SyliusCatalogPromotionPlugin\Rule\ContainsProductRule;
1010
use Setono\SyliusCatalogPromotionPlugin\Rule\ContainsProductsRule;
11+
use Setono\SyliusCatalogPromotionPlugin\Rule\HasNotTaxonRule;
1112
use Setono\SyliusCatalogPromotionPlugin\Rule\HasTaxonRule;
1213
use function sprintf;
1314
use Sylius\Component\Resource\Factory\FactoryInterface;
@@ -43,6 +44,11 @@ public function createByType(string $type, array $configuration, bool $strict =
4344
Assert::isArray($configuration['taxons']);
4445

4546
return $this->createHasTaxon($configuration['taxons']);
47+
case HasNotTaxonRule::TYPE:
48+
Assert::keyExists($configuration, 'taxons');
49+
Assert::isArray($configuration['taxons']);
50+
51+
return $this->createHasNotTaxon($configuration['taxons']);
4652
case ContainsProductRule::TYPE:
4753
Assert::keyExists($configuration, 'product');
4854
Assert::string($configuration['product']);
@@ -75,6 +81,16 @@ public function createHasTaxon(array $taxonCodes): PromotionRuleInterface
7581
);
7682
}
7783

84+
public function createHasNotTaxon(array $taxonCodes): PromotionRuleInterface
85+
{
86+
Assert::allString($taxonCodes);
87+
88+
return $this->createPromotionRule(
89+
HasNotTaxonRule::TYPE,
90+
['taxons' => $taxonCodes]
91+
);
92+
}
93+
7894
public function createContainsProduct(string $productCode): PromotionRuleInterface
7995
{
8096
return $this->createPromotionRule(

src/Factory/PromotionRuleFactoryInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public function createByType(string $type, array $configuration, bool $strict =
1313

1414
public function createHasTaxon(array $taxonCodes): PromotionRuleInterface;
1515

16+
public function createHasNotTaxon(array $taxonCodes): PromotionRuleInterface;
17+
1618
public function createContainsProduct(string $productCode): PromotionRuleInterface;
1719

1820
public function createContainsProducts(array $productCodes): PromotionRuleInterface;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusCatalogPromotionPlugin\Form\Type\Rule;
6+
7+
use Sylius\Bundle\TaxonomyBundle\Form\Type\TaxonAutocompleteChoiceType;
8+
use Symfony\Component\Form\AbstractType;
9+
use Symfony\Component\Form\DataTransformerInterface;
10+
use Symfony\Component\Form\FormBuilderInterface;
11+
12+
final class HasNotTaxonConfigurationType extends AbstractType
13+
{
14+
private DataTransformerInterface $taxonsToCodesTransformer;
15+
16+
public function __construct(DataTransformerInterface $taxonsToCodesTransformer)
17+
{
18+
$this->taxonsToCodesTransformer = $taxonsToCodesTransformer;
19+
}
20+
21+
public function buildForm(FormBuilderInterface $builder, array $options): void
22+
{
23+
$builder
24+
->add('taxons', TaxonAutocompleteChoiceType::class, [
25+
'label' => 'setono_sylius_catalog_promotion.form.promotion_rule.has_taxon_configuration.taxons',
26+
'multiple' => true,
27+
])
28+
;
29+
30+
$builder->get('taxons')->addModelTransformer($this->taxonsToCodesTransformer);
31+
}
32+
33+
public function getBlockPrefix(): string
34+
{
35+
return 'setono_sylius_catalog_promotion_promotion_rule_has_not_taxon_configuration';
36+
}
37+
}

src/Resources/config/app/fixtures.yaml

+98-28
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,98 @@ sylius_fixtures:
22
suites:
33
default:
44
fixtures:
5+
6+
catalog_promotion_taxons:
7+
name: taxon
8+
options:
9+
custom:
10+
taxon_santa_caps:
11+
code: "santa_caps"
12+
slug: "santa-caps"
13+
children:
14+
- code: "pompon_santa_caps"
15+
slug: "santa-caps/pompon"
16+
name: "Santa caps with pompon"
17+
518
product:
619
options:
720
custom:
21+
product_cap_with_pompon:
22+
code: "pompon_cap"
23+
name: "Cap with pompon"
24+
main_taxon: "caps"
25+
taxons:
26+
- 'caps'
27+
- 'caps_with_pompons'
28+
images:
29+
- { path: '@SyliusCoreBundle/Resources/fixtures/caps/cap_01.jpg', type: 'main' }
30+
channels:
31+
- "FASHION_WEB"
32+
833
product_santa_cap:
9-
code: "santa-cap"
34+
code: "santa_cap"
35+
slug: "santa-cap"
1036
name: "Santa cap"
1137
main_taxon: "caps"
1238
taxons:
1339
- 'caps'
40+
- 'santa_caps'
1441
images:
1542
- { path: '@SyliusCoreBundle/Resources/fixtures/caps/cap_03.jpg', type: 'main' }
1643
channels:
1744
- "FASHION_WEB"
1845

46+
product_santa_cap_with_pompon:
47+
code: "pompon_santa_cap"
48+
slug: "pompon-santa-cap"
49+
name: "Santa cap with pompon"
50+
main_taxon: "caps"
51+
taxons:
52+
- 'caps'
53+
- 'santa_caps'
54+
- 'pompon_santa_caps'
55+
images:
56+
- { path: '@SyliusCoreBundle/Resources/fixtures/caps/cap_01.jpg', type: 'main' }
57+
channels:
58+
- "FASHION_WEB"
59+
1960
catalog_promotion_random:
2061
name: catalog_promotion
2162
options:
2263
random: 10
2364
prototype:
2465
rules:
25-
- type: "has_taxon"
26-
configuration:
27-
taxons:
28-
- "jeans"
66+
- type: "has_taxon"
67+
configuration:
68+
taxons:
69+
- "jeans"
70+
71+
catalog_promotion_tshirts:
72+
name: catalog_promotion
73+
options:
74+
custom:
75+
thirts:
76+
code: "thirts_50_off"
77+
name: "-90% for tshirts (except mens)"
78+
priority: 1000
79+
exclusive: true
80+
starts_at: "now"
81+
ends_at: "+14 day"
82+
enabled: true
83+
discount: 90.00
84+
rules:
85+
- type: "has_taxon"
86+
configuration:
87+
taxons:
88+
- "t_shirts"
89+
- type: "has_not_taxon"
90+
configuration:
91+
taxons:
92+
- "mens_t_shirts"
93+
channels:
94+
- "FASHION_WEB"
2995

30-
catalog_promotion_custom:
96+
catalog_promotion_caps:
3197
name: catalog_promotion
3298
options:
3399
custom:
@@ -39,10 +105,10 @@ sylius_fixtures:
39105
enabled: true
40106
discount: 20.00
41107
rules:
42-
- type: "has_taxon"
43-
configuration:
44-
taxons:
45-
- "caps"
108+
- type: "has_taxon"
109+
configuration:
110+
taxons:
111+
- "caps"
46112
accidentally_disabled:
47113
code: "accidentally_disabled"
48114
name: "Accidentally disabled catalog promotion"
@@ -51,10 +117,10 @@ sylius_fixtures:
51117
enabled: false
52118
discount: 10.00
53119
rules:
54-
- type: "has_taxon"
55-
configuration:
56-
taxons:
57-
- "caps"
120+
- type: "has_taxon"
121+
configuration:
122+
taxons:
123+
- "caps"
58124

59125
ny_caps_50_off:
60126
code: "ny_caps_50_off"
@@ -67,12 +133,16 @@ sylius_fixtures:
67133
enabled: true
68134
discount: 50.00
69135
rules:
70-
- type: "has_taxon"
71-
configuration:
72-
taxons:
73-
- "caps"
136+
- type: "has_taxon"
137+
configuration:
138+
taxons:
139+
- "caps"
140+
- type: "has_not_taxon"
141+
configuration:
142+
taxons:
143+
- "pompon_santa_caps"
74144
channels:
75-
- "FASHION_WEB"
145+
- "FASHION_WEB"
76146

77147
ny_santa_cap_75_off:
78148
code: "ny_santa_cap_75_off"
@@ -85,11 +155,11 @@ sylius_fixtures:
85155
enabled: true
86156
discount: 75.00
87157
rules:
88-
- type: "contains_product"
89-
configuration:
90-
product: "santa-cap"
158+
- type: "contains_product"
159+
configuration:
160+
product: "santa_cap"
91161
channels:
92-
- "FASHION_WEB"
162+
- "FASHION_WEB"
93163

94164
bf_santa_cap_75_off:
95165
code: "bf_santa_cap_75_off"
@@ -102,9 +172,9 @@ sylius_fixtures:
102172
enabled: true
103173
discount: 75.00
104174
rules:
105-
- type: "contains_products"
106-
configuration:
107-
products:
108-
- "santa-cap"
175+
- type: "contains_products"
176+
configuration:
177+
products:
178+
- "santa_cap"
109179
channels:
110-
- "FASHION_WEB"
180+
- "FASHION_WEB"

src/Resources/config/services/form.xml

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
<tag name="form.type" />
5151
</service>
5252

53+
<service id="setono_sylius_catalog_promotion.form.type.promotion_rule.has_not_taxon_configuration"
54+
class="Setono\SyliusCatalogPromotionPlugin\Form\Type\Rule\HasNotTaxonConfigurationType">
55+
<argument type="service" id="sylius.form.type.data_transformer.taxons_to_codes" />
56+
<tag name="form.type" />
57+
</service>
58+
5359
<service id="setono_sylius_catalog_promotion.form.type.promotion_rule.contains_products_configuration"
5460
class="Setono\SyliusCatalogPromotionPlugin\Form\Type\Rule\ContainsProductsConfigurationType">
5561
<argument type="service" id="sylius.form.type.data_transformer.products_to_codes" />

src/Resources/config/services/rule.xml

+10
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@
66
<services>
77
<service id="setono_sylius_catalog_promotion.rule.has_taxon"
88
class="Setono\SyliusCatalogPromotionPlugin\Rule\HasTaxonRule">
9+
<argument type="service" id="sylius.repository.taxon" />
910
<tag name="setono_sylius_catalog_promotion.rule"
1011
type="has_taxon"
1112
label="setono_sylius_catalog_promotion.form.promotion_rule.has_taxon"
1213
form-type="Setono\SyliusCatalogPromotionPlugin\Form\Type\Rule\HasTaxonConfigurationType"/>
1314
</service>
1415

16+
<service id="setono_sylius_catalog_promotion.rule.has_not_taxon"
17+
class="Setono\SyliusCatalogPromotionPlugin\Rule\HasNotTaxonRule">
18+
<argument type="service" id="sylius.repository.taxon" />
19+
<tag name="setono_sylius_catalog_promotion.rule"
20+
type="has_not_taxon"
21+
label="setono_sylius_catalog_promotion.form.promotion_rule.has_not_taxon"
22+
form-type="Setono\SyliusCatalogPromotionPlugin\Form\Type\Rule\HasNotTaxonConfigurationType"/>
23+
</service>
24+
1525
<service id="setono_sylius_catalog_promotion.rule.contains_product"
1626
class="Setono\SyliusCatalogPromotionPlugin\Rule\ContainsProductRule">
1727
<tag name="setono_sylius_catalog_promotion.rule"

src/Resources/translations/messages.en.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,8 @@ setono_sylius_catalog_promotion:
3535
has_taxon: Product having one of taxons
3636
has_taxon_configuration:
3737
taxons: Taxons
38+
has_not_taxon: Product not having any of taxons
39+
has_not_taxon_configuration:
40+
taxons: Taxons
3841
taxonomy: Taxonomy
3942
type: Type

0 commit comments

Comments
 (0)