function commerce_order_rules_contains_product in Commerce Core 7
Condition callback: checks to see if a particular product exists on an order in the specified quantity.
1 string reference to 'commerce_order_rules_contains_product'
- commerce_order_rules_condition_info in modules/
order/ commerce_order.rules.inc - Implements hook_rules_condition_info().
File
- modules/
order/ commerce_order.rules.inc, line 276 - Rules integration for orders.
Code
function commerce_order_rules_contains_product($order, $sku, $operator, $value) {
$count = 0;
// If we got a valid order with line items...
if (!empty($order) && !empty($order->commerce_line_items)) {
// Collect all the line item IDs on the order.
$line_item_ids = array();
if ($items = field_get_items('commerce_order', $order, 'commerce_line_items')) {
foreach ($items as $item) {
$line_item_ids[] = $item['line_item_id'];
}
}
// If we found valid line item IDs...
if (!empty($line_item_ids)) {
// Get the product ID matching the given SKU.
$product_id = db_query("SELECT product_id FROM {commerce_product} WHERE sku = :sku", array(
':sku' => $sku,
))
->fetchField();
// Build a query to find the line items matching the given SKU.
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'commerce_line_item')
->entityCondition('bundle', commerce_product_line_item_types(), 'IN')
->entityCondition('entity_id', $line_item_ids, 'IN')
->fieldCondition('commerce_product', 'product_id', $product_id);
$result = $query
->execute();
if (!empty($result['commerce_line_item'])) {
$count = commerce_line_items_quantity_by_id(array_keys($result['commerce_line_item']));
}
}
}
// Make a quantity comparison based on the operator.
switch ($operator) {
case '<':
return $count < $value;
case '<=':
return $count <= $value;
case '=':
return $count == $value;
case '>=':
return $count >= $value;
case '>':
return $count > $value;
}
return FALSE;
}