Empty cart in ajax
-
Good afternoon, the problem I have today is that I need to modify the shopping cart from the checkout page.
I need that when clicking on specific buttons, an ajax request is sent to a server action that removes a product from the cart and adds it again with a different variation_id.
The checkout window should be updated automatically
The problem I have is that when I connect via ajax to the server the WC()->cart variable contains an empty array even though there are products in the cart (when I see this variable without ajax it returns the cart correctly)Javascript:
function uac_change_cart(element) { //Cambiar carrito. var request = new XMLHttpRequest(); request.open('POST', 'https://mywebsite.com/wp-admin/admin-ajax.php', true); request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;'); request.onload = function () { if (this.status >= 200 && this.status < 400) { uac_update_cart_ui(); } else { alert("Ha ocurrido un error actualizando su carrito. Por favor, vuelva a intentarlo"); } }; request.onerror = function () { console.log("Error de conexión con el servidor. Compruebe su conexión a Internet"); }; request.send('action=uac_workspace_change_cart&payment=' + document.getElementById('uac_workspace_payment_mode').value + '&value=' + document.getElementById('uac_workspace_is_domain_purchase').checked + '&user_id=' + document.getElementById('uac_user_id').value); }
PHP:
function uac_workspace_change_cart() { global $wpdb, $woocommerce; if (!isset($_POST["payment"]) || !isset($_POST["value"]) || !isset($_POST["user_id"]) || !isset($_POST["cookie"])) { wp_send_json_error(402); } $workspace_product = ""; $payment = $_POST["payment"]; $value = $_POST["value"]; $user_id = $_POST["user_id"]; //this foreach is never executed because it detects the empty cart (When it isn't). The cases "XXXXX" are the id_product of the products i want to run this for. foreach (WC()->cart->get_cart_contents() as $cart_item) { $product_id = $cart_item["product_id"]; switch($product_id) { case 25754: case 23628: case 23836: case 21909: case 15479: case 21903: case 21895: case 16943: case 15467: case 19535: case 21895: case 21893: case 16926: case 16931: case 15453: $workspace_product = $cart_item; break; } } //If dont find the correct product in the cart give 404 to the client. if ($workspace_product == "") { wp_send_json_error(404); } //Remove old item from cart: WC()->cart->remove_cart_item( $workspace_product["key"] ); //Find the new variation_id (the product_id is the same). $variation_id = null; if ($payment == "YEARLY") { $period = "ANUAL"; } else { $period = "MENSUAL"; } if ($value == "true") { $variation = "Quiero comprar un dominio"; } else { $variation = "Ya tengo dominio profesional"; } $sql = "SELECT id FROM ".$wpdb->prefix."posts WHERE post_parent=".$workspace_product["product_id"]." and post_excerpt='Facturación: ".$period.", Dominio profesional: ".$variation."'"; $variation_id = $wpdb->get_results($sql, ARRAY_A)[0]["id"]; //Now i have the new variation_id for insert in the cart. //Add the same product_id but different variation_id to the cart. WC()->cart->add_to_cart( $workspace_product["product_id"], $workspace_product["quantity"], $variation_id, null, null ); //Trying to save the cart in session (dont work propertly i think). WC()->cart->set_session(); wp_send_json_success(200); } add_action('wp_ajax_uac_workspace_change_cart','uac_workspace_change_cart'); add_action('wp_ajax_nopriv_uac_workspace_change_cart','uac_workspace_change_cart');
I guess the problem is that when connecting asynchronously (ajax) the woocommerce init doesn’t run or the session cart doesn’t load. How could I fix it?
- The topic ‘Empty cart in ajax’ is closed to new replies.