Dipak Kumar Pusti
Dipak Kumar Pusti

Reputation: 1723

How opencart is calculating the price using the extensions

In the cofirmation page of opencart, the price is being calculated using some extensions, saved in database having extension type "total". Here is the code that has been used there,

$total_data = array();
$total = 0;
$taxes = $this->cart->getTaxes();

$this->load->model('checkout/extension');
$sort_order = array(); 

$results = $this->model_checkout_extension->getExtensions('total');
foreach ($results as $key => $value) {
    $sort_order[$key] = $this->config->get($value['key'] . '_sort_order');
}

array_multisort($sort_order, SORT_ASC, $results);

foreach ($results as $result) {
    $this->load->model('total/' . $result['key']);
    $this->{'model_total_' . $result['key']}->getTotal($total_data, $total, $taxes);
}

$sort_order = array();

foreach ($total_data as $key => $value) {
        $sort_order[$key] = $value['sort_order'];
}

array_multisort($sort_order, SORT_ASC, $total_data);

After this calculation the variable $total_data is used to disply in the template file as the pricing detail. I am really confused how the calculation is being done. I want to add some extra data to calculation.

Upvotes: 2

Views: 1950

Answers (1)

shadyyx
shadyyx

Reputation: 16055

First, all the totals extensions are loaded, then sorted, then for each one it's corresponding price is calculated and finally the sorted values are returned. Little bit overkill I guess as instead of 3 for-each cycles maybe the code could be improved and packed into just one (didn't try, just my first guess).

Anyway, the totals are like these:

  • sub-total
  • VAT
  • coupon
  • shipping
  • total

They are displayed in their respective order (set in administration) while the total total has a sort_order of 99. All these totals are then stored with the order as a text representation, so when You change their sort_order this will be visible only with the new orders...

If You want to add Your own total, I'd recommend creating a new class for it so that it is handled by the default flow. You can start by copying, renaming and editing any of the totals You find in catalog/model/total/*.php for frontend part and

  • admin/controller/total/*.php
  • admin/view/template/total/*.tpl
  • admin/language/english/total/*.php

for the backend part...

Upvotes: 1

Related Questions