Discussions

Load line item's prices in an array

Let me know if I'm posting in the wrong place. I searched and tried a lot of things but didn't find anything yet.

I need to load each line item's price present in the current user cart in an array. Then I want to sort the values from lowest to highest. For now I just want to print_r the result in a block with PHP eval module.

All I managed to do is to load total quantity of items in the cart and total amount of the cart with this code...

<?php
global $user;
$order = commerce_cart_order_load($user->uid);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$line_items = $order_wrapper->commerce_line_items;

$quantity = commerce_line_items_quantity($line_items, commerce_product_line_item_types());
$total = commerce_line_items_total($line_items, commerce_product_line_item_types());
print_r($quantity);
echo
"<br>";
print_r($total);
echo
"<br>";
?>

If I print_r $line_items I can see all the arrays with all the line items but don't know how to extract them.

Posted: May 17, 2013

Comments

Favio Manriquez on May 21, 2013

What about:

$line_items = $order_wrapper->commerce_line_items;
foreach ($line_items as $line_item) {
dpm($line_item->commerce_unit_price->value());
}