How to Stay on Product Page After Adding to Cart

By default, Sylius redirects the customer to the cart summary page after adding a product to the cart. This guide shows how to change this behavior to keep the customer on the product page.

Understanding the Default Behavior

The AddToCartFormComponent has two properties that control the redirect behavior:

Property
Default Value
Description

routeName

sylius_shop_cart_summary

Route to redirect after adding to cart

routeParameters

[]

Parameters passed to the route

Overriding the Redirect

To stay on the product page after adding to cart, override these properties in the twig hook configuration.

Create a new file in your project:

# config/packages/twig_hooks/product/show.yaml
sylius_twig_hooks:
    hooks:
        'sylius_shop.product.show.content.info.summary':
            add_to_cart:
                component: 'sylius_shop:product:add_to_cart_form'
                props:
                    product: '@=_context.product'
                    template: '@SyliusShop/product/show/content/info/summary/add_to_cart.html.twig'
                    routeName: 'sylius_shop_product_show'
                    routeParameters:
                        slug: '@=_context.product.getTranslation().getSlug()'
                priority: 100

The key changes are:

  • routeName: 'sylius_shop_product_show' - redirects back to the product page

  • routeParameters.slug - passes the product slug to generate the correct URL

Last updated

Was this helpful?