Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function commerce_discounts_commerce_tax_type_calculate_rates($tax_type, $line_item) {
- // An implementation might contact a web service and apply the tax to the unit
- // price of the line item based on the returned data.
- dsm($tax_type, 'taxtype');
- if ($tax_type['name'] == 'discount' ) {
- global $user;
- $order = commerce_cart_order_load($user->uid);
- dsm($order->commerce_order_total['und'][0]['amount'], 'order commerce order total - amount');
- dsm($order, 'order');
- $discount_rate = commerce_discounts_get_rate($order->commerce_order_total['und'][0]['data']['components'][0]['price']['amount']);
- dsm($line_item, '$line_item');
- if ($discount_rate) {
- $item_base_price = $line_item->commerce_unit_price['und'][0]['amount'];
- $amount_discount = $item_base_price * $discount_rate;
- dsm('$item_base_price = ' . $item_base_price . ' $discount_rate=' . $discount_rate . ' $amount_discount=' . $amount_discount);
- $line_item->commerce_unit_price['und'][0]['data']['components'][] = array(
- 'name' => 'tax|discount_7_',
- 'price' => array(
- 'amount' => $amount_discount,
- 'currency_code' => 'USD',
- 'data' => array(
- 'tax_name' => array(
- 'name' => 'discount_7_',
- 'display_title' => 'Discount 7%',
- 'rate' => -0.07,
- 'price_component' => 'tax|discount_7_',
- 'calculation_callback ' => 'commerce_tax_rate_calculate',
- 'module' => 'commerce_tax_ui',
- 'title' => 'Discount 7%',
- ),
- ),
- ),
- 'included' => FALSE,
- );
- }
- dsm($line_item, '$line_item2');
- //dsm($line_item['commerce_total']['und'][0]->amount, '$line_item');
- dsm($line_item->commerce_total['und']['0']['amount'], '$line_item3');
- }
- dsm($tax_type, 'taxtype2');
- }
- /**
- * Get discount rate based on amount.
- * @param type $amount
- * @return type
- */
- function commerce_discounts_get_rate($amount) {
- // @TODO
- $discount = 0;
- if ($amount >= 6000 && $amount < 12000) {
- $discount = -0.07;
- }
- else if ($amount >= 12000 && $amount < 35000) {
- $discount = -0.15;
- }
- else if ($amount >= 35000) {
- $discount = -0.20;
- }
- return $discount;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement