Advertisement
Guest User

Untitled

a guest
Oct 1st, 2012
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. <?php
  2. // file commerce_discounts.rules.inc
  3.  
  4. function commerce_discounts_rules_condition_info() {
  5. $conditions = array();
  6.  
  7. $conditions['commerce_discounts_order_balance_comparison'] = array(
  8. 'label' => t('Subtotal comparison'),
  9. 'parameter' => array(
  10. 'commerce_order' => array(
  11. 'type' => 'commerce_order',
  12. 'label' => t('Order'),
  13. 'description' => t('The order whose balance should be compared (calculated as the order total minus completed payment amounts).'),
  14. ),
  15. 'operator' => array(
  16. 'type' => 'text',
  17. 'label' => t('Operator'),
  18. 'description' => t('The comparison operator.'),
  19. 'optional' => TRUE,
  20. 'default value' => '<=',
  21. 'options list' => 'commerce_numeric_comparison_operator_options_list',
  22. 'restriction' => 'input',
  23. ),
  24. 'value' => array(
  25. 'type' => 'text',
  26. 'label' => t('Value'),
  27. '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.'),
  28. 'default value' => '0',
  29. ),
  30. ),
  31. 'group' => t('Commerce Payment'),
  32. 'callbacks' => array(
  33. 'execute' => 'commerce_discounts_rules_compare_balance',
  34. ),
  35. );
  36.  
  37.  
  38. return $conditions;
  39. }
  40.  
  41. /**
  42. * Condition callback: checks the unpaid balance of an order.
  43. */
  44. function commerce_discounts_rules_compare_balance($order, $operator, $value) {
  45. // Check the balance of the order.
  46. //$balance = commerce_payment_order_balance($order);
  47.  
  48. $subtotal = 0;
  49. $i = 0;
  50. foreach ($order->commerce_line_items['und'] as $line_item_id) {
  51. //dsm($line_item_id , '$line_item_id');
  52. $line_item = commerce_line_item_load($line_item_id['line_item_id']);
  53. $i++;
  54. //dsm($line_item, 'line item ' . $i);
  55. if($line_item->type == 'product') {
  56. $subtotal += $line_item->commerce_total['und'][0]['data']['components'][0]['price']['amount'];
  57. }
  58. }
  59. //dsm($order, '$ordeer');
  60. //dsm($balance, 'balance');
  61. //dsm($subtotal, '$subtotal');
  62. // If the balance was incalculable, set the balance to the order total.
  63. // if ($balance === FALSE) {
  64. // $balance = entity_metadata_wrapper('commerce_order', $order)->commerce_order_total->value();
  65. // }
  66.  
  67. // Make a quantity comparison based on the operator.
  68. switch ($operator) {
  69. case '<':
  70. return $subtotal < $value;
  71. case '<=':
  72. return $subtotal <= $value;
  73. case '=':
  74. return $subtotal == $value;
  75. case '>=':
  76. return $subtotal >= $value;
  77. case '>':
  78. return $subtotal > $value;
  79. }
  80.  
  81. return FALSE;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement