Browsed a load of similar questions with no success so far.
I want to display a WC notice naming the last item added to the cart on a regular page.
Notification is up and running, however, so far I was not able to identify the ID of last item added to the cart.
I've tried this
$items = WC()->cart->get_cart();
$ids = array();
foreach($items as $item => $values) {
$_product = $values['data']->post;
$ids[] = $_product->ID;
}
$last_product_id = end($ids);
$added_product = wc_get_product( $last_product_id );
$added_product_name = $added_product->get_title();
But as I've learned cart content does not get updated during AJAX calls. The easiest way to obtain the product ID should be the AJAX parameter containing it, but it cannot be read via $_GET.
Does anyone know of a way to retrieve the product ID of the last item added via WC hook/jQuery?
See Question&Answers more detail:os