Glue API: Product Bundle + Cart feature integration

Edit on GitHub

Follow the steps below to integrate the Glue API: Product Bundle + Cart feature.

Prerequisites

To start the feature integration, overview and install the necessary features:

NAME VERSION INTEGRATION GUIDE
Spryker Core 202204.0 Glue API: Spryker Core feature integration
Product Bundles 202204.0 Product Bundles feature integration
Cart 202204.0 Install the Cart Glue API

1) Install the required modules using Composer

Run the following command to install the required modules:

composer require spryker/product-bundle-carts-rest-api:"^1.0.0" --update-with-dependencies
Verification

Ensure that the following module has been installed:

Module Expected Directory
ProductBundleCartsRestApi vendor/spryker/product-bundle-carts-rest-api

2) Set up transfer objects

console transfer:generate
Verification

Ensure that the following changes have been applied in the transfer objects:

Transfer Type Event Path
RestErrorMessageTransfer class created src/Generated/Shared/Transfer/RestErrorMessageTransfer
ItemTransfer class created src/Generated/Shared/Transfer/ItemTransfer
RestCalculatedDiscountTransfer class created src/Generated/Shared/Transfer/RestCalculatedDiscountTransfer
ProductBundleStorageCriteriaTransfer class created src/Generated/Shared/Transfer/ProductBundleStorageCriteriaTransfer
RestItemsAttributesTransfer class created src/Generated/Shared/Transfer/RestItemsAttributesTransfer
RestCartItemCalculationsTransfer class created src/Generated/Shared/Transfer/RestCartItemCalculationsTransfer
QuoteTransfer class created src/Generated/Shared/Transfer/QuoteTransfer
CartItemRequestTransfer class created src/Generated/Shared/Transfer/CartItemRequestTransfer

3) Set up behavior

Activate the following plugins:

Plugin Specification Prerequisites Namespace
ProductBundleCartItemFilterPlugin Filters bundle items off the list of simple cart items. None Spryker\Glue\ProductBundleCartsRestApi\Plugin\CartsRestApi
BundleItemByQuoteResourceRelationshipPlugin Adds the bundle-items resource as a relationship by QuoteTransfer provided as a payload. None Spryker\Glue\ProductBundleCartsRestApi\Plugin\GlueApplication
BundledItemByQuoteResourceRelationshipPlugin Adds the bundled-items resource as a relationship to the bundle-items resource. Uses theQuoteTransfer payload of the bundle-items resource. None Spryker\Glue\ProductBundleCartsRestApi\Plugin\GlueApplication
GuestBundleItemByQuoteResourceRelationshipPlugin Adds the bundle-items resource as a relationship if QuoteTransfer is provided as a payload. It should be used for the guest-carts parent resource. None Spryker\Glue\ProductBundleCartsRestApi\Plugin\GlueApplication
BundleItemQuoteItemReadValidatorPlugin Checks if CartItemRequestTransfer is a bundle item in QuoteTransfer before performing update or delete operations on it. None Spryker\Zed\ProductBundleCartsRestApi\Communication\Plugin
src/Pyz/Glue/GlueApplication/GlueApplicationDependencyProvider.php
<?php

namespace Pyz\Glue\GlueApplication;

use Spryker\Glue\CartsRestApi\CartsRestApiConfig;
use Spryker\Glue\CartsRestApi\Plugin\GlueApplication\GuestCartItemsByQuoteResourceRelationshipPlugin;
use Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\ResourceRelationshipCollectionInterface;
use Spryker\Glue\ProductBundleCartsRestApi\Plugin\GlueApplication\BundleItemByQuoteResourceRelationshipPlugin;
use Spryker\Glue\ProductBundleCartsRestApi\Plugin\GlueApplication\BundledItemByQuoteResourceRelationshipPlugin;
use Spryker\Glue\ProductBundleCartsRestApi\Plugin\GlueApplication\GuestBundleItemByQuoteResourceRelationshipPlugin;
use Spryker\Glue\ProductBundleCartsRestApi\ProductBundleCartsRestApiConfig;
use Spryker\Glue\GlueApplication\GlueApplicationDependencyProvider as SprykerGlueApplicationDependencyProvider;
use Spryker\Glue\ProductsRestApi\Plugin\GlueApplication\ConcreteProductBySkuResourceRelationshipPlugin;

class GlueApplicationDependencyProvider extends SprykerGlueApplicationDependencyProvider
{
    /**
     * @param \Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\ResourceRelationshipCollectionInterface $resourceRelationshipCollection
     *
     * @return \Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\ResourceRelationshipCollectionInterface
     */
    protected function getResourceRelationshipPlugins(
        ResourceRelationshipCollectionInterface $resourceRelationshipCollection
    ): ResourceRelationshipCollectionInterface {
        $resourceRelationshipCollection->addRelationship(
            CartsRestApiConfig::RESOURCE_CARTS,
            new BundleItemByQuoteResourceRelationshipPlugin()
        );
        $resourceRelationshipCollection->addRelationship(
            CartsRestApiConfig::RESOURCE_GUEST_CARTS,
            new GuestBundleItemByQuoteResourceRelationshipPlugin()
        );
        $resourceRelationshipCollection->addRelationship(
            ProductBundleCartsRestApiConfig::RESOURCE_BUNDLE_ITEMS,
            new BundledItemByQuoteResourceRelationshipPlugin()
        );
        $resourceRelationshipCollection->addRelationship(
            ProductBundleCartsRestApiConfig::RESOURCE_BUNDLE_ITEMS,
            new ConcreteProductBySkuResourceRelationshipPlugin()
        );
        $resourceRelationshipCollection->addRelationship(
            ProductBundleCartsRestApiConfig::RESOURCE_BUNDLED_ITEMS,
            new ConcreteProductBySkuResourceRelationshipPlugin()
        );
        $resourceRelationshipCollection->addRelationship(
            CartsRestApiConfig::RESOURCE_GUEST_CARTS,
            new GuestCartItemsByQuoteResourceRelationshipPlugin()
        );

        return $resourceRelationshipCollection;
    }
}
Verification

Ensure that you have activated the plugins:

REQUEST TEST
GET https://glue.mysprykershop.com/carts/{{uuid}}?include=bundle-items
GET https://glue.mysprykershop.com/guest-carts/{{uuid}}?include=bundle-items
The bundle-items resource is returned as a relationship.
GET https://glue.mysprykershop.com/carts/{{uuid}}?include=bundle-items,bundled-items
GET https://glue.mysprykershop.com/guest-carts/{{uuid}}?include=bundle-items,bundled-items
The bundle-items resource has a relationship of the bundled-items resource.
GET https://glue.mysprykershop.com/carts/{{uuid}}?include=bundle-items,bundled-items,concrete-products
GET https://glue.mysprykershop.com/guest-carts/{{uuid}}?include=bundle-items,bundled-items,concrete-products
Concrete products are returned as relationships for bundle items and bundled items.

src/Pyz/Glue/CartsRestApi/CartsRestApiDependencyProvider.php

<?php

namespace Pyz\Glue\CartsRestApi;

use Spryker\Glue\CartsRestApi\CartsRestApiDependencyProvider as SprykerCartsRestApiDependencyProvider;
use Spryker\Glue\ProductBundleCartsRestApi\Plugin\CartsRestApi\ProductBundleCartItemFilterPlugin;

class CartsRestApiDependencyProvider extends SprykerCartsRestApiDependencyProvider
{
    /**
     * @return \Spryker\Glue\CartsRestApiExtension\Dependency\Plugin\CartItemFilterPluginInterface[]
     */
    protected function getCartItemFilterPlugins(): array
    {
        return [
            new ProductBundleCartItemFilterPlugin(),
        ];
    }
}
Verification

Ensure that you have activated the plugins:

  1. Add one or more bundle items to cart.

  2. Send the request: GET https://glue.mysprykershop.com/carts/:uuid?include=items.

  3. Check that the items of the bundle are not displayed as an items relationship.

src/Pyz/Zed/CartsRestApi/CartsRestApiDependencyProvider.php

<?php

namespace Pyz\Zed\CartsRestApi;

use Spryker\Zed\CartsRestApi\CartsRestApiDependencyProvider as SprykerCartsRestApiDependencyProvider;
use Spryker\Glue\ProductBundleCartsRestApi\Plugin\CartsRestApi\ProductBundleCartItemFilterPlugin;

class CartsRestApiDependencyProvider extends SprykerCartsRestApiDependencyProvider
{
    /**
     * @return \Spryker\Zed\CartsRestApiExtension\Dependency\Plugin\QuoteItemReadValidatorPluginInterface[]
     */
    protected function getQuoteItemReadValidatorPlugins(): array
    {
        return [
            new BundleItemQuoteItemReadValidatorPlugin(),
        ];
    }
}
Verification

Ensure that you can:

  • Edit bundle item quantity: PATCH https://glue.mysprykershop.com/carts/{{uuid}}/items/{{bundleItemGroupKey}}.
  • Delete a bundle from cart: DELETE https://glue.mysprykershop.com/carts/{{uuid}}/items/{{bundleItemGroupKey}}.

Integrate the following related features:

FEATURE REQUIRED FOR THE CURRENT FEATURE INTEGRATION GUIDE
Products Glue API: Products feature integration
Product Bundles Glue API: Product Bundles feature integration