Skip to content

Compare: Shop integration

20 changes: 11 additions & 9 deletions Shop integration.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This tutorial explains how you can add the parts of a product configuration to the user's shopping cart when the "request product" button is clicked.
This tutorial explains how you can add the parts of a product or plan configuration to the user's shopping cart when the "add to cart" button is clicked.

You can implement this feature with any shop software or Kirby shop plugin. To keep it simple, this tutorial only shows the general setup for a shop that can receive products from Kirby backend code.

Expand All @@ -15,12 +15,14 @@ return [
'pattern' => 'configuration-to-cart',
'method' => 'POST',
'action' => function () {
if ($configuration = roomleConfiguration()) {
foreach ($configuration->parts() as $part) {
yourShop()->addToCart([
'articleNr' => $part->articleNr(),
'parameters' => $part->parameters()->rawData()
]);
if ($plan = roomlePlan()) {
foreach ($plan->items() as $item) {
foreach ($item->parts() as $part) {
yourShop()->addToCart([
'articleNr' => $part->articleNr(),
'parameters' => $part->parameters()->rawData()
]);
}
}
}

Expand All @@ -33,11 +35,11 @@ return [

This example only passes the article numbers and the parameter data (custom options like size, color and features depending on the part) to the shop. This works if the shop knows all the part details such as name and price. It is recommended to use this approach if possible. This ensures that users cannot manipulate the data and e.g. change the price of the parts.

If you really need to use data from the client and have other protections against manipulations, you can also access other fields from the configuration objects. To see what is possible, please take a look at the [plugin classes](https://github.com/lukasbestle/kirby-roomle/tree/main/src/classes), particularly `Configuration`, `Parameter` and `Part`.
If you really need to use data from the client and have other protections against manipulations, you can also access other fields from the configuration objects. To see what is possible, please take a look at the [plugin classes](https://github.com/lukasbestle/kirby-roomle/tree/main/src/classes), particularly `Configuration`, `Parameter`, `Part` and `Plan`.

## 2. Point the plugin to the custom route

Now you need to tell the Roomle plugin that it should submit the configuration data to this custom route whenever the "request product" button is clicked. You can do this by setting the `target` option in `site/config/config.php`:
Now you need to tell the Roomle plugin that it should submit the configuration data to this custom route whenever the "add to cart" button is clicked. You can do this by setting the `target` option in `site/config/config.php`:

```php
<?php
Expand Down