Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOCS] API custom logic example #12627

Merged
merged 3 commits into from
May 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions docs/getting-started-with-sylius/custom-business-logic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,95 @@ For 4 products:

|

This customization will also work when using unified API without any extra steps:

First, you need to pick up a new cart:

.. code-block:: bash

curl -X POST "https://master.demo.sylius.com/api/v2/shop/orders" -H "accept: application/ld+json"

With body:

.. code-block:: json
arti0090 marked this conversation as resolved.
Show resolved Hide resolved

{
"localeCode": "string"
}

.. note::

The ``localeCode`` value is optional in the body of cart pickup. This means that if you won't provide it, the default locale from the channel will be used.

This should return a response with ``tokenValue`` which we would need for the next API calls:

.. code-block:: json

{
"shippingState": "string",
"tokenValue": "CART_TOKEN",
"id": 123,
Comment on lines +157 to +159
Copy link
Member

@GSadee GSadee May 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we shouldn't show the whole response

}

Then we need to add a product to the cart but first, it would be good to have any. You can use this call to retrieve some products:

.. code-block:: bash

curl -X GET "https://master.demo.sylius.com/api/v2/shop/products?page=1&itemsPerPage=30" -H "accept: application/ld+json"

And choose any product variant IRI that you would like to add to your cart:

.. code-block:: bash

curl --location --request PATCH 'https://master.demo.sylius.com/api/v2/shop/orders/CART_TOKEN/items' -H 'Content-Type: application/merge-patch+json'

With a chosen product variant in the body:

.. code-block:: json

{
"productVariant": "/api/v2/shop/product-variants/PRODUCT_VARIANT_CODE",
"quantity": 1
}

This should return a response with the cart that should contain a ``shippingTotal``:

.. code-block:: json

{
"taxTotal": 0,
"shippingTotal": 500,
"orderPromotionTotal": 0
}

.. attention::

API returns costs in decimal numbers that's why in response it is 500 currency unit shipping total (which stands for 5 USD in this case).

Now let's change the quantity of our product variant. We can do it by calling the endpoint above once again, or by utilizing the ``changeQuantity`` endpoint:

.. code-block:: bash

curl --location --request PATCH 'https://master.demo.sylius.com/api/v2/shop/orders/CART_TOKEN/items/ORDER_ITEM_ID' -H 'Content-Type: application/merge-patch+json'

With new quantity in the body:

.. code-block:: json

{
"quantity": 4
}

Which should return a response with the cart:

.. code-block:: json

{
"taxTotal": 0,
"shippingTotal": 1000,
"orderPromotionTotal": 0
}

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, you're ready to customize your shop even more and make it as suitable
to your business needs as possible.
Expand All @@ -141,3 +230,4 @@ Learn more
* :doc:`Checkout </book/orders/checkout>`
* :doc:`Orders </book/orders/orders>`
* :doc:`Adjustments </book/orders/adjustments>`
* :doc:`Unified API </api/index>`