diff --git a/docs/_images/getting-started-with-sylius/shipping-calculator.png b/docs/_images/getting-started-with-sylius/shipping-calculator.png new file mode 100644 index 0000000000..f2249aa6b2 Binary files /dev/null and b/docs/_images/getting-started-with-sylius/shipping-calculator.png differ diff --git a/docs/_images/getting-started-with-sylius/shipping-cost-1.png b/docs/_images/getting-started-with-sylius/shipping-cost-1.png new file mode 100644 index 0000000000..cf97b43f2f Binary files /dev/null and b/docs/_images/getting-started-with-sylius/shipping-cost-1.png differ diff --git a/docs/_images/getting-started-with-sylius/shipping-cost-2.png b/docs/_images/getting-started-with-sylius/shipping-cost-2.png new file mode 100644 index 0000000000..bb6d7eeb91 Binary files /dev/null and b/docs/_images/getting-started-with-sylius/shipping-cost-2.png differ diff --git a/docs/getting-started-with-sylius/custom-business-logic.rst b/docs/getting-started-with-sylius/custom-business-logic.rst index cc03005933..55fd0793d0 100644 --- a/docs/getting-started-with-sylius/custom-business-logic.rst +++ b/docs/getting-started-with-sylius/custom-business-logic.rst @@ -1,2 +1,143 @@ Custom business logic ===================== + +Templates customization is just the beginning of the broad spectrum of customization possibilities in Sylius. There +are very few things in Sylius you're not able to customize or override. Let's take a look at one of the typical example of +customizing Sylius default logic, in this case, logic related to shipments and their cost. It's time for a custom shipping +calculator. + +Custom shipping calculator +-------------------------- + +Each shipping calculator is able to calculate a shipping cost for the provided order. This calculation is usually based on +bought products and some configuration done by Administrator. By default Sylius provides ``FlatRateCalculator`` and +``PerUnitRateCalculator`` (their names are quite self-explaining), but it's sometimes not enough. So let's say your store packs +ordered products in parcels and you need to charge a customer for each of them. + +You should start with the implementation of your custom shipping calculator service. Remember, that it must implement the +``CalculatorInterface`` from **Shipping Component**. Let's name it ``ParcelCalculator`` and place in ``src/ShippingCalculator`` +directory. + +.. code-block:: php + + # src/ShippingCalculator/ + + getUnits()->count() / $parcelSize); + + return (int) ($numberOfPackages * $parcelPrice); + } + + public function getType(): string + { + return 'parcel'; + } + } + +Two more things are needed to make it work. A form type, that would be used to pass some data to ``$configuration`` array +in the calculator service, and a proper service registration in ``services.yaml`` file. + +.. code-block:: php + + # src/Form/Type/ + + add('size', NumberType::class) + ->add('price', MoneyType::class, [ + 'currency' => 'USD', + ]) + ; + } + } + +.. attention:: + + The currency needed for ``MoneyType`` in the proposed implementation is hardcoded just for testing reasons. In a real application, + you should get the proper currency code from the repository, context or some configuration file. + +.. code-block:: yaml + + # config/services.yml + + services: + //... + + App\ShippingCalculator\ParcelCalculator: + tags: + - + { + name: sylius.shipping_calculator, + calculator: "parcel", + label: "Parcel", + form_type: App\Form\Type\ParcelShippingCalculatorType + } + +That's it! You should now be able to select your shipping calculator during the creation or edition of shipping method. + +.. image:: /_images/getting-started-with-sylius/shipping-calculator.png + :scale: 55% + :align: center + +| + +You can also see the results of your customization on checkout shipping step, how the shipping fee changes depending on how +many products you have in the cart. + +For 1 product: + +.. image:: /_images/getting-started-with-sylius/shipping-cost-1.png + :scale: 55% + :align: center + +| + +For 4 products: + +.. image:: /_images/getting-started-with-sylius/shipping-cost-2.png + :scale: 55% + :align: center + +| + +Amazing job! You've just provided your own logic into a Sylius-based system. Therefore, your store can provide a unique +experience for your Customers. Basing on this knowledge, your ready to customize your shop even more and make it as suitable +to your business needs as possible. + +Learn more +########## + +* :doc:`Customizations ` +* :doc:`Shipments ` +* :doc:`Checkout ` +* :doc:`Orders ` +* :doc:`Adjustments ` diff --git a/docs/getting-started-with-sylius/shop-customizations.rst b/docs/getting-started-with-sylius/shop-customizations.rst index e58c08899a..b9611d535f 100644 --- a/docs/getting-started-with-sylius/shop-customizations.rst +++ b/docs/getting-started-with-sylius/shop-customizations.rst @@ -13,7 +13,7 @@ Logo You can start with the shop panel. The default templates are elegant and straightforward, but for sure you would like to make them unique for your online store. Maybe some colors should be different? Or even the whole product page does not look like you want? Fortunately, twig templates are easy to override or customize (take a look at -:doc:`Customizing Templates cookbook ` for more info). +:doc:`Customizing Templates chapter` for more info). In the beginning, try a very simple, but also one of the most crucial change - displaying your shop logo instead of the Sylius' one.