Inside this hook I need the total products per sku. For example, user #1 buy 2 products with the sku #7, user #2 buy 5 products with the sku #10. I need to know how to get:

  • sku #7 -> 2
  • sku #10 -> 5

I was trying something like:

foreach ( $order->commerce_line_items as $line_item ) {
   $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);

But it gives me just sku #7 and sku #10. Also, this function commerce_line_items_quantity($line_items) didn't help so much because it gave me just the total of all sku's, not individually.

This way I can award the user with some "user points" (I think is easier this way than using rules).

  • How are you intended to award point here ?
    – RajeevK
    Commented Jun 5, 2013 at 8:47
  • "I think is easier this way than using rules" I'd be very surprised if that's true...
    – Clive
    Commented Jun 5, 2013 at 9:08
  • My point here is to do some calculations with the points before award the user with them. For example, give the user more points depending the role, date, quantity they buy, etc.
    – user16360
    Commented Jun 5, 2013 at 16:00
  • @RajeevK - I will use the function userpoints_userpointsapi($params);
    – user16360
    Commented Jun 5, 2013 at 16:07

1 Answer 1


You need to wrap the $order object before looping over the line items. It also removes the need to wrap the line items explicitly.

$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
foreach ( $order_wrapper->commerce_line_items as $line_item_wrapper ) {
  if ($line_item_wrapper->commerce_product->sku->value() == 'some-SKU') {
    // increment quantity counter, award points etc.
  • Is there something like $line_item_wrapper->commerce_product->quantity->value(); ? Also, there is some way to print all the $line_item_wrapper object (dpm, dsm, debug didn't work)?
    – user16360
    Commented Jun 5, 2013 at 20:40
  • 1
    Updated the code example to use a separate variable for the wrapper. And no, there is no quantity property on the product referenced by the line item. You'd find the quantity in $line_item_wrapper->quantity->value() because it's a property of the line item itself. I updated the code example to show a comparison against a SKU on the product, and you'd increment a quantity counter or something inside the if statement. Commented Jun 6, 2013 at 3:21
  • Thanks @Clive and @Ryan! It worked perfectly! Just a quick question: how do I debug those wrappers in order to know what they have inside? Using dsm, dpm, debug, var_dump, etc, didn't help so much (should I make another question?)
    – user16360
    Commented Jun 6, 2013 at 4:29
  • @Jose Beat you to it :P drupal.stackexchange.com/questions/33473/…
    – Clive
    Commented Jun 6, 2013 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.