-
Hello! How can I make this happen as well when I remove products from the minicart ? Also I have made a filter for buttons , when they click on 'add to cart' , it adds to cart and then appears the "View cart" button. So in my case , if I delete that product from minicart , he is not longer in my cart so I have to recall the fragments? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi Robert, yes, cart and mini-cart do not update each other. This is the default WooCommerce cart and in the Offcanvas it is the default mini-cart widget. We have added our own AJAX functionality only to single-product pages. So, yes you have to recall the fragments. But before spending too much time on this, please note that cart and checkout will be replaced by WooCommerce in November. This will change everything https://developer.woocommerce.com/2023/08/18/cart-and-checkout-blocks-becoming-the-default-experience/. |
Beta Was this translation helpful? Give feedback.
-
I have to make this functionality because its mandatory for user experience in my website , so I figured out how. class WoocommerceAjaxFragments
{
public function __construct()
{
$this->initFragments();
}
public function initFragments()
{
// this filter adds extra fragments to the main core ajax handler
add_filter('woocommerce_add_to_cart_fragments', [$this, 'shopContentFragments']);
}
} And here is a method that will refresh everything with small code function emptyCart()
{
$slug = $_POST['slug'] ?? '';
$taxQuery = [];
WC()->cart->empty_cart();
WC_AJAX::get_refreshed_fragments();
die();
}
add_action('wp_ajax_emptyCart', 'emptyCart');
add_action('wp_ajax_nopriv_emptyCart', 'emptyCart'); success: function (response) {
console.log(response);
// unblock cart
if (cart.length) {
cart.unblock();
}
// refresh minicart fragments
if (response && response.hasOwnProperty('fragments')) {
// refresh cart
if (response.fragments['div.widget_shopping_cart_content']) {
if (cart.length) {
cart.html(response.fragments['div.widget_shopping_cart_content']);
}
}
// refresh shop
if (response.fragments['shop']) {
if (shopContent.length) {
shopContent.html(response.fragments['shop']);
}
}
// refresh badge total
if (response.fragments['span.cart-content2']) {
if (badgeCart.length) {
badgeCart.html(response.fragments['span.cart-content2']);
}
}
}
}, Hope it will helps someone that needs to take control over woocommerce ajax functionalities. Thank you. |
Beta Was this translation helpful? Give feedback.
I have to make this functionality because its mandatory for user experience in my website , so I figured out how.
Basically , like you did for notices , I added custom fragments with custom code that needs refreshed , and I just make an ajax call and call this function that stores all my updated fragments and render them back with javascript:
And here is a method that…