简体   繁体   中英

Woocommerce ajax remove from cart and update cart data returns zero

I am trying to minus a product or remove a product from cart.Suppose i have 3 same item in cart and i want to minus one but the ajax i used not works.I returns zero on console log.I thing this zero is from wp die().But it is expected to return some data.

Below is my codes in functions.php

add_action( 'wp_enqueue_scripts', 'the_foody_enqueue_cartajax_scripts' );
if ( ! function_exists( 'the_foody_enqueue_cartajax_scripts' ) )
{ 
    function the_foody_enqueue_cartajax_scripts() {
        wp_enqueue_script( 'ajax-script', get_template_directory_uri().'/js/ajax-cart.js', array('jquery'), '1.0', true );
        wp_localize_script( 'ajax-script', 'cart_ajax', array( 'ajaxurl' =>   admin_url( 'admin-ajax.php' ) ) );
    }
}

function the_foody_removefrom_cart_process(){
    if( !wp_verify_nonce( $_POST['nonce'], 'foody_reservation_nonce' ) ){
       die();
    }
    if( !isset($_POST['hash']) || !isset($_POST['quantity']) ){
       exit;
    }
    $cart_item_key = $_POST['hash'];
    if( !isset( WC()->cart->get_cart()[ $cart_item_key ] ) ){
       exit;
    }
    $values = WC()->cart->get_cart()[ $cart_item_key ];

    $_product = $values['data'];

    // Sanitize
    $quantity = apply_filters( 'woocommerce_stock_amount_cart_item',    apply_filters( 'woocommerce_stock_amount', preg_replace( "/[^0-9\.]/", '', filter_var($_POST['quantity'], FILTER_SANITIZE_NUMBER_INT)) ), $cart_item_key );

    if ( '' === $quantity || $quantity == $values['quantity'] )
        exit;

    // Update cart validation
    $passed_validation  = apply_filters( 'woocommerce_update_cart_validation', true, $cart_item_key, $values, $quantity );

    if ( $passed_validation ) {
        WC()->cart->set_quantity( $cart_item_key, $quantity, false );
    }

    // Recalc our totals
    WC()->cart->calculate_totals();
    return woocommerce_cart_totals();
}
add_action( 'wp_ajax_foody_removefrom_cart', 'the_foody_removefrom_cart_process' );    // If called from admin panel
add_action( 'wp_ajax_nopriv_foody_removefrom_cart', 'the_foody_removefrom_cart_process' );

And JS codes

function removefromCart( hash, nonce, qty ){
    $.ajax({
        action : 'foody_removefrom_cart',
        type : 'POST',
        url : cart_ajax.ajaxurl,
        data : {
            'hash'     : hash,
            'nonce'    : nonce,
            'quantity' : qty,
        },
        beforeSend : function(){
            // codes to execute
        },
        success : function(response){
            console.log(response);

        }
    })
}

But it returns only zero in console.

In js passed variable hash,nonce,qty are okay.

Got php codes from here

You should use like code below:

    function removefromCart( hash, nonce, qty ){
        $.ajax({

            type : 'POST',
            url : cart_ajax.ajaxurl,
            data : {
                'hash'     : hash,
                'nonce'    : nonce,
                'quantity' : qty,
                action : 'the_foody_removefrom_cart_process',
            },
            beforeSend : function(){
                // codes to execute
            },
            success : function(response){
                console.log(response);

            }
        })
    }


    add_action( 'wp_ajax_the_foody_removefrom_cart_process', 'the_foody_removefrom_cart_process' );    // If called from admin panel
    add_action( 'wp_ajax_nopriv_the_foody_removefrom_cart_process', 'the_foody_removefrom_cart_process' );

instead of

    add_action( 'wp_ajax_foody_removefrom_cart', 'the_foody_removefrom_cart_process' );    // If called from admin panel
    add_action( 'wp_ajax_nopriv_foody_removefrom_cart', 'the_foody_removefrom_cart_process' );

updated

       type : 'POST',
        url : cart_ajax.ajaxurl,
        data : {
            'hash'     : hash,
            'nonce'    : nonce,
            'quantity' : qty,
            action : 'the_foody_removefrom_cart_process',
        },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM