2

How do I?:

  1. Get the current cart in commerce
  2. For each item in the cart, check its product type?

I want to do the following:

foreach($cart_items as $item){
  if($item[product_type]=='X'){
    //perform a task
  }
}

I want to use this in the module code for a custom checkout pane.

  • when do you want to do this? At checkout? When an item is added to the cart? – Chapabu Oct 23 '12 at 14:40
  • @Chapabu When deciding what to render for the display of the custom pane. So within the module code for a custom pane in this case checkout_pane_eventinfo_pane_checkout_form() – Scott Lawrence Oct 23 '12 at 14:46
8

Well this seems to work!

global $user;
$order = commerce_cart_order_load($user->uid);
$wrapper = entity_metadata_wrapper('commerce_order', $order);

foreach ($wrapper->commerce_line_items as $delta => $line_item_wrapper) {                                  
  $type = $line_item_wrapper->commerce_product->value()->type;   
  if($type=='X'){
    //perform a task
  }
}   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.