Advertisement
Guest User

Drupal Commerce Discount - amount

a guest
Sep 16th, 2012
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. <?php
  2.  
  3. function commerce_discounts_commerce_tax_type_calculate_rates($tax_type, $line_item) {
  4. // An implementation might contact a web service and apply the tax to the unit
  5. // price of the line item based on the returned data.
  6.  
  7. dsm($tax_type, 'taxtype');
  8. if ($tax_type['name'] == 'discount' ) {
  9.  
  10. global $user;
  11. $order = commerce_cart_order_load($user->uid);
  12. dsm($order->commerce_order_total['und'][0]['amount'], 'order commerce order total - amount');
  13. dsm($order, 'order');
  14. $discount_rate = commerce_discounts_get_rate($order->commerce_order_total['und'][0]['data']['components'][0]['price']['amount']);
  15. dsm($line_item, '$line_item');
  16. if ($discount_rate) {
  17. $item_base_price = $line_item->commerce_unit_price['und'][0]['amount'];
  18. $amount_discount = $item_base_price * $discount_rate;
  19. dsm('$item_base_price = ' . $item_base_price . ' $discount_rate=' . $discount_rate . ' $amount_discount=' . $amount_discount);
  20. $line_item->commerce_unit_price['und'][0]['data']['components'][] = array(
  21. 'name' => 'tax|discount_7_',
  22. 'price' => array(
  23. 'amount' => $amount_discount,
  24. 'currency_code' => 'USD',
  25. 'data' => array(
  26. 'tax_name' => array(
  27. 'name' => 'discount_7_',
  28. 'display_title' => 'Discount 7%',
  29. 'rate' => -0.07,
  30. 'price_component' => 'tax|discount_7_',
  31. 'calculation_callback ' => 'commerce_tax_rate_calculate',
  32. 'module' => 'commerce_tax_ui',
  33. 'title' => 'Discount 7%',
  34. ),
  35. ),
  36. ),
  37. 'included' => FALSE,
  38. );
  39. }
  40. dsm($line_item, '$line_item2');
  41. //dsm($line_item['commerce_total']['und'][0]->amount, '$line_item');
  42. dsm($line_item->commerce_total['und']['0']['amount'], '$line_item3');
  43. }
  44. dsm($tax_type, 'taxtype2');
  45. }
  46.  
  47.  
  48. /**
  49. * Get discount rate based on amount.
  50. * @param type $amount
  51. * @return type
  52. */
  53. function commerce_discounts_get_rate($amount) {
  54. // @TODO
  55. $discount = 0;
  56. if ($amount >= 6000 && $amount < 12000) {
  57. $discount = -0.07;
  58. }
  59. else if ($amount >= 12000 && $amount < 35000) {
  60. $discount = -0.15;
  61. }
  62. else if ($amount >= 35000) {
  63. $discount = -0.20;
  64. }
  65. return $discount;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement