Skip to content

Commit

Permalink
minor #12731 Add cookbook with how to change tax address (arti0090)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.9 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | 1.9
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | same as #12724 but on 1.9 branch
| License         | MIT

Opened new PR as old one was making a lot of merge conflicts.

Commits
-------

934749e Add cookbook with how to change tax address
  • Loading branch information
GSadee committed Jun 22, 2021
2 parents 234eedd + 934749e commit 73e1efe
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/cookbook/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,13 @@ Frontend
frontend/webpack.rst

.. include:: /cookbook/frontend/map.rst.inc

Taxation
--------

.. toctree::
:hidden:

taxation/customize-tax-by-address

.. include:: /cookbook/taxation/map.rst.inc
49 changes: 49 additions & 0 deletions docs/cookbook/taxation/customize-tax-by-address.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
How to configure tax rates to be based on shipping address?
===========================================================

The default configuration of Sylius tax calculation is based on billing address but there are situations where we would
like to use a shipping address to be used in this process. This may be useful to anyone who uses Sylius in European Union
as from 1st July 2021 the new taxation rules will be applied.

.. note::

You can learn more about new EU taxation rules `here <https://ec.europa.eu/taxation_customs/business/vat/modernising-vat-cross-border-ecommerce_en>`_.

To change the way how the taxes are calculated; by billing or by shipping address, you need to override the service called
``OrderTaxesProcessor.php`` from ``Sylius/Component/Core/OrderProcessing``.

First let's copy code from original Processor to our service
from ``%kernel.project_dir%/vendor/sylius/sylius/src/Sylius/Component/Core/OrderProcessing/OrderTaxesProcessor.php`` to ``src/OrderProcessing/OrderTaxesProcessor.php``

Then register our new service:

.. code-block:: yaml
# app/config/services.yaml
App\OrderProcessing\OrderTaxesProcessor:
arguments:
- '@sylius.provider.channel_based_default_zone_provider'
- '@sylius.zone_matcher'
- '@sylius.registry.tax_calculation_strategy'
tags:
- { name: sylius.order_processor, priority: 10 }
Now we need to change the method ``getTaxZone`` to be using the shipping address:

.. code-block:: php
//...
private function getTaxZone(OrderInterface $order): ?ZoneInterface
{
$shippingAddress = $order->getShippingAddress();
$zone = null;
if (null !== $shippingAddress) {
$zone = $this->zoneMatcher->match($shippingAddress, Scope::TAX);
}
return $zone ?: $this->defaultTaxZoneProvider->getZone($order);
}
//...
And with this change, the way how taxes are calculated will be based on shipping address.
1 change: 1 addition & 0 deletions docs/cookbook/taxation/map.rst.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* :doc:`/cookbook/taxation/customize-tax-by-address`

0 comments on commit 73e1efe

Please sign in to comment.