From f24a490c61d6732d0da44a73e8974c95c7ee7080 Mon Sep 17 00:00:00 2001 From: arti0090 Date: Wed, 6 Oct 2021 07:58:22 +0200 Subject: [PATCH] Create the doc about making new CP scope --- .../custom-catalog-promotion-scope.rst | 166 ++++++++++++++++++ docs/cookbook/catalog-promotions/map.rst.inc | 1 + docs/cookbook/index.rst | 9 + 3 files changed, 176 insertions(+) create mode 100644 docs/cookbook/catalog-promotions/custom-catalog-promotion-scope.rst create mode 100644 docs/cookbook/catalog-promotions/map.rst.inc diff --git a/docs/cookbook/catalog-promotions/custom-catalog-promotion-scope.rst b/docs/cookbook/catalog-promotions/custom-catalog-promotion-scope.rst new file mode 100644 index 000000000000..b849e3fb8779 --- /dev/null +++ b/docs/cookbook/catalog-promotions/custom-catalog-promotion-scope.rst @@ -0,0 +1,166 @@ +How to add a custom catalog promotion scope? +============================================ + +Adding new, custom catalog promotion scope to your shop should become a quite helpful extension to your own Catalog Promotions. +You can imagine for instance, that you have some custom way of aggregating products, or any other method of filtering them. +These products that will fulfil your specific scope will become eligible for actions of Catalog Promotion, and as we know +cheaper Products attract more customers. +Let's try implementing the new **Catalog Promotion Scope** in this cookbook, that will work with Products that contains a phrase. + +.. note:: + + If you are familiar with **Promotions** and you know how **Promotion Rules** work, + than the Catalog Promotion Scope should look familiar, as the concept of them is quite similar. + +Create a new catalog promotion scope +------------------------------------ + +The new Scope needs to be declared somewhere, it would be nice to extend the current interface first: + +.. code-block:: php + + buildViolation("There is no phrase provided")->atPath('configuration.phrase')->addViolation(); + } + } + } + +Alright now we have a working basic validation, and our new type Scope exists. We should now create a Provider that will return +for us all of eligible product variants. We can start with config: + +.. code-block:: yaml + + # config/services.yaml + + App\Provider\ByPhraseVariantsProvider: + arguments: + - '@sylius.repository.product_variant' + tags: + - { name: 'sylius.catalog_promotion.variants_provider' } + +.. note:: + + Please take a note on tags of Validator and Provider, thanks to them declared those services are working properly. + +And the code for the provider itself: + +.. code-block:: php + + productVariantRepository = $productVariantRepository; + } + + public function supports(CatalogPromotionScopeInterface $catalogPromotionScopeType): bool + { + return $catalogPromotionScopeType->getType() === \App\Model\CatalogPromotionScopeInterface::TYPE_BY_PHRASE; + } + + public function provideEligibleVariants(CatalogPromotionScopeInterface $scope): array + { + $configuration = $scope->getConfiguration(); + Assert::keyExists($configuration, 'phrase', 'This rule should have configured phrase'); + + return $this->productVariantRepository->findByPhrase($configuration['amount'], 'en_US'); + } + } + +.. note:: + + In this example there is hardcoded locale in ``->findByPhrase($configuration['amount'], 'en_US')`` but you can use LocaleContextInterface + or extend the code from this cookbook to e.g. consume key ``localeCode`` from configuration. + +Now the Catalog Promotion should work with your new Scope for programmatically and API created resource. +Lets now prepare a validation for UI part by using form types. + + +###### SECTION TODO +Prepare a configuration form type for your new scope +---------------------------------------------------- + +To be able to configure a Catalog Promotion with your new scope you will need a form type for the admin panel. + +That's all. You will now be able to choose the new scope while creating a new catalog promotion. + +#end todo + +Learn more +---------- + +* :doc:`Customization Guide ` +* :doc:`Catalog Promotion Concept Book ` diff --git a/docs/cookbook/catalog-promotions/map.rst.inc b/docs/cookbook/catalog-promotions/map.rst.inc new file mode 100644 index 000000000000..5d4591ade574 --- /dev/null +++ b/docs/cookbook/catalog-promotions/map.rst.inc @@ -0,0 +1 @@ +* :doc:`/cookbook/promotions/custom-catalog-promotion-scope` diff --git a/docs/cookbook/index.rst b/docs/cookbook/index.rst index 3a8277b102e6..456e1f001bad 100644 --- a/docs/cookbook/index.rst +++ b/docs/cookbook/index.rst @@ -76,6 +76,15 @@ Promotions .. include:: /cookbook/promotions/map.rst.inc +Catalog Promotions +------------------ + +.. toctree:: + :hidden: + + catalog-promotions/custom-catalog-promotion-scope + + Inventory ---------