Get total wishlist via ajax
-
Hi there .. I have a problem on how can I update my wishlist count on the header but dont need to reload. I want it to show by doing ajax. normally, it shows all the total count after load of the page but my client want no loading the page.
https://www.ads-software.com/plugins/yith-woocommerce-wishlist/
-
Hi Anthony!
It will require some code to obtain your goal;
First of all create a file yith-wcwl-custom.js in your theme root; add this code to the file created
jQuery( document ).ready( function($){ var update_wishlist_count = function() { $.ajax({ beforeSend: function () { }, complete : function () { }, data : { action: 'update_wishlist_count' }, success : function (data) { console.log( data ); //do something }, url: yith_wcwl_l10n.ajax_url }); }; $('body').on( 'added_to_wishlist removed_from_wishlist', update_wishlist_count ); } );
Then append this code at the end of functions.php file of your theme
function update_wishlist_count(){ if( function_exists( 'YITH_WCWL' ) ){ wp_send_json( YITH_WCWL()->count_products() ); } } add_action( 'wp_ajax_update_wishlist_count', 'update_wishlist_count' ); add_action( 'wp_ajax_nopriv_update_wishlist_count', 'update_wishlist_count' ); function enqueue_custom_wishlist_js(){ wp_enqueue_script( 'yith-wcwl-custom-js', get_stylesheet_directory_uri() . '/yith-wcwl-custom.js', array( 'jquery' ), false, true ); } add_action( 'wp_enqueue_scripts', 'enqueue_custom_wishlist_js' );
Now when adding or removing an item from the wishlist, an ajax call should be sent, and the number of product in wishlist will be returned in data param
Now customize the js code to update your count ??
Please, note that removed_from_wishlist action will be added with 2.0.7 version of the plugin, that will be released soon
Let me know if this helps
Have a nice day ??Thank you sir YITHEMES .. this is working perfectly ..
Hi again Anthony!
Glad to help! Marking this as resolvedHave a nice day ??
Hello fellow Anthony, Do you mind sharing what you have so others may use the same example you were successful with. I’m trying to do a similar thing. I’m trying to add a simple heart icon in the fix header of the theme I’m using this plugin with, and trying to get it so that when a user add’s something to their wishlist, the number automatically gets indicated in the wishlist icon.
Thanks for helping us in sharing our plugin. If you have appreciated our help, please don’t forget to give us a 5-star vote! ??
AVOID Anthony Carbon site links.
They’re full of redirects to scam sites.Thanks for the tip!
I’ve tried the ajax function above but it seems to break the plugin. Chrome developer tools returns an error with a blank index.php file, breaking the woocommerce home page. If I remove the function my woocommerce is perfectly loaded.
I’ve tried this function after my theme’s custom function to fetch the wishlist count also stopped working. I just can’t see why it is so hard to achieve this functionality.
Works like a charm!
Thank you very much (:Thank to all.
Have a nice day ??
Get total compare via ajax
Hi to all..!!
Same as Whishlist, I wanted to update compare count in header but don’t want to reload the page. I wanted it to show by doing AJAX.
I am using YITH Plugin for compare and whishlist.Please help ASAP..!!
@adriano: Ciao ??
Can you please enable debug on your site, and tell me if some error appear?
@ktleedham: please, write in the correct forum; we are a team of many developers, and I’m not the right person to ask about compare. Sorry ??
@yiththemes, @ktleedham – Thanks for the feedback! After enabling debug it turned out to be a database error, here’s the error output:
WordPress database error: [Table ‘mrancho_db.wp_yith_wcwl_lists’ doesn’t exist]
SELECT l.* FROMwp_yith_wcwl_lists
AS l WHERE 1 AND l.user_id
= 2 AND l.is_default
= 1 ORDER BY ID DESC
WordPress database error: [Table ‘mrancho_db.wp_yith_wcwl_lists’ doesn’t exist]
SELECT l.* FROMwp_yith_wcwl_lists
AS l WHERE 1 AND l.user_id
= 2 AND l.is_default
= 1 ORDER BY ID DESC
WordPress database error: [Table ‘mrancho_db.wp_yith_wcwl_lists’ doesn’t exist]
SELECT COUNT(*) FROMwp_yith_wcwl_lists
WHEREwishlist_token
= ‘NMJVAFPHCNM3’
WordPress database error: [Table ‘mrancho_db.wp_yith_wcwl_lists’ doesn’t exist]
SHOW FULL COLUMNS FROMwp_yith_wcwl_lists
WordPress database error: [Table ‘mrancho_db.wp_yith_wcwl_lists’ doesn’t exist]
SELECT l.* FROMwp_yith_wcwl_lists
AS l WHERE 1 AND l.user_id
= 2 AND l.is_default
= 1 ORDER BY ID DESCI have no idea why I’m getting such a DB error, I’ve updated WordPress to its latest version (4.4.2) as also the plugin itself (2.0.13)… Although it seems the error points out to the non existence of the yith plugin’s tables within the DB, those tables actually do exist, so, not sure what to do from here.
I’d really appreciate any input regarding this issue.
Sorry, but I didn’t get what you mean about the correct forum.
Regards,
Adriano.this is a really strange issue
The best solution I can imagine, is to force WP to regenerate WishList DB structureAnyway, this will require to delete all current contents of wishlist, and some hardcoding
If you don’t bother to proceed this way, here’s the step to follow:
1. Disable wishlist plugin
2. visit wp-admin/options.php in your site backend and search for yith_wcwl_db_version option; delete the content of the option, and save
3. Delete wcwl* tables from your DB (and yes, you may want to make a complete backup before to click the “I’m sure” button ?? )
4. Activate again the pluginLet me know if this helps
Does this snippet works on premium version? I have tried it without success.
- The topic ‘Get total wishlist via ajax’ is closed to new replies.