Refresh cart with WooCommerce ajax

In my woocommerce website, I changed the cart page, removed the "refresh cart" button, and created 2 buttons to add and remove product elements, as shown in this figure:

enter image description here

When I press the quantity buttons, I want to call the same function if I press the button to refresh the basket.

I use ajax for this, but it does nothing.

First in my file function.phpI have this:

  function update_my_cart() {
    // here update then cart
    var_dump("execute");
  }
  add_action( 'wp_ajax_update_my_cart', 'update_my_cart' );    // If called from admin panel
  add_action( 'wp_ajax_nopriv_update_my_cart', 'update_my_cart' );  



    add_action( 'wp_enqueue_scripts', 'rct_enqueue_scripts' );

    if ( ! function_exists( 'rct_enqueue_scripts' ) ) :

    function rct_enqueue_scripts() {
    wp_enqueue_script( 'rct-js', get_template_directory_uri() . '/js/themeCoffee.js', array(), '1.0', true );
    wp_localize_script('rct-js', 'ajax_object', array('ajax_url' => admin_url( 'admin-ajax.php' )));
    }

    endif;

And in my jquery file, I have the following:

  updatecart = function(qty) {
    var currentVal, data, item_hash, request;
    currentVal = void 0;
    data = void 0;
    item_hash = void 0;
    currentVal = parseFloat(qty);
    request = $.ajax({
      url: 'ajax_object.ajax_url',
      method: 'POST',
      data: {
        quantity: currentVal,
        action: 'update_my_cart'
      },
      dataType: 'html'
    });
    request.done(function(msg) {
      alert('cart update ');
    });
    request.fail(function(jqXHR, textStatus) {
      alert('Request failed: ' + textStatus);
    });
  };   

I get this error:

Failed to load resource: the server responded with a status of 404 (Not Found)

Because I'm trying to download my_website/cart/ajax_object.ajax_url.

Thanks in advance!

+5
source share
2 answers

You forgot this important process:

add_action('wp_enqueue_scripts', 'add_my_ajax_scripts'); 

function add_my_ajax_scripts() {
    // Here you register your script located in a subfolder 'js' of your active theme
    wp_enqueue_script( 'ajax-script', get_template_directory_uri().'/js/script.js', array('jquery'), '1.0', true );
    // Here you are going to make the bridge between php and js
    wp_localize_script( 'ajax-script', 'cart_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}

" ajaxurl " " cart_ajax " JavaScript " url: ":

$.ajax({
  url: cart_ajax.ajaxurl,
  ...
})

JavaScript . , :

+7

WooCommerce 2.6.0, 2016 , WooCommerce Ajax .

Ajax, , " ".

Ajax Cart AutoUpdate WooCommerce, - .

, . , . , 1 10, , 9 Ajax 1.

JQuery , js jQuery, jQuery:

var timeout;

jQuery('div.woocommerce').on('change keyup mouseup', 'input.qty', function(){ // keyup and mouseup for Firefox support
    if (timeout != undefined) clearTimeout(timeout); //cancel previously scheduled event
    if (jQuery(this).val() == '') return; //qty empty, instead of removing item from cart, do nothing
    timeout = setTimeout(function() {
        jQuery('[name="update_cart"]').trigger('click');
    }, 1000 );
});
0

All Articles