M
M
Muvka2019-01-06 16:41:32
WooCommerce
Muvka, 2019-01-06 16:41:32

Can I pass my data through Woocommerce snippets?

This is the code to remove an item from the cart

add_action('wp_ajax_remove_cart_item', 'remove_cart_item');
add_action('wp_ajax_nopriv_remove_cart_item', 'remove_cart_item');
        
function remove_cart_item() {
  global $woocommerce;
  $cart_id = wc_clean($_POST['cart_id']);

  if(WC()->cart->remove_cart_item($cart_id)) {
    WC_AJAX::get_refreshed_fragments();
  } else {
    $data = array(
      'error' => true
    );
    echo wp_send_json($data);
  }

  wp_die();
}

But the problem is that in order to make the "add to cart" button available, I need to get the product id in the js. I know how to get the product id. The problem is how to pass this id to js. I have only 2 options in my head - make one more request to the server before removing the item from the cart, but this method is not very correct, as for me. And the second - in the woocommerce_add_to_cart_fragments filter, in the fragments, create the product_id key and, when replacing the content of the elements, check for this key. But also not a great option. Is it possible somehow to add one more key to the cart_hash and fragments returned after WC_AJAX::get_refreshed_fragments() only in the function I need?
Addendum: After looking at the code of the method, I came to the conclusion that it might be easier to write my own similar function that will take parameters? I still write all the js code myself.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question