I’ve assembled the link to add a variable product to my cart like so but I’m at a loss on how to then “refresh” the cart without reloading the page. My guess is that I’m not properly making this AJAX add to cart request and therefore, the woocommerce_add_to_cart_fragments (where I believe my cart HTML would be placed for refresh) isn’t being called.
$addToCartLink = '?add-to-cart=' . $prod->id . '&variation_id=' . $var_id . '&attribute_pa_quantity-attribute=' . $var_quantity;
jQuery.get($addToCartLink, function() {
//refreshCart();
});
If anyone can just point me in the right direction I’d be greatly appreciative. Variable/AJAX/WooCommerce docs seems to be rather sparse.
@helgatheviking – Thanks, I found that a few hours after I posted. This also provided a mega assist https://github.com/wp-plugins/woocommerce-ajax-add-to-cart-for-variable-products/blob/master/js/add-to-cart-variation.js.
Here was my finished solution in case it helps anyone.
I’m storing my var_id and product_id as data attributes and quantity and the var_name are hardcoded in this instance. .doodleVarButton is the class I apply to all my variable item add to cart links.
The JS
The extended WC action (threw in functions.php)
Then – the key – WC_AJAX::get_refreshed_fragments calls the crucial woocommerce_header_add_to_cart_fragment function where you update your cart. My cart is modularized in a separate PHP file.
Just make sure ‘xxx’ in $fragments[xxx] matches the container for your cart code.