-2

I am looking to get WooCommerce product ID after successful addition of product in cart and store in global variable maybe? Because I want to use it on another page. I tried many way to get the ID but nothing work. Any idea?

I see someone marked this question duplicate, the solution in the link works for only first product added in cart, for example if someone add product A in cart, then on another page it shows the correct data, but if someone add product B, it still shows product A data. It doesn't change.

Ahmed Hassan
  • 259
  • 3
  • 15

1 Answers1

-2

I think this other stack overflow shows how to get product ids of the cart items:

Get in WooCommerce cart the product ID of a cart item

If you want to get the latest item, I believe you can take the last element in the get_cart array.

Myrtue
  • 306
  • 2
  • 14
  • Thank you for your answer. I already tried that, it works for some extent but it fails if there are number of products in cart. Is there any way to sort cart items in the order products added in cart? Then I think your solution will work. – Ahmed Hassan Oct 06 '18 at 10:52
  • How about storing the latest added product in cookies. You can write some code that executes when a product is added to the cart by using the woocommerce add to cart hook: add_action('woocommerce_add_to_cart', 'custome_add_to_cart'); function custome_add_to_cart() { //Add cookie about the product here } Place the code in your functions.php file. – Myrtue Oct 07 '18 at 12:18