in Education by
I have succeed implement this code to remove product from cart with Ajax. But it didn't works with Variable Product. /** * Remove Cart via Ajax */ function product_remove() { global $wpdb, $woocommerce; session_start(); $cart = WC()->instance()->cart; $id = $_POST['product_id']; $cart_id = $cart->generate_cart_id($id); $cart_item_id = $cart->find_product_in_cart($cart_id); if($cart_item_id){ $cart->set_quantity($cart_item_id,0); } } add_action( 'wp_ajax_product_remove', 'product_remove' ); add_action( 'wp_ajax_nopriv_product_remove', 'product_remove' ); Maybe i need to pass $variation_id to $cart_id but i dont know how to do it. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Create the link on cart using the $cart_item_key instead of the $product_id. Then, on server side, you don't need to use the $cart->generate_cart_id($id); method, because you already have it. See the example that works for me: First, the creation of the cart: // This is the logic that create the cart foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { ?> "> // Remove product link ">× // Other product info goes here... <?php } Now the modifications on server-side: /** * Remove Cart via Ajax */ function product_remove() { global $wpdb, $woocommerce; session_start(); $cart = WC()->instance()->cart; $cart_id = $_POST['product_id']; // This info is already the result of generate_cart_id method now /* $cart_id = $cart->generate_cart_id($id); // No need for this! :) */ $cart_item_id = $cart->find_product_in_cart($cart_id); if($cart_item_id){ $cart->set_quantity($cart_item_id,0); } } add_action( 'wp_ajax_product_remove', 'product_remove' ); add_action( 'wp_ajax_nopriv_product_remove', 'product_remove' ); This works fine for me!

Related questions

0 votes
    I am using a framework, which returns invalid JSON String like: /* { "myobject" : "test"} */ ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    Is the following a valid variable assignment var product cost = 3.45; 1. No. There should be no space in the variable ... No. Floating numbers are not allowed 3. Yes. It is valid...
asked Feb 23, 2021 in Technology by JackTerrance
+1 vote
    Is the following a valid variable assignment var product cost = 3.45; a)No. There should be no space in the variable name b)No. Floating numbers are not allowed c)Yes. It is valid...
asked Oct 8, 2020 in Technology by JackTerrance
0 votes
    I'm using the WooCommerce Membership plugin to display member discounts on the Product page. Now I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I want to make a button Go To Cart visible after adding a product into cart. But when page gets ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    _________ databases are used to store Session information, User profile, preferences, and shopping cart data. (1)Key-Value Pair (2)Graph (3)Document (4)Column...
asked Apr 21, 2021 in Technology by JackTerrance
+1 vote
    _________ databases are used to store Session information, User profile, preferences, and shopping cart data....
asked Oct 11, 2020 in Education by anonymous
0 votes
    Can I use ASP.NET AJAX with any other technology apart from ASP.NET?...
asked Dec 31, 2020 in Technology by JackTerrance
0 votes
    I have following code and I want to get data from service. I have set everything from what I get ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I have following code and I want to get data from service. I have set everything from what I get ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    My goal here is to delete the correspondent data in mysql db table using AJAX. So I've generated a table with several rows and want to a non specific row... PHP Code:...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    First I'll explain my senario: I have a page in which users login or register. The login control ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    The data from the AJAX request is usually in XML. True or False? 1. False 2. True...
asked Feb 26, 2021 in Technology by JackTerrance
0 votes
0 votes
    Two numbers are selected at random from 1,2,3,....100 and are multiplied, then the probability correct to two places ... 67 75 D. 8 75 Select the correct answer from above options...
asked Nov 15, 2021 in Education by JackTerrance
...