Skip to content

Instantly share code, notes, and snippets.

@jasonruyle
Created January 14, 2014 18:03
Show Gist options
  • Save jasonruyle/8422776 to your computer and use it in GitHub Desktop.
Save jasonruyle/8422776 to your computer and use it in GitHub Desktop.
Drupal Commerce Rules: Loop through Commerce Line Items
<?php
// Use the code inside rule conditions with a Custom PHP code
// FIELD_MACHINE_NAME example: field_product_free_shipping
global $user;
$commerce_order = commerce_cart_order_load($user->uid);
$line_items = $commerce_order->commerce_line_items['und'];
foreach ($line_items as $lid) {
$line_item = commerce_line_item_load($lid['line_item_id']);
if ( isset($line_item->commerce_product)) {
$pid = $line_item->commerce_product['und'][0]['product_id'];
$product = commerce_product_load($pid);
$FIELD_MACHINE_NAME = render($product->FIELD_MACHINE_NAME['und'][0]['value']);
if ($FIELD_MACHINE_NAME == '1') {
return TRUE;
}
}
}
return FALSE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment