How to redirect straight to Cart/Checkout on “Add to cart” (single-item cart) an

  • Posts: 4
  • Thank you received: 0
  • Hikashop Business
2 days 19 hours ago #368286

-- HikaShop version -- : 6.0.0
-- Joomla version -- : 5.3.3
-- PHP version -- : 8.2.28

Hi HikaShop team,

Goal
On my site each cart must contain only one product and one unit. When a user clicks “Add to cart” from a product listing or product page, I want to:

Immediately redirect to the Cart/Checkout page (skip the add-to-cart popup/notification), and

If there is already a product in the cart, replace it with the newly selected product (so the cart always keeps just that single item).

Ideally, I’d like to do this without the HikaShop Cart Notification plugin (no popup).

Environment

Joomla: 5.x

HikaShop: 6.0.x (Business)

PHP: 8.x

Template: Helix Ultimate (if relevant)

What I’ve tried

I found discussions about the Cart Notification plugin, but my goal is to redirect directly to the cart/checkout instead of showing a popup.

I can enforce a single product by clearing the cart before adding the new one via an override/custom code (e.g., customizing add_to_cart_ajax.php), but I’d prefer an official setting/hook or a recommended approach that survives updates.

Looked for a config like “After add to cart → go to checkout/cart” but I’m not sure if there is a built-in option in HikaShop 6.0.x to force the redirect site-wide.

Questions

Is there a built-in setting to make “Add to cart” redirect directly to the cart/checkout (no popup/notice), both from product listings and product pages? If yes, where exactly is it in HikaShop 6.0.x?

What’s the recommended way to ensure the cart always contains only one item (qty=1) and to replace any existing item when adding a new one?

Is there a core event/plugin hook (e.g., onBeforeCartSave, onBeforeAddToCart, etc.) we should use to empty the cart before adding the new product?

Or a configuration that enforces single-item carts globally?

If there’s no built-in redirect, is there a supported pattern (override or system plugin) to:

Clear the cart, add the selected product, then redirect to the cart/checkout URL,

And keep this behavior working for both AJAX and non-AJAX add-to-cart?

Desired UX (summary)

User clicks “Add to cart” → existing cart (if any) is cleared → new product (qty=1) is added → user is redirected straight to the cart/checkout.

If you can share the proper settings path, the recommended event hooks, or a short example for HikaShop 6.0.x, that would be perfect. Thanks a lot!

Best,
David

Please Log in or Create an account to join the conversation.

  • Posts: 84239
  • Thank you received: 13685
  • MODERATOR
2 days 17 hours ago #368287

Hi,

1. If you disable the HikaShop cart notification plugin, AND you don't have the HikaShop cart module displayed on the page, the system will automatically redirect to the checkout after an add to cart.
Alternatively, you can also configure the settings of the HikaShop cart notification plugin to redirect to the checkout on success. This approach is recommended to better handle error messages when a problem happens during the add to cart process (for example, if the product can't be added because of a limit rule).

2. In the Products>Limits menu, you can configure limits on purchases. So, for example, you can configure a limit rule to restrict the cart to a quantity of 1. When you do so, if a user tries to add a product to his cart and he already has a product in his cart, the add to cart will be refused and he will see an error message explaining that he can only have 1 product in his cart.

3. Now, what I said in point 2 is not exactly what you want.
To get that, you'll have to develop a small plugin. I would recommend implementing the event onBeforeCartLoad(&$cachedCart, &$options)
In it:
- Check the URL of the current page. If it's not an add to cart request, skip.
- Then, also use a static variable so that you only do your process once ( because after the product is add to the cart in the database, the cart in memory is cleared and reloaded, and you don't want to delete the product from it at that point ).
- Then, get the cart id from HikaShop:

$cartClass = hikashop_get('class.cart');
$id = $cartClass->getCurrentCartId();
If the id returned is empty, skip.
- Finally, you can run a MySQL query on the hikashop_cart_product table to delete the entries where the cart_id is equal to what you have in $id. It is important to directly run a MySQL query with Joomla's functions, and not use the save process of class.cart to avoid a potential loop of the code.

That way, the add to cart process will go like that:
- the cart controller get the request
- the method addProduct of class.cart is called
- the current cart starts loading. If no cart, it is created in the database in the hikashop_cart table
- onBeforeCartLoad is called
- your plugin delete existing products already in the cart
- the cart loading process continues the loading of the cart by loading the products of the current cart if any
- the product being added, is actually added to the cart in the database

Your plugin should only have a dozen lines of code, so, if you're a developer, it should not be a problem. And in that case, you don't want to use the limits system in HikaShop.

Last edit: 2 days 17 hours ago by nicolas.
The following user(s) said Thank You: UPL

Please Log in or Create an account to join the conversation.

Time to create page: 0.054 seconds
Powered by Kunena Forum