Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // file commerce_discounts.rules.inc
- function commerce_discounts_rules_condition_info() {
- $conditions = array();
- $conditions['commerce_discounts_order_balance_comparison'] = array(
- 'label' => t('Subtotal comparison'),
- 'parameter' => array(
- 'commerce_order' => array(
- 'type' => 'commerce_order',
- 'label' => t('Order'),
- 'description' => t('The order whose balance should be compared (calculated as the order total minus completed payment amounts).'),
- ),
- 'operator' => array(
- 'type' => 'text',
- 'label' => t('Operator'),
- 'description' => t('The comparison operator.'),
- 'optional' => TRUE,
- 'default value' => '<=',
- 'options list' => 'commerce_numeric_comparison_operator_options_list',
- 'restriction' => 'input',
- ),
- 'value' => array(
- 'type' => 'text',
- 'label' => t('Value'),
- 'description' => t('The value to compare against the order balance. An order balance less than or equal to 0 indicates the order has been paid in full.'),
- 'default value' => '0',
- ),
- ),
- 'group' => t('Commerce Payment'),
- 'callbacks' => array(
- 'execute' => 'commerce_discounts_rules_compare_balance',
- ),
- );
- return $conditions;
- }
- /**
- * Condition callback: checks the unpaid balance of an order.
- */
- function commerce_discounts_rules_compare_balance($order, $operator, $value) {
- // Check the balance of the order.
- //$balance = commerce_payment_order_balance($order);
- $subtotal = 0;
- $i = 0;
- foreach ($order->commerce_line_items['und'] as $line_item_id) {
- //dsm($line_item_id , '$line_item_id');
- $line_item = commerce_line_item_load($line_item_id['line_item_id']);
- $i++;
- //dsm($line_item, 'line item ' . $i);
- if($line_item->type == 'product') {
- $subtotal += $line_item->commerce_total['und'][0]['data']['components'][0]['price']['amount'];
- }
- }
- //dsm($order, '$ordeer');
- //dsm($balance, 'balance');
- //dsm($subtotal, '$subtotal');
- // If the balance was incalculable, set the balance to the order total.
- // if ($balance === FALSE) {
- // $balance = entity_metadata_wrapper('commerce_order', $order)->commerce_order_total->value();
- // }
- // Make a quantity comparison based on the operator.
- switch ($operator) {
- case '<':
- return $subtotal < $value;
- case '<=':
- return $subtotal <= $value;
- case '=':
- return $subtotal == $value;
- case '>=':
- return $subtotal >= $value;
- case '>':
- return $subtotal > $value;
- }
- return FALSE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement