Ajax update product count on cart menu in Woocommerce
I have a menu item that points to the cart and that reads the number of products currently in the cart.
CART (3)
When, on the cart page, I change the number of products and click the "Update cart"
button, or delete an item, the number doesn't refresh.
Any idea why?
/*CHANGE CART MENU TITLE IN MENU*/
add_filter( 'wp_setup_nav_menu_item','my_item_setup' );
function my_item_setup($item) {
if ( ! is_admin() ) {
if ( class_exists( 'woocommerce' ) ) {
global $woocommerce;
if ( $item->url == esc_url( wc_get_cart_url() ) ) {
if (is_null($woocommerce->cart)){
} else {
if( get_locale() == 'fr_FR' ) {
$item->title = 'PANIER ('. '<span class="count-cart">' . $woocommerce->cart->get_cart_contents_count() . '</span>)';
} else {
$item->title = 'MY CART ('. '<span class="count-cart">' . $woocommerce->cart->get_cart_contents_count() . '</span>)';
}
}
}
}
}
return $item;
}
And
add_filter( 'woocommerce_add_to_cart_fragments', 'my_woocommerce_add_to_cart_fragments' );
function my_woocommerce_add_to_cart_fragments( $fragments ) {
// Add our fragment
$fragments['li.menu-item-type-woocommerce-cart'] = my_item_setup( '');
return $fragments;
}
EDIT:
Using Ajaxify the cart items count in Woocommerce answer code, seems to work a little better. The product number updates when I delete an item from the cart page, or when I change the number of items, but it doesn't update when I empty the cart. Moreover, there's a space before and after the product number (see picture with empty cart and "CART ( 1 )"
.
add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_cart_fragments');
function wc_refresh_cart_fragments($fragments){
ob_start();
?>
<span class="count-cart"><?php echo WC()->cart->get_cart_contents_count(); ?></span>
<?php
$fragments['li a span.count-cart'] = ob_get_clean();
return $fragments;
}
php ajax wordpress woocommerce cart
add a comment |
I have a menu item that points to the cart and that reads the number of products currently in the cart.
CART (3)
When, on the cart page, I change the number of products and click the "Update cart"
button, or delete an item, the number doesn't refresh.
Any idea why?
/*CHANGE CART MENU TITLE IN MENU*/
add_filter( 'wp_setup_nav_menu_item','my_item_setup' );
function my_item_setup($item) {
if ( ! is_admin() ) {
if ( class_exists( 'woocommerce' ) ) {
global $woocommerce;
if ( $item->url == esc_url( wc_get_cart_url() ) ) {
if (is_null($woocommerce->cart)){
} else {
if( get_locale() == 'fr_FR' ) {
$item->title = 'PANIER ('. '<span class="count-cart">' . $woocommerce->cart->get_cart_contents_count() . '</span>)';
} else {
$item->title = 'MY CART ('. '<span class="count-cart">' . $woocommerce->cart->get_cart_contents_count() . '</span>)';
}
}
}
}
}
return $item;
}
And
add_filter( 'woocommerce_add_to_cart_fragments', 'my_woocommerce_add_to_cart_fragments' );
function my_woocommerce_add_to_cart_fragments( $fragments ) {
// Add our fragment
$fragments['li.menu-item-type-woocommerce-cart'] = my_item_setup( '');
return $fragments;
}
EDIT:
Using Ajaxify the cart items count in Woocommerce answer code, seems to work a little better. The product number updates when I delete an item from the cart page, or when I change the number of items, but it doesn't update when I empty the cart. Moreover, there's a space before and after the product number (see picture with empty cart and "CART ( 1 )"
.
add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_cart_fragments');
function wc_refresh_cart_fragments($fragments){
ob_start();
?>
<span class="count-cart"><?php echo WC()->cart->get_cart_contents_count(); ?></span>
<?php
$fragments['li a span.count-cart'] = ob_get_clean();
return $fragments;
}
php ajax wordpress woocommerce cart
Are you using AJax to update cart data?
– Gufran Hasan
Nov 13 at 11:55
Does it work Louis?
– LoicTheAztec
Nov 13 at 23:09
I commented your answer below.
– Louis
Nov 13 at 23:21
add a comment |
I have a menu item that points to the cart and that reads the number of products currently in the cart.
CART (3)
When, on the cart page, I change the number of products and click the "Update cart"
button, or delete an item, the number doesn't refresh.
Any idea why?
/*CHANGE CART MENU TITLE IN MENU*/
add_filter( 'wp_setup_nav_menu_item','my_item_setup' );
function my_item_setup($item) {
if ( ! is_admin() ) {
if ( class_exists( 'woocommerce' ) ) {
global $woocommerce;
if ( $item->url == esc_url( wc_get_cart_url() ) ) {
if (is_null($woocommerce->cart)){
} else {
if( get_locale() == 'fr_FR' ) {
$item->title = 'PANIER ('. '<span class="count-cart">' . $woocommerce->cart->get_cart_contents_count() . '</span>)';
} else {
$item->title = 'MY CART ('. '<span class="count-cart">' . $woocommerce->cart->get_cart_contents_count() . '</span>)';
}
}
}
}
}
return $item;
}
And
add_filter( 'woocommerce_add_to_cart_fragments', 'my_woocommerce_add_to_cart_fragments' );
function my_woocommerce_add_to_cart_fragments( $fragments ) {
// Add our fragment
$fragments['li.menu-item-type-woocommerce-cart'] = my_item_setup( '');
return $fragments;
}
EDIT:
Using Ajaxify the cart items count in Woocommerce answer code, seems to work a little better. The product number updates when I delete an item from the cart page, or when I change the number of items, but it doesn't update when I empty the cart. Moreover, there's a space before and after the product number (see picture with empty cart and "CART ( 1 )"
.
add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_cart_fragments');
function wc_refresh_cart_fragments($fragments){
ob_start();
?>
<span class="count-cart"><?php echo WC()->cart->get_cart_contents_count(); ?></span>
<?php
$fragments['li a span.count-cart'] = ob_get_clean();
return $fragments;
}
php ajax wordpress woocommerce cart
I have a menu item that points to the cart and that reads the number of products currently in the cart.
CART (3)
When, on the cart page, I change the number of products and click the "Update cart"
button, or delete an item, the number doesn't refresh.
Any idea why?
/*CHANGE CART MENU TITLE IN MENU*/
add_filter( 'wp_setup_nav_menu_item','my_item_setup' );
function my_item_setup($item) {
if ( ! is_admin() ) {
if ( class_exists( 'woocommerce' ) ) {
global $woocommerce;
if ( $item->url == esc_url( wc_get_cart_url() ) ) {
if (is_null($woocommerce->cart)){
} else {
if( get_locale() == 'fr_FR' ) {
$item->title = 'PANIER ('. '<span class="count-cart">' . $woocommerce->cart->get_cart_contents_count() . '</span>)';
} else {
$item->title = 'MY CART ('. '<span class="count-cart">' . $woocommerce->cart->get_cart_contents_count() . '</span>)';
}
}
}
}
}
return $item;
}
And
add_filter( 'woocommerce_add_to_cart_fragments', 'my_woocommerce_add_to_cart_fragments' );
function my_woocommerce_add_to_cart_fragments( $fragments ) {
// Add our fragment
$fragments['li.menu-item-type-woocommerce-cart'] = my_item_setup( '');
return $fragments;
}
EDIT:
Using Ajaxify the cart items count in Woocommerce answer code, seems to work a little better. The product number updates when I delete an item from the cart page, or when I change the number of items, but it doesn't update when I empty the cart. Moreover, there's a space before and after the product number (see picture with empty cart and "CART ( 1 )"
.
add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_cart_fragments');
function wc_refresh_cart_fragments($fragments){
ob_start();
?>
<span class="count-cart"><?php echo WC()->cart->get_cart_contents_count(); ?></span>
<?php
$fragments['li a span.count-cart'] = ob_get_clean();
return $fragments;
}
php ajax wordpress woocommerce cart
php ajax wordpress woocommerce cart
edited Nov 13 at 13:47
LoicTheAztec
84.2k136095
84.2k136095
asked Nov 13 at 11:49
Louis
1,24783473
1,24783473
Are you using AJax to update cart data?
– Gufran Hasan
Nov 13 at 11:55
Does it work Louis?
– LoicTheAztec
Nov 13 at 23:09
I commented your answer below.
– Louis
Nov 13 at 23:21
add a comment |
Are you using AJax to update cart data?
– Gufran Hasan
Nov 13 at 11:55
Does it work Louis?
– LoicTheAztec
Nov 13 at 23:09
I commented your answer below.
– Louis
Nov 13 at 23:21
Are you using AJax to update cart data?
– Gufran Hasan
Nov 13 at 11:55
Are you using AJax to update cart data?
– Gufran Hasan
Nov 13 at 11:55
Does it work Louis?
– LoicTheAztec
Nov 13 at 23:09
Does it work Louis?
– LoicTheAztec
Nov 13 at 23:09
I commented your answer below.
– Louis
Nov 13 at 23:21
I commented your answer below.
– Louis
Nov 13 at 23:21
add a comment |
2 Answers
2
active
oldest
votes
Updated
It's better to set a tag ID than a tag CLASS to ajaxify a cart count, as this selector reference need to be unique. If it's not the case, and there is a hidden mobile duplicated menu, it can't work.
So this item menu need to be unique on the generated html code of your page… If this menu item is duplicated in a mobile version, you will need to change the tag ID or the tag CLASS for the mobile code version.
I have revisited your code a bit:
add_filter( 'wp_setup_nav_menu_item','my_item_setup' );
function my_item_setup( $item ) {
if ( ! is_admin() && class_exists( 'woocommerce' ) ) {
if ( $item->url == esc_url( wc_get_cart_url() ) && ! WC()->cart->is_empty() ){
$title = get_locale() == 'fr_FR' ? 'PANIER' : 'MY CART';
$item->title = $title . ' (<span id="count-cart-items">' . WC()->cart->get_cart_contents_count() . '</span>)';
}
}
return $item;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_cart_fragments', 50, 1 );
function wc_refresh_cart_fragments( $fragments ){
$cart_count = WC()->cart->get_cart_contents_count();
// Normal version
$count_normal = '<span id="count-cart-items">' . $cart_count . '</span>';
$fragments['#count-cart-items'] = $count_normal;
// Mobile version
$count_mobile = '<span id="count-cart-itemob">' . $cart_count . '</span>';
$fragments['#count-cart-itemob'] = $count_mobile;
return $fragments;
}
Code goes in function.php file of your active child theme (active theme). It should better work.
Hey ! Thanks. Way cleaner code. The only problem is that I have three occurrences of the cart linkCART (2)
. One in the desktop menu, one in the mobile menu and one in the mobile header. I just tried using a class everywhere, I see no bugs. But if you recommend using an id, how can I differentiate the code for the mobile menu and the code for the desktop menu, so I can pu two different ids? Because for now my function my_item_setup is replacing the item in both mobile and desktop menus. And with this code, emptying the cart still keeps a number in the fragments.
– Louis
Nov 13 at 23:17
1
@Louis "C'est compliqué ça dis donc!"… I think that you need to add a different Id for each and a fragment for each ID like in my updated answer (but it's difficult to guess for me without the generated html code). If needed you can contact me on skype chat searching for Loïc de Marcé (Paris).
– LoicTheAztec
Nov 13 at 23:34
mmh ok I see. I am going to get back on this tomorrow ! I'll let your know. Thanks a lot Loïc ;)
– Louis
Nov 13 at 23:45
add a comment |
To Ajaxify your cart viewer so it updates when an item is added or deleted (via ajax) use:
/**
* Show cart contents / total Ajax
*/
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<a class="cart-customlocation" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cart-customlocation'] = ob_get_clean();
return $fragments;
} ?>
Reference Link
Thanks for answering, it's been recommended to use WC() instead of $woocommerce for a few times
– Louis
Nov 13 at 23:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280425%2fajax-update-product-count-on-cart-menu-in-woocommerce%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Updated
It's better to set a tag ID than a tag CLASS to ajaxify a cart count, as this selector reference need to be unique. If it's not the case, and there is a hidden mobile duplicated menu, it can't work.
So this item menu need to be unique on the generated html code of your page… If this menu item is duplicated in a mobile version, you will need to change the tag ID or the tag CLASS for the mobile code version.
I have revisited your code a bit:
add_filter( 'wp_setup_nav_menu_item','my_item_setup' );
function my_item_setup( $item ) {
if ( ! is_admin() && class_exists( 'woocommerce' ) ) {
if ( $item->url == esc_url( wc_get_cart_url() ) && ! WC()->cart->is_empty() ){
$title = get_locale() == 'fr_FR' ? 'PANIER' : 'MY CART';
$item->title = $title . ' (<span id="count-cart-items">' . WC()->cart->get_cart_contents_count() . '</span>)';
}
}
return $item;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_cart_fragments', 50, 1 );
function wc_refresh_cart_fragments( $fragments ){
$cart_count = WC()->cart->get_cart_contents_count();
// Normal version
$count_normal = '<span id="count-cart-items">' . $cart_count . '</span>';
$fragments['#count-cart-items'] = $count_normal;
// Mobile version
$count_mobile = '<span id="count-cart-itemob">' . $cart_count . '</span>';
$fragments['#count-cart-itemob'] = $count_mobile;
return $fragments;
}
Code goes in function.php file of your active child theme (active theme). It should better work.
Hey ! Thanks. Way cleaner code. The only problem is that I have three occurrences of the cart linkCART (2)
. One in the desktop menu, one in the mobile menu and one in the mobile header. I just tried using a class everywhere, I see no bugs. But if you recommend using an id, how can I differentiate the code for the mobile menu and the code for the desktop menu, so I can pu two different ids? Because for now my function my_item_setup is replacing the item in both mobile and desktop menus. And with this code, emptying the cart still keeps a number in the fragments.
– Louis
Nov 13 at 23:17
1
@Louis "C'est compliqué ça dis donc!"… I think that you need to add a different Id for each and a fragment for each ID like in my updated answer (but it's difficult to guess for me without the generated html code). If needed you can contact me on skype chat searching for Loïc de Marcé (Paris).
– LoicTheAztec
Nov 13 at 23:34
mmh ok I see. I am going to get back on this tomorrow ! I'll let your know. Thanks a lot Loïc ;)
– Louis
Nov 13 at 23:45
add a comment |
Updated
It's better to set a tag ID than a tag CLASS to ajaxify a cart count, as this selector reference need to be unique. If it's not the case, and there is a hidden mobile duplicated menu, it can't work.
So this item menu need to be unique on the generated html code of your page… If this menu item is duplicated in a mobile version, you will need to change the tag ID or the tag CLASS for the mobile code version.
I have revisited your code a bit:
add_filter( 'wp_setup_nav_menu_item','my_item_setup' );
function my_item_setup( $item ) {
if ( ! is_admin() && class_exists( 'woocommerce' ) ) {
if ( $item->url == esc_url( wc_get_cart_url() ) && ! WC()->cart->is_empty() ){
$title = get_locale() == 'fr_FR' ? 'PANIER' : 'MY CART';
$item->title = $title . ' (<span id="count-cart-items">' . WC()->cart->get_cart_contents_count() . '</span>)';
}
}
return $item;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_cart_fragments', 50, 1 );
function wc_refresh_cart_fragments( $fragments ){
$cart_count = WC()->cart->get_cart_contents_count();
// Normal version
$count_normal = '<span id="count-cart-items">' . $cart_count . '</span>';
$fragments['#count-cart-items'] = $count_normal;
// Mobile version
$count_mobile = '<span id="count-cart-itemob">' . $cart_count . '</span>';
$fragments['#count-cart-itemob'] = $count_mobile;
return $fragments;
}
Code goes in function.php file of your active child theme (active theme). It should better work.
Hey ! Thanks. Way cleaner code. The only problem is that I have three occurrences of the cart linkCART (2)
. One in the desktop menu, one in the mobile menu and one in the mobile header. I just tried using a class everywhere, I see no bugs. But if you recommend using an id, how can I differentiate the code for the mobile menu and the code for the desktop menu, so I can pu two different ids? Because for now my function my_item_setup is replacing the item in both mobile and desktop menus. And with this code, emptying the cart still keeps a number in the fragments.
– Louis
Nov 13 at 23:17
1
@Louis "C'est compliqué ça dis donc!"… I think that you need to add a different Id for each and a fragment for each ID like in my updated answer (but it's difficult to guess for me without the generated html code). If needed you can contact me on skype chat searching for Loïc de Marcé (Paris).
– LoicTheAztec
Nov 13 at 23:34
mmh ok I see. I am going to get back on this tomorrow ! I'll let your know. Thanks a lot Loïc ;)
– Louis
Nov 13 at 23:45
add a comment |
Updated
It's better to set a tag ID than a tag CLASS to ajaxify a cart count, as this selector reference need to be unique. If it's not the case, and there is a hidden mobile duplicated menu, it can't work.
So this item menu need to be unique on the generated html code of your page… If this menu item is duplicated in a mobile version, you will need to change the tag ID or the tag CLASS for the mobile code version.
I have revisited your code a bit:
add_filter( 'wp_setup_nav_menu_item','my_item_setup' );
function my_item_setup( $item ) {
if ( ! is_admin() && class_exists( 'woocommerce' ) ) {
if ( $item->url == esc_url( wc_get_cart_url() ) && ! WC()->cart->is_empty() ){
$title = get_locale() == 'fr_FR' ? 'PANIER' : 'MY CART';
$item->title = $title . ' (<span id="count-cart-items">' . WC()->cart->get_cart_contents_count() . '</span>)';
}
}
return $item;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_cart_fragments', 50, 1 );
function wc_refresh_cart_fragments( $fragments ){
$cart_count = WC()->cart->get_cart_contents_count();
// Normal version
$count_normal = '<span id="count-cart-items">' . $cart_count . '</span>';
$fragments['#count-cart-items'] = $count_normal;
// Mobile version
$count_mobile = '<span id="count-cart-itemob">' . $cart_count . '</span>';
$fragments['#count-cart-itemob'] = $count_mobile;
return $fragments;
}
Code goes in function.php file of your active child theme (active theme). It should better work.
Updated
It's better to set a tag ID than a tag CLASS to ajaxify a cart count, as this selector reference need to be unique. If it's not the case, and there is a hidden mobile duplicated menu, it can't work.
So this item menu need to be unique on the generated html code of your page… If this menu item is duplicated in a mobile version, you will need to change the tag ID or the tag CLASS for the mobile code version.
I have revisited your code a bit:
add_filter( 'wp_setup_nav_menu_item','my_item_setup' );
function my_item_setup( $item ) {
if ( ! is_admin() && class_exists( 'woocommerce' ) ) {
if ( $item->url == esc_url( wc_get_cart_url() ) && ! WC()->cart->is_empty() ){
$title = get_locale() == 'fr_FR' ? 'PANIER' : 'MY CART';
$item->title = $title . ' (<span id="count-cart-items">' . WC()->cart->get_cart_contents_count() . '</span>)';
}
}
return $item;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_cart_fragments', 50, 1 );
function wc_refresh_cart_fragments( $fragments ){
$cart_count = WC()->cart->get_cart_contents_count();
// Normal version
$count_normal = '<span id="count-cart-items">' . $cart_count . '</span>';
$fragments['#count-cart-items'] = $count_normal;
// Mobile version
$count_mobile = '<span id="count-cart-itemob">' . $cart_count . '</span>';
$fragments['#count-cart-itemob'] = $count_mobile;
return $fragments;
}
Code goes in function.php file of your active child theme (active theme). It should better work.
edited Nov 13 at 23:56
answered Nov 13 at 13:45
LoicTheAztec
84.2k136095
84.2k136095
Hey ! Thanks. Way cleaner code. The only problem is that I have three occurrences of the cart linkCART (2)
. One in the desktop menu, one in the mobile menu and one in the mobile header. I just tried using a class everywhere, I see no bugs. But if you recommend using an id, how can I differentiate the code for the mobile menu and the code for the desktop menu, so I can pu two different ids? Because for now my function my_item_setup is replacing the item in both mobile and desktop menus. And with this code, emptying the cart still keeps a number in the fragments.
– Louis
Nov 13 at 23:17
1
@Louis "C'est compliqué ça dis donc!"… I think that you need to add a different Id for each and a fragment for each ID like in my updated answer (but it's difficult to guess for me without the generated html code). If needed you can contact me on skype chat searching for Loïc de Marcé (Paris).
– LoicTheAztec
Nov 13 at 23:34
mmh ok I see. I am going to get back on this tomorrow ! I'll let your know. Thanks a lot Loïc ;)
– Louis
Nov 13 at 23:45
add a comment |
Hey ! Thanks. Way cleaner code. The only problem is that I have three occurrences of the cart linkCART (2)
. One in the desktop menu, one in the mobile menu and one in the mobile header. I just tried using a class everywhere, I see no bugs. But if you recommend using an id, how can I differentiate the code for the mobile menu and the code for the desktop menu, so I can pu two different ids? Because for now my function my_item_setup is replacing the item in both mobile and desktop menus. And with this code, emptying the cart still keeps a number in the fragments.
– Louis
Nov 13 at 23:17
1
@Louis "C'est compliqué ça dis donc!"… I think that you need to add a different Id for each and a fragment for each ID like in my updated answer (but it's difficult to guess for me without the generated html code). If needed you can contact me on skype chat searching for Loïc de Marcé (Paris).
– LoicTheAztec
Nov 13 at 23:34
mmh ok I see. I am going to get back on this tomorrow ! I'll let your know. Thanks a lot Loïc ;)
– Louis
Nov 13 at 23:45
Hey ! Thanks. Way cleaner code. The only problem is that I have three occurrences of the cart link
CART (2)
. One in the desktop menu, one in the mobile menu and one in the mobile header. I just tried using a class everywhere, I see no bugs. But if you recommend using an id, how can I differentiate the code for the mobile menu and the code for the desktop menu, so I can pu two different ids? Because for now my function my_item_setup is replacing the item in both mobile and desktop menus. And with this code, emptying the cart still keeps a number in the fragments.– Louis
Nov 13 at 23:17
Hey ! Thanks. Way cleaner code. The only problem is that I have three occurrences of the cart link
CART (2)
. One in the desktop menu, one in the mobile menu and one in the mobile header. I just tried using a class everywhere, I see no bugs. But if you recommend using an id, how can I differentiate the code for the mobile menu and the code for the desktop menu, so I can pu two different ids? Because for now my function my_item_setup is replacing the item in both mobile and desktop menus. And with this code, emptying the cart still keeps a number in the fragments.– Louis
Nov 13 at 23:17
1
1
@Louis "C'est compliqué ça dis donc!"… I think that you need to add a different Id for each and a fragment for each ID like in my updated answer (but it's difficult to guess for me without the generated html code). If needed you can contact me on skype chat searching for Loïc de Marcé (Paris).
– LoicTheAztec
Nov 13 at 23:34
@Louis "C'est compliqué ça dis donc!"… I think that you need to add a different Id for each and a fragment for each ID like in my updated answer (but it's difficult to guess for me without the generated html code). If needed you can contact me on skype chat searching for Loïc de Marcé (Paris).
– LoicTheAztec
Nov 13 at 23:34
mmh ok I see. I am going to get back on this tomorrow ! I'll let your know. Thanks a lot Loïc ;)
– Louis
Nov 13 at 23:45
mmh ok I see. I am going to get back on this tomorrow ! I'll let your know. Thanks a lot Loïc ;)
– Louis
Nov 13 at 23:45
add a comment |
To Ajaxify your cart viewer so it updates when an item is added or deleted (via ajax) use:
/**
* Show cart contents / total Ajax
*/
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<a class="cart-customlocation" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cart-customlocation'] = ob_get_clean();
return $fragments;
} ?>
Reference Link
Thanks for answering, it's been recommended to use WC() instead of $woocommerce for a few times
– Louis
Nov 13 at 23:21
add a comment |
To Ajaxify your cart viewer so it updates when an item is added or deleted (via ajax) use:
/**
* Show cart contents / total Ajax
*/
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<a class="cart-customlocation" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cart-customlocation'] = ob_get_clean();
return $fragments;
} ?>
Reference Link
Thanks for answering, it's been recommended to use WC() instead of $woocommerce for a few times
– Louis
Nov 13 at 23:21
add a comment |
To Ajaxify your cart viewer so it updates when an item is added or deleted (via ajax) use:
/**
* Show cart contents / total Ajax
*/
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<a class="cart-customlocation" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cart-customlocation'] = ob_get_clean();
return $fragments;
} ?>
Reference Link
To Ajaxify your cart viewer so it updates when an item is added or deleted (via ajax) use:
/**
* Show cart contents / total Ajax
*/
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<a class="cart-customlocation" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cart-customlocation'] = ob_get_clean();
return $fragments;
} ?>
Reference Link
answered Nov 13 at 11:56
Gufran Hasan
3,43441326
3,43441326
Thanks for answering, it's been recommended to use WC() instead of $woocommerce for a few times
– Louis
Nov 13 at 23:21
add a comment |
Thanks for answering, it's been recommended to use WC() instead of $woocommerce for a few times
– Louis
Nov 13 at 23:21
Thanks for answering, it's been recommended to use WC() instead of $woocommerce for a few times
– Louis
Nov 13 at 23:21
Thanks for answering, it's been recommended to use WC() instead of $woocommerce for a few times
– Louis
Nov 13 at 23:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280425%2fajax-update-product-count-on-cart-menu-in-woocommerce%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you using AJax to update cart data?
– Gufran Hasan
Nov 13 at 11:55
Does it work Louis?
– LoicTheAztec
Nov 13 at 23:09
I commented your answer below.
– Louis
Nov 13 at 23:21