0

I try all of code from google, but none of them works.

such as:

$items = WC()->cart->get_cart();
$product_id = end($items)['data']->post->ID;

For example,

shoes have two different colors: black and gray. I added black shoes, then gray shoes, I order black shoes in the end.

It should return black shoes's ID, but the code on the top will shows gray shoes's id.

I want last added product, not the last position product.

fhgroupauto
  • 13
  • 1
  • 7
  • Use `$items = WC()->cart->get_cart();` and `$product_id = end($items)['data']->get_id();` or `$product_id = end($items)['product_id'];` – LoicTheAztec Sep 11 '18 at 18:50
  • 1
    Actually it's different. I want the last product added to cart's id . that code only get last position product. If I added A product, then B product, then A product, it should shows A's id ,but the result is B. – fhgroupauto Sep 11 '18 at 19:11
  • I try reset, it would always return A's id, not matter the last product added to cart is A or B. – fhgroupauto Sep 11 '18 at 19:28
  • I want last added item. – fhgroupauto Sep 11 '18 at 19:44
  • So for the last item is `$product_id = end($items)['data']->get_id();` or `$product_id = end($items)['product_id'];` … Now **may be your problem is that this is not refreshed** when using **ajax** add to cart. To get that refreshed, it is a new question that you will need to ask, with all related details well explained clearly. – LoicTheAztec Sep 11 '18 at 19:52
  • I need the recently product's id added to cart . the code is to get last product in the cart. – fhgroupauto Sep 11 '18 at 19:56

3 Answers3

1

You can put the products into an array and pick the last one using the php end() function as follows:

global $woocommerce;

//get cart items
$items = $woocommerce->cart->get_cart();

$ids = array();
foreach($items as $item => $values) { 
        $_product = $values['data']->post; 
        //push each id into array
        $ids[] = $_product->ID; 
} 

//get last product id
$last_product_id = end($ids);

//get product variation details
$variations = get_variation_data_from_variation_id( $last_product_id );

Then define get_variation_data_from_variation_id($item_id) as follows:

//function to get product variation
    function get_variation_data_from_variation_id( $item_id ) {
        $_product = new WC_Product_Variation( $item_id );
        $variation_data = $_product->get_variation_attributes();
        //return variation detail
        return woocommerce_get_formatted_variation( $variation_data, true ); 
    }
Elisha Senoo
  • 3,489
  • 2
  • 22
  • 29
0

Edited I think you were looking for product attribute IDs? Like green or blue etc.

$cart = WC()->cart->get_cart();
$variation = end($cart)['variation'];

// This will have ID, slug, count, etc.
echo json_encode( get_term_by( 'name', end($variation) , str_replace( "attribute_", "", key($variation))) );

If you are specific about you attribute like just size change it like this based on your ta

$cart = WC()->cart->get_cart();
$variation = end($cart)['variation']['attribute_pa_size'];

// This will have ID, slug, count, etc.
echo json_encode( get_term_by( 'name', $variation , 'pa_size') );
hemnath mouli
  • 2,617
  • 2
  • 17
  • 35
0

That means you probably access the cart Object before It's created, You will need to place that code inside 'wp' hook for example.

add_action('wp', function(){
   $product_id = end( WC()->cart->cart_contents)['product_id'];
});
Ash0ur
  • 1,505
  • 2
  • 8
  • 11