Skip to content

Commit

Permalink
Merge pull request #58 from Setono/new-not-in-taxon-rule
Browse files Browse the repository at this point in the history
New has_not_taxon rule
  • Loading branch information
igormukhingmailcom authored Oct 12, 2021
2 parents 81c7d11 + f24e3d6 commit a32a263
Show file tree
Hide file tree
Showing 14 changed files with 419 additions and 42 deletions.
63 changes: 63 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,66 @@
```
ALTER TABLE setono_sylius_catalog_promotion__promotion CHANGE discount discount NUMERIC(10, 5) DEFAULT '0' NOT NULL;
```

2. Change rules configuration at your `catalog_promotion` fixtures:

- **Taxon**

Replace:

```
rules:
- type: "has_taxon"
configuration:
- "caps"
```

to:

```
rules:
- type: "has_taxon"
configuration:
taxons: # <---
- "caps"
```

- **Product**

Replace:

```
rules:
- type: "contains_product"
configuration: "santa-cap"
```

to:

```
rules:
- type: "contains_product"
configuration:
product: "santa-cap" # <---
```

- **Products**

Replace:

```
rules:
- type: "contains_products"
configuration:
- "santa-cap"
```

to:

```
rules:
- type: "contains_products"
configuration:
products: # <---
- "santa-cap"
```
5 changes: 4 additions & 1 deletion composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
"Sylius\\Component\\Core\\Formatter\\StringInflector",
"Sylius\\Component\\Core\\Model\\ChannelInterface",
"Sylius\\Component\\Core\\Model\\ChannelPricingInterface",
"Sylius\\Component\\Core\\Model\\ProductTaxon",
"Sylius\\Component\\Core\\Model\\ProductVariant",
"Sylius\\Component\\Core\\Repository\\ProductRepositoryInterface",
"Sylius\\Component\\Core\\Repository\\ProductVariantRepositoryInterface"
"Sylius\\Component\\Core\\Repository\\ProductVariantRepositoryInterface",
"Sylius\\Component\\Taxonomy\\Repository\\TaxonRepositoryInterface"
]
}
16 changes: 16 additions & 0 deletions src/Factory/PromotionRuleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Setono\SyliusCatalogPromotionPlugin\Model\PromotionRuleInterface;
use Setono\SyliusCatalogPromotionPlugin\Rule\ContainsProductRule;
use Setono\SyliusCatalogPromotionPlugin\Rule\ContainsProductsRule;
use Setono\SyliusCatalogPromotionPlugin\Rule\HasNotTaxonRule;
use Setono\SyliusCatalogPromotionPlugin\Rule\HasTaxonRule;
use function sprintf;
use Sylius\Component\Resource\Factory\FactoryInterface;
Expand Down Expand Up @@ -43,6 +44,11 @@ public function createByType(string $type, array $configuration, bool $strict =
Assert::isArray($configuration['taxons']);

return $this->createHasTaxon($configuration['taxons']);
case HasNotTaxonRule::TYPE:
Assert::keyExists($configuration, 'taxons');
Assert::isArray($configuration['taxons']);

return $this->createHasNotTaxon($configuration['taxons']);
case ContainsProductRule::TYPE:
Assert::keyExists($configuration, 'product');
Assert::string($configuration['product']);
Expand Down Expand Up @@ -75,6 +81,16 @@ public function createHasTaxon(array $taxonCodes): PromotionRuleInterface
);
}

public function createHasNotTaxon(array $taxonCodes): PromotionRuleInterface
{
Assert::allString($taxonCodes);

return $this->createPromotionRule(
HasNotTaxonRule::TYPE,
['taxons' => $taxonCodes]
);
}

public function createContainsProduct(string $productCode): PromotionRuleInterface
{
return $this->createPromotionRule(
Expand Down
2 changes: 2 additions & 0 deletions src/Factory/PromotionRuleFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public function createByType(string $type, array $configuration, bool $strict =

public function createHasTaxon(array $taxonCodes): PromotionRuleInterface;

public function createHasNotTaxon(array $taxonCodes): PromotionRuleInterface;

public function createContainsProduct(string $productCode): PromotionRuleInterface;

public function createContainsProducts(array $productCodes): PromotionRuleInterface;
Expand Down
37 changes: 37 additions & 0 deletions src/Form/Type/Rule/HasNotTaxonConfigurationType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusCatalogPromotionPlugin\Form\Type\Rule;

use Sylius\Bundle\TaxonomyBundle\Form\Type\TaxonAutocompleteChoiceType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\FormBuilderInterface;

final class HasNotTaxonConfigurationType extends AbstractType
{
private DataTransformerInterface $taxonsToCodesTransformer;

public function __construct(DataTransformerInterface $taxonsToCodesTransformer)
{
$this->taxonsToCodesTransformer = $taxonsToCodesTransformer;
}

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('taxons', TaxonAutocompleteChoiceType::class, [
'label' => 'setono_sylius_catalog_promotion.form.promotion_rule.has_taxon_configuration.taxons',
'multiple' => true,
])
;

$builder->get('taxons')->addModelTransformer($this->taxonsToCodesTransformer);
}

public function getBlockPrefix(): string
{
return 'setono_sylius_catalog_promotion_promotion_rule_has_not_taxon_configuration';
}
}
126 changes: 98 additions & 28 deletions src/Resources/config/app/fixtures.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,98 @@ sylius_fixtures:
suites:
default:
fixtures:

catalog_promotion_taxons:
name: taxon
options:
custom:
taxon_santa_caps:
code: "santa_caps"
slug: "santa-caps"
children:
- code: "pompon_santa_caps"
slug: "santa-caps/pompon"
name: "Santa caps with pompon"

product:
options:
custom:
product_cap_with_pompon:
code: "pompon_cap"
name: "Cap with pompon"
main_taxon: "caps"
taxons:
- 'caps'
- 'caps_with_pompons'
images:
- { path: '@SyliusCoreBundle/Resources/fixtures/caps/cap_01.jpg', type: 'main' }
channels:
- "FASHION_WEB"

product_santa_cap:
code: "santa-cap"
code: "santa_cap"
slug: "santa-cap"
name: "Santa cap"
main_taxon: "caps"
taxons:
- 'caps'
- 'santa_caps'
images:
- { path: '@SyliusCoreBundle/Resources/fixtures/caps/cap_03.jpg', type: 'main' }
channels:
- "FASHION_WEB"

product_santa_cap_with_pompon:
code: "pompon_santa_cap"
slug: "pompon-santa-cap"
name: "Santa cap with pompon"
main_taxon: "caps"
taxons:
- 'caps'
- 'santa_caps'
- 'pompon_santa_caps'
images:
- { path: '@SyliusCoreBundle/Resources/fixtures/caps/cap_01.jpg', type: 'main' }
channels:
- "FASHION_WEB"

catalog_promotion_random:
name: catalog_promotion
options:
random: 10
prototype:
rules:
- type: "has_taxon"
configuration:
taxons:
- "jeans"
- type: "has_taxon"
configuration:
taxons:
- "jeans"

catalog_promotion_tshirts:
name: catalog_promotion
options:
custom:
thirts:
code: "thirts_50_off"
name: "-90% for tshirts (except mens)"
priority: 1000
exclusive: true
starts_at: "now"
ends_at: "+14 day"
enabled: true
discount: 90.00
rules:
- type: "has_taxon"
configuration:
taxons:
- "t_shirts"
- type: "has_not_taxon"
configuration:
taxons:
- "mens_t_shirts"
channels:
- "FASHION_WEB"

catalog_promotion_custom:
catalog_promotion_caps:
name: catalog_promotion
options:
custom:
Expand All @@ -39,10 +105,10 @@ sylius_fixtures:
enabled: true
discount: 20.00
rules:
- type: "has_taxon"
configuration:
taxons:
- "caps"
- type: "has_taxon"
configuration:
taxons:
- "caps"
accidentally_disabled:
code: "accidentally_disabled"
name: "Accidentally disabled catalog promotion"
Expand All @@ -51,10 +117,10 @@ sylius_fixtures:
enabled: false
discount: 10.00
rules:
- type: "has_taxon"
configuration:
taxons:
- "caps"
- type: "has_taxon"
configuration:
taxons:
- "caps"

ny_caps_50_off:
code: "ny_caps_50_off"
Expand All @@ -67,12 +133,16 @@ sylius_fixtures:
enabled: true
discount: 50.00
rules:
- type: "has_taxon"
configuration:
taxons:
- "caps"
- type: "has_taxon"
configuration:
taxons:
- "caps"
- type: "has_not_taxon"
configuration:
taxons:
- "pompon_santa_caps"
channels:
- "FASHION_WEB"
- "FASHION_WEB"

ny_santa_cap_75_off:
code: "ny_santa_cap_75_off"
Expand All @@ -85,11 +155,11 @@ sylius_fixtures:
enabled: true
discount: 75.00
rules:
- type: "contains_product"
configuration:
product: "santa-cap"
- type: "contains_product"
configuration:
product: "santa_cap"
channels:
- "FASHION_WEB"
- "FASHION_WEB"

bf_santa_cap_75_off:
code: "bf_santa_cap_75_off"
Expand All @@ -102,9 +172,9 @@ sylius_fixtures:
enabled: true
discount: 75.00
rules:
- type: "contains_products"
configuration:
products:
- "santa-cap"
- type: "contains_products"
configuration:
products:
- "santa_cap"
channels:
- "FASHION_WEB"
- "FASHION_WEB"
6 changes: 6 additions & 0 deletions src/Resources/config/services/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
<tag name="form.type" />
</service>

<service id="setono_sylius_catalog_promotion.form.type.promotion_rule.has_not_taxon_configuration"
class="Setono\SyliusCatalogPromotionPlugin\Form\Type\Rule\HasNotTaxonConfigurationType">
<argument type="service" id="sylius.form.type.data_transformer.taxons_to_codes" />
<tag name="form.type" />
</service>

<service id="setono_sylius_catalog_promotion.form.type.promotion_rule.contains_products_configuration"
class="Setono\SyliusCatalogPromotionPlugin\Form\Type\Rule\ContainsProductsConfigurationType">
<argument type="service" id="sylius.form.type.data_transformer.products_to_codes" />
Expand Down
10 changes: 10 additions & 0 deletions src/Resources/config/services/rule.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@
<services>
<service id="setono_sylius_catalog_promotion.rule.has_taxon"
class="Setono\SyliusCatalogPromotionPlugin\Rule\HasTaxonRule">
<argument type="service" id="sylius.repository.taxon" />
<tag name="setono_sylius_catalog_promotion.rule"
type="has_taxon"
label="setono_sylius_catalog_promotion.form.promotion_rule.has_taxon"
form-type="Setono\SyliusCatalogPromotionPlugin\Form\Type\Rule\HasTaxonConfigurationType"/>
</service>

<service id="setono_sylius_catalog_promotion.rule.has_not_taxon"
class="Setono\SyliusCatalogPromotionPlugin\Rule\HasNotTaxonRule">
<argument type="service" id="sylius.repository.taxon" />
<tag name="setono_sylius_catalog_promotion.rule"
type="has_not_taxon"
label="setono_sylius_catalog_promotion.form.promotion_rule.has_not_taxon"
form-type="Setono\SyliusCatalogPromotionPlugin\Form\Type\Rule\HasNotTaxonConfigurationType"/>
</service>

<service id="setono_sylius_catalog_promotion.rule.contains_product"
class="Setono\SyliusCatalogPromotionPlugin\Rule\ContainsProductRule">
<tag name="setono_sylius_catalog_promotion.rule"
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ setono_sylius_catalog_promotion:
has_taxon: Product having one of taxons
has_taxon_configuration:
taxons: Taxons
has_not_taxon: Product not having any of taxons
has_not_taxon_configuration:
taxons: Taxons
taxonomy: Taxonomy
type: Type
Loading

0 comments on commit a32a263

Please sign in to comment.