Because the line_items were concatenated,
the last added item doesn't have to be the item the message was shown.

So I chose to take the last changed item instead. Because I did not use git on the machine, here is the src code from line 880 following in dc_ajax_add_cart.module:

/**
 * Returns the last line item that was changed to cart.
 *
 * @param object $line_items_wrapper
 *   Entity list wrapper of line items.
 *
 * @return object
 *   The last line item.
 */
function dc_ajax_add_cart_get_last_line_item_wrapper($line_items_wrapper) {
  $result = $line_items_wrapper->value();
  $changed = 0;
  foreach ($result as $key => $value) {
    if($value->changed > $changed){
      $last_changed_key = $key;
      $changed = $value->changed;
    }
  }
  return $result[$last_changed_key];
}

Comments

maen created an issue. See original summary.

maen’s picture

Issue summary: View changes
subhojit777’s picture

Version: 7.x-2.11 » 7.x-2.x-dev
Status: Fixed » Postponed (maintainer needs more info)

The logic to get the last line item is nice. But I would like to replicate the problem on my end. What do you mean by Because the line_items were concatenated?

subhojit777’s picture

maen’s picture

I use Commerce Customizable Products to have line item fields in the product form. When I add the exact same product multiple times to cart they were gathered in 1 line item. So I do not see the last added product, instead I see the last product which was added with a different price.
For example:
I buy 1 white shirt -> 3 $. -> correct display.
I buy 1 black shirt -> 4 $. -> correct display.
I buy 1 white shirt-> 3 $. -> shows the black shirt in the pop-up display. -> wrong!

That's it.
The db table "commerce_line_item" sets the quantity from 1 to 2.

stockticker’s picture

Got a similar issue, when ajax pop-up message showed wrong items (not that products, where 'add to cart' button was pressed on).

However, piece of code, that maen provided - worked like a charm.

Many thanks!

subhojit777’s picture

Assigned: Unassigned » subhojit777
Status: Postponed (maintainer needs more info) » Needs work

  • subhojit777 committed 22b0076 on 7.x-2.x authored by maen
    Issue #2915502 by maen, subhojit777: Display popup message shows wrong...
subhojit777’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.