0

I'm working on a Drupal module that needs to access the line items in a Drupal Commerce Shopping Cart. On most carts, there is a "line items" table of some sort that is linked to the cart via an unique cart id.

However, Drupal Commerce uses "Entities" which effectively obfuscate the relationship between the shopping cart id, the line items, the products and their prices. If you've played around with it at all, then you understand my pain when seeing the "revision" tables and how the data could exist in one of a dozen tables and requiring complex joins that would break if (when) the client upgrades their Commerce module.

I know there is a way to query entities, but I haven't found adequate explanations for using those queries with Drupal Commerce to allow me to pull line items and pricing from a cart. I keep running into Ubercart documentation which is close, but doesn't use entities.

Does anyone know where there is documentation on doing Entity queries (particularly with Drupal Commerce)? Better yet, do you have a query in your pocket that can pull cart contents, and line item pricing?

0

2 Answers 2

1

You can use Entity API that provide a unified way to deal with entities and their properties, please read README.TXT there. Handbook is poor.

1

There is one part that is confusing you a bit, when thinking about Drupal Commerce, and that is the cart. Users doesn't have a cart, but instead have orders. An order can have the status of being completed or being active. What you see in your cart is really your active order.

An order has reference to line items and each line item has a unit price, quantity, total price and a reference to a product. If all you want is to load the line items of a users active order (cart), you can do that pretty easily:

global $user;
$order = commerce_cart_order_load($user->uid);
if ($order) {
  // Using entity wrapper.
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
    $line_item = $line_item_wrapper->value();
  }
}

You could also get the line item ids from the order and use commerce_line_item_load_multiple to load the line items.

Your Answer

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