Ajaxify header cart items count in Woocommerce

前端 未结 3 390
臣服心动
臣服心动 2020-12-03 16:11

I\'m creating a custom woocommerce integrated theme for wordpress.

I have a blob on the top that displays the total number of items in the cart, I want to update thi

3条回答
  •  遥遥无期
    2020-12-03 16:59

    For anyone who wants the proper ajax implementation, here is the way to go.

    in functions.php

    add_action('wp_ajax_cart_count_retriever', 'cart_count_retriever');
    add_action('wp_ajax_nopriv_cart_count_retriever', 'cart_count_retriever');
    function cart_count_retriever() {
        global $wpdb;
        echo WC()->cart->get_cart_contents_count();
        wp_die();
    }
    

    in your script file (assuming you have enqued the script file and passed the ajax object into the script. you also need to put this block into a setInterval or in some other jquery action.

    var data = {
      'action': 'cart_count_retriever'
    };
    jQuery.post(ajax_object.ajax_url, data, function(response) {
      alert('Got this from the server: ' + response);
    });
    

提交回复
热议问题