Solved

Redirect to cart web page when item added to cart - Express theme

OwlzzzSleep
Visitor
3 0 3

In the Express theme, when someone clicks the “Add to Cart” button, I want to redirect them to the Cart web page so they can see what they added. I have seen this solution for other themes, but not the Express theme.

I’m guessing I need to edit the theme.js file’s "_addItemToCart()” function, but I don’t know exactly what code to change or where.

Accepted Solution (1)

Bunty
Shopify Partner
133 39 82

This is an accepted solution.

Ideally, you should invoke the method to open the cart drawer, I have a suggestion to do that, but it requires a few changes so might be tricky to communicate, and I'll provide the next best suggestion, to redirect to cart. You have rightly identified the method in the theme.js file but remember that the theme uses theme.min.js , exactly how theme.js is minified, I'm not sure but you could test this by updating the method on the minified file as follows

_addItemToCart() {
            this._hideErrorMessage(),
            this._toggleSuccessMessage(!0),
            S(this.elements.productForm).then(t=>{
                this._toggleSuccessMessage(!1, this._getSuccessMessage()),
                this._showInstantQuantity(t),
                window.carts.length || this._onCartUpdated(),
                this.isUpdatingCart = !1,
                document.dispatchEvent(new CustomEvent("productAddedToCart")),
                window.carts.forEach(t=>{
                    t.onCartUpdated(this.productId)
                }
                ),
                theme.cartQuantity.updateQuantityInputElements(t.key, t.quantity);
                const e = this._getQuantityUpdatedText(t.quantity, !0);
                this._updateLiveRegion(e)
            }
            ).catch(t=>{
                this._handleProductError(t),
                this.isUpdatingCart = !1
            }
            ).finally(t=>{
                window.location.href = '/cart'
            })
        }

 

The function has an additional

.finally(t=>{
                window.location.href = '/cart'
            })

to achieve the outcome

 

Hope this helps

 

View solution in original post

Replies 2 (2)

Bunty
Shopify Partner
133 39 82

This is an accepted solution.

Ideally, you should invoke the method to open the cart drawer, I have a suggestion to do that, but it requires a few changes so might be tricky to communicate, and I'll provide the next best suggestion, to redirect to cart. You have rightly identified the method in the theme.js file but remember that the theme uses theme.min.js , exactly how theme.js is minified, I'm not sure but you could test this by updating the method on the minified file as follows

_addItemToCart() {
            this._hideErrorMessage(),
            this._toggleSuccessMessage(!0),
            S(this.elements.productForm).then(t=>{
                this._toggleSuccessMessage(!1, this._getSuccessMessage()),
                this._showInstantQuantity(t),
                window.carts.length || this._onCartUpdated(),
                this.isUpdatingCart = !1,
                document.dispatchEvent(new CustomEvent("productAddedToCart")),
                window.carts.forEach(t=>{
                    t.onCartUpdated(this.productId)
                }
                ),
                theme.cartQuantity.updateQuantityInputElements(t.key, t.quantity);
                const e = this._getQuantityUpdatedText(t.quantity, !0);
                this._updateLiveRegion(e)
            }
            ).catch(t=>{
                this._handleProductError(t),
                this.isUpdatingCart = !1
            }
            ).finally(t=>{
                window.location.href = '/cart'
            })
        }

 

The function has an additional

.finally(t=>{
                window.location.href = '/cart'
            })

to achieve the outcome

 

Hope this helps

 

OwlzzzSleep
Visitor
3 0 3

Thanks! Adding that extra function worked perfectly! The Add to Cart button now adds item to cart and opens my cart web page (I don’t use the cart drawer).

I’ve never received such a quick and perfect solution in a first forum reply. Hats off to you, Bunty!