Paul Williams
Paul Williams

Reputation: 1598

Retrieving the total discount of an order in process in ZenCart

I'm working with ZenCart 1.5.1 and a custom mod called "TaxCloud".

I would like to know if there is a way to retrieve an active, in process order's discount total that is being computed by the module ot_coupon.php. (As in an order that is not completed yet but in the middle of being completed.)

I know that the value is stored in a variable called $od_amount['total']. It is also displayed on the pages checkout_payment and checkout_confirmation.

But I need to refer to that value

Here is the relevant code from ZenCart 1.5.1's ot_coupon.php. Mind you this is a part of a PHP class.

function calculate_deductions($order_total) {
global $db, $order, $messageStack, $currencies;
$currencyDecimalPlaces = $currencies->get_decimal_places($_SESSION['currency']);
$od_amount = array('tax'=>0, 'total'=>0);
if ($_SESSION['cc_id']) 
{
  $coupon = $db->Execute("select * from " . TABLE_COUPONS . " where coupon_id = '" . (int)$_SESSION['cc_id'] . "'");
  $this->coupon_code = $coupon->fields['coupon_code'];
  $orderTotalDetails = $this->get_order_total($_SESSION['cc_id']);
  if ($coupon->RecordCount() > 0 && $orderTotalDetails['orderTotal'] != 0 ) 
  {
    if (strval($orderTotalDetails['orderTotal']) >= $coupon->fields['coupon_minimum_order']) 
    {
      switch($coupon->fields['coupon_type'])
      {
        case 'S':
          $od_amount['total'] = $orderTotalDetails['shipping'];
          $od_amount['type'] = 'S';
          $od_amount['tax'] = ($this->calculate_tax == 'Standard') ? $orderTotalDetails['shippingTax'] : 0;
          if (isset($_SESSION['shipping_tax_description']) && $_SESSION['shipping_tax_description'] != '') {
            $od_amount['tax_groups'][$_SESSION['shipping_tax_description']] = $od_amount['tax'];
          }
          return $od_amount;
          break;
        case 'P':
          $od_amount['total'] = zen_round($orderTotalDetails['orderTotal']*($coupon->fields['coupon_amount']/100), $currencyDecimalPlaces);
          $od_amount['type'] = $coupon->fields['coupon_type']; 
          $ratio = $od_amount['total']/$orderTotalDetails['orderTotal'];
          break;
        case 'F':
          $od_amount['total'] = zen_round($coupon->fields['coupon_amount'] * ($orderTotalDetails['orderTotal']>0), $currencyDecimalPlaces);
          $od_amount['type'] = $coupon->fields['coupon_type']; // amount off 'F' or amount off and free shipping 'O'
          $ratio = $od_amount['total']/$orderTotalDetails['orderTotal'];
          break;
      }
      switch ($this->calculate_tax) 
      {
        case 'None':
          break;
        case 'Standard':
          if ($od_amount['total'] >= $orderTotalDetails['orderTotal']) $ratio = 1;
          foreach ($orderTotalDetails['orderTaxGroups'] as $key=>$value) 
          {
            $od_amount['tax_groups'][$key] = zen_round($orderTotalDetails['orderTaxGroups'][$key] * $ratio, $currencyDecimalPlaces);
            $od_amount['tax'] += $od_amount['tax_groups'][$key];
            if ($od_amount['tax_groups'][$key] == 0) unset($od_amount['tax_groups'][$key]);
          }
          if (DISPLAY_PRICE_WITH_TAX == 'true' && $coupon->fields['coupon_type'] == 'F') $od_amount['total'] = $od_amount['total'] + $od_amount['tax']; 
          break;
        case 'Credit Note':
          $tax_rate = zen_get_tax_rate($this->tax_class);
          $od_amount['tax'] = zen_calculate_tax($od_amount['total'], $tax_rate);
          $tax_description = zen_get_tax_description($this->tax_class);
          $od_amount['tax_groups'][$tax_description] = $od_amount['tax'];
      }
    }
  }
}


  return $od_amount;
}

Upvotes: 0

Views: 495

Answers (2)

pRose_la
pRose_la

Reputation: 305

i'm not sure why this is so difficult...

the problem i see with the code above, is that the calculate deductions function on an order total does NOT take into account product/category restrictions that may exist for the coupon.

i'm using a the local taxes add in and i used the following code to come up with the deduction:

require_once(DIR_WS_MODULES . '/order_total/ot_coupon.php');
                $ot_coupon = new ot_coupon;
                if (isset($order->info['coupon_code'])) {
                    $tax_rate = $taxrec['tax'];  //used for coupon removal....
                    $coupon_total = $ot_coupon->calculate_deductions($_SESSION['cart']->show_total());
                    $deduction_amount = ($ot_coupon->get_order_total())/(1+($coupon_total['tax']/$coupon_total['total']));
                    $coupon_tot = $ot_coupon->calculate_deductions($deduction_amount);
                    $tax_reduction_coupon = ($coupon_tot['total'] + $coupon_tot['tax']) * $tax_rate *.01;
                    $tax_total_for_class -= $tax_reduction_coupon;
                    $order->info['tax'] -= $tax_reduction_coupon;
                    $order->info['total'] -= $tax_reduction_coupon;
                }

I had to use the calculate deductions function twice; as far as the original posters question; i believe the answer is:

($coupon_tot['total'] + $coupon_tot['tax'])

of course, this could change depending on how you have the flags set in the modules option on the configuration for your store...

hope that helps.

one could also redo the whole ot_coupon calculation, which to me seemed even less elegant than my proposed solution.

Upvotes: 1

Chris Lemke
Chris Lemke

Reputation: 41

While not exactly trying to do the same, it is similar enough to what I want to be able to do, which is to calculate and adjust the freeoptions shipping module to base the free shipping on Cart Total AFTER the discounts are applied, not before.

To do that, I had to figure out a way to get the amount (of the total discount) per discount module (ot_coupon.php) and for the added mod plugin I had installed, newsletter discount (ot_newsletter_discount.php) found in the includes/modules/order_total directory.

Any other discount mods you may have installed should be located here as well. You will need to figure out what is the best method to determine if the discount is active or not. That is why I have the IF - ELSE statements.

No explanation is necessary if you understand the code below. It works. You just need to figure out where to put it and use it for your purposes. I'm on a tight schedule and when I have more time, I may expand on it further. Or not, lol.

// added this for calculating Adjusted Total AFTER Discounts to base free shipping on,
// instead of free shipping based on total BEFORE discounts!

if (isset($_SESSION['cc_id'])) {
    $coupon_total = $ot_coupon->calculate_deductions($_SESSION['cart']->show_total());
} else { $coupon_total['total'] = 0; }

if ($ot_newsletter_discount->is_subscriber()) {
    $nwsltr_total = $ot_newsletter_discount->calculate_deductions();
} else { $nwsltr_total['total'] = 0; }

$total_discounts = $coupon_total['total'] + $nwsltr_total['total'];
$adjusted_total = $_SESSION['cart']->show_total() - $total_discounts;

// echo 'Total Discounts = ' . $currencies->format($total_discounts) . '<br>';
// echo 'Adjusted Total = ' . $currencies->format($adjusted_total);

Hope this helps.

Upvotes: 1

Related Questions