Remove products from cart using Ajax

Hi..

I’m trying to remove products from cart using Ajax.

The php code in the backend is:

if (isset( $_POST ) ) {

require_once(‘../../../wp-load.php’:wink:;

wp_head();

global $mp;

list($bid, $product_id, $variation) = split(‘:’, $_POST);

$global_cart = $mp->get_cart_cookie(true);

unset ( $global_cart[$bid][$product_id][$variation] );

$mp->set_global_cart_cookie($global_cart);

}

but it’s not working… I’ve tooked this code from your “marketpress.php”.

all the objects and variables are fine and with the right data, but for some reason the “unset” function have no effect.

  • Aaron
    • CTO

    Here is the bit of javascript to empty the cart in ajax-cart.js:

    $("a.mp_empty_cart").click(function() {
    var answer = confirm(MP_Ajax.emptyCartMsg);
    if (answer) {
    $(this).html('<img src="'+MP_Ajax.imgUrl+'" />');
    $.post(MP_Ajax.ajaxUrl, {action: 'mp-update-cart', empty_cart: 1}, function(data) {
    $("div.mp_cart_widget_content").html(data);
    });
    }
    return false;
    });

    The key part being the ajax call:

    jQuery.post(MP_Ajax.ajaxUrl, {action: 'mp-update-cart', empty_cart: 1});

    Pretty simple as MP already has an ajax handler in the backend for this.

  • InnoviNet
    • Design Lord, Child of Thor

    Yes i know… my question is different…

    My AJAX is fine. the problem is in the back-end.

    The code I’ve wrote in the question should make the “unset” on the wanted product to remove.

    The parameter “remove” contains his $bid : $product_id : $variation.

    The $global_cart contains all the products in the cart in this format also.

    the “unset” suppose to remove “remove” from $global_cart… right?

    and that’s it… this is my full code:

    JS:

    $(“.delete-button”:wink:.off(‘vclick’:wink:.on(‘vclick’, function(){

    removeProduct( $(this).siblings(‘input’:wink:.val() );

    });

    function removeProduct(val) {

    $.ajax({

    url: “/path/mp_cart.php”, /* in my theme */

    data: {remove: val},

    type: “POST”,

    dataType: “json”,

    success: function(msg) {

    console.log(msg);

    }

    });

    }

    my_cart.php: // in my theme

    if (isset( $_POST ) ) {

    require_once(‘../../../wp-load.php’:wink:;

    wp_head();

    global $mp;

    list($bid, $product_id, $variation) = split(‘:’, $_POST);

    $global_cart = $mp->get_cart_cookie(true);

    unset ( $global_cart[$bid][$product_id][$variation] );

    $mp->set_global_cart_cookie($global_cart);

    }

    In conclusion, the AJAX works fine, he passes the right parameter to remove, the $global_cart also fine, contains all the products in the cart. but the unset isn’t working!!! that is the only problem…………..

    Thanks

  • InnoviNet
    • Design Lord, Child of Thor

    Yes the wp_head shouldn’t be there.. i’ve removed it from there.. its not changing nothing.

    There is a reason way he will not be able to set the cookies?

    The “get_cart_cookie” is working fine, so why the “set_global_cart_cookie” will not work?

    Here is a snapshot of the “$global_cart” after the “$global_cart = $mp->get_cart_cookie(true);”

    Array

    (

    [3] => Array

    (

    [64] => Array

    (

    [0] => 4

    )

    [56] => Array

    (

    [0] => 2

    )

    )

    )

    so actually the unset is looking like that:

    unset ( $global_cart[3][64][0] );

  • InnoviNet
    • Design Lord, Child of Thor

    so after this piece of code:

    print_r($global_cart);

    unset ( $global_cart[$bid][$product_id][$variation] );

    print_r($global_cart);

    i’m getting this:

    Array

    (

    [3] => Array

    (

    [64] => Array

    (

    [0] => 4

    )

    [56] => Array

    (

    [0] => 2

    )

    )

    )

    Array

    (

    [3] => Array

    (

    [64] => Array

    (

    [0] => 4

    )

    [56] => Array

    (

    [0] => 2

    )

    )

    )

    Nothing changed.. Regardless of the Cookies stuff…

    Why???

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.