Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce ajax update messing up values

I'm currently making a web shop with WooCommerce and i have this cart made that you can access at any page at any time, you can update the quantity of a product within the cart itself. The problem happens whenever i do this some values get messed up. For example when i try to get WC()->cart->total it returns 0.

But when i go to the checkout page it shows all the correct cart data, so this makes me think i'm missing some action i have to run after adjusting something in the cart. I've been looking trough the set_quantity() functions and it automatically refreshes total with $this->calculate_totals(); (tried it manually as well).

Ajax function:

public function set_quantity($direction = false, $product_id) {
    $response = array();
    $justOne = false;

    if($_GET['data']['direction'] && $_GET['data']['product_id']) {
        $direction = $_GET['data']['direction'];
        $product_id = $_GET['data']['product_id'];
        $justOne = true;
    }

    foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
        $_product = $values['data'];
        if ($product_id == $_product->id) {

            if($justOne && $direction == 'minus') {
                WC()->cart->set_quantity($cart_item_key, $values['quantity'] - 1, true);
                $response['success']['quantity'] = $values['quantity'] - 1;
            } else if($justOne && $direction == 'plus') {
                WC()->cart->set_quantity($cart_item_key, $values['quantity'] + 1, true);
                $response['success']['quantity'] = $values['quantity'] + 1;
            } else {
                WC()->cart->set_quantity($cart_item_key, $values['quantity'] + $direction, true);
            }

            $response['success']['line_total'] = '€ '.number_format((float)$response['success']['quantity'] * $_product->price, 2, '.', '');
            $response['success']['cart_count'] = WC()->cart->get_cart_contents_count();
            $response['success']['total'] = number_format((float)WC()->cart->total, 2, '.', '');
            die(json_encode($response));
        }
    }
    return false;
}
like image 795
LVDM Avatar asked Aug 30 '16 09:08

LVDM


1 Answers

Use this modified ajax function. I've tested this. It'll work.

Modified Ajax Function:

public function set_quantity($direction = false, $product_id) {
    $response = array();
    $justOne = false;

    if($_GET['data']['direction'] && $_GET['data']['product_id']) {
        $direction = $_GET['data']['direction'];
        $product_id = $_GET['data']['product_id'];
        $justOne = true;
    }

    foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
        $_product = $values['data'];
        if ($product_id == $_product->id) {

            if($justOne && $direction == 'minus') {
                WC()->cart->set_quantity($cart_item_key, $values['quantity'] - 1, true);
                $response['success']['quantity'] = $values['quantity'] - 1;
            } else if($justOne && $direction == 'plus') {
                WC()->cart->set_quantity($cart_item_key, $values['quantity'] + 1, true);
                $response['success']['quantity'] = $values['quantity'] + 1;
            } else {
                WC()->cart->set_quantity($cart_item_key, $values['quantity'] + $direction, true);
            }

            if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
                define( 'WOOCOMMERCE_CART', true );
            }
            WC()->cart->calculate_totals();

            $response['success']['line_total'] = '€ '.number_format((float)$response['success']['quantity'] * $_product->price, 2, '.', '');
            $response['success']['cart_count'] = WC()->cart->get_cart_contents_count();
            $response['success']['total'] = number_format((float)WC()->cart->total, 2, '.', '');
            die(json_encode($response));
        }
    }
    return false;
}
like image 78
Ratnakar - StoreApps Avatar answered Sep 21 '22 09:09

Ratnakar - StoreApps