AJAX Add to Cart
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
The AJAX Add to Cart module lets you add products to the cart from the single product page without reloading it. WooCommerce already adds simple products by AJAX on shop and category pages; this module covers the product page form, including variable products.
Features
Section titled “Features”- Simple and variable products on single product pages
- Toast notification with a link to the cart
- Automatic cart fragment (mini-cart) updates
- Button animation (loading -> added)
- WooCommerce validation and stock errors shown in the toast instead of after the next page load
Enabling
Section titled “Enabling”Go to Polski > Modules and enable the AJAX Add to Cart module in the “Stock & Cart” section. The module is off by default and has no settings of its own.
Where it works
Section titled “Where it works”The script is loaded on the shop page, product category and tag archives and single product pages. It takes over the form.cart submit only when the page body has the single-product class, so in practice on the single product page.
The normal (reloading) submit is kept when:
- the product is grouped (
grouped_form), because the handler adds one product ID per request, - the product is external (the form uses
method="get"and links away), - the add to cart button has the
disabledclass, for example before a variation is chosen, - the form is shown on another page, such as a product shortcode placed on a regular page.
On shop and category pages the WooCommerce AJAX buttons work as before; the module does not change them.
How it works
Section titled “How it works”- The customer clicks “Add to cart” on the product page
- The form is sent to
admin-ajax.phpwith the actionpolski_ajax_add_to_cartand a nonce, without reloading - The button gets the
loadingclass and is disabled while the request runs - The server reads the product ID, the variation ID, the quantity (at least 1) and the
attribute_*fields of a variation - It runs the
woocommerce_add_to_cart_validationfilter and adds the item withWC()->cart->add_to_cart(), so stock checks and other plugins’ rules apply - After a successful add it fires
woocommerce_ajax_added_to_cartand returns the refreshed cart fragments - A green “Added to cart!” notification appears with a “View cart” link and the mini-cart in the header updates
If validation or adding fails, the first WooCommerce error notice (for example a stock message) is shown in a red toast. Without a notice the toast says “Validation failed.” (validation) or “Could not add to cart.” (the add itself). “Could not add to cart. Please try again.” appears when the request fails outright, for example an expired nonce.
JavaScript events
Section titled “JavaScript events”| Event | When | Arguments |
|---|---|---|
polski_adding_to_cart |
Before the AJAX request is sent | $button, formData |
polski_added_to_cart |
After a successful add | $button, response |
added_to_cart |
The standard WooCommerce event, after a successful add | fragments, cart_hash, $button |
All events are triggered on document.body:
jQuery( document.body ).on( 'polski_added_to_cart', function ( e, $button, response ) { // e.g. open a mini-cart drawer} );PHP hooks
Section titled “PHP hooks”The module registers no filters of its own. It calls the standard WooCommerce hooks, so code hooked to them runs for AJAX adds too:
| Hook | Type | Arguments |
|---|---|---|
woocommerce_add_to_cart_validation |
filter | $passed, $product_id, $quantity, $variation_id, $variation |
woocommerce_ajax_added_to_cart |
action | $product_id |
Notification style
Section titled “Notification style”The toast notification appears in the top right corner of the screen (fixed, 32 px from the top) and disappears after 4 seconds. Success toasts are green, errors red (class .polski-ajax-cart-notice.error). You can customize it through the CSS class .polski-ajax-cart-notice. Success is announced to screen readers politely (role="status"), errors assertively (role="alert").
Troubleshooting
Section titled “Troubleshooting”- Every add fails with “Could not add to cart. Please try again.” The request nonce is printed into the page. If a full-page cache serves the product page for longer than the WordPress nonce lifetime (up to 24 hours), the nonce check fails. Shorten the cache lifetime for product pages or exclude them.
- The page still reloads. Check that the product is not grouped or external and that the theme keeps the
single-productbody class and thecartclass on the form.