Opencart: cart api getTotal() wrong value

Created on 2 Feb 2017  Â·  4Comments  Â·  Source: opencart/opencart

Hi,

the getTotal() function in system/library/cart/cart.php returns the wrong value. It returns the value _without_ shipping costs. If taxes are zero getSubTotal() and getTotal() return the same value.

To reproduce this:
echo($this->cart->getTotal());

Found this while porting an old payment plugin.

Opencart version: 2.3.0.2

Most helpful comment

// Totals
$this->load->model('extension/extension');

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

// Because __call can not keep var references so we put them into an array.             
$total_data = array(
    'totals' => &$totals,
    'taxes'  => &$taxes,
    'total'  => &$total
);

// Display prices
if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
    $sort_order = array();

    $results = $this->model_extension_extension->getExtensions('total');

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

    array_multisort($sort_order, SORT_ASC, $results);

    foreach ($results as $result) {
        if ($this->config->get($result['code'] . '_status')) {
            $this->load->model('extension/total/' . $result['code']);

            // We have to put the totals in an array so that they pass by reference.
            $this->{'model_extension_total_' . $result['code']}->getTotal($total_data);
        }
    }

    $sort_order = array();

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

    array_multisort($sort_order, SORT_ASC, $totals);
}

$data['total'] = $this->currency->format($total, $this->session->data['currency']);

All 4 comments

And getTotal() dosen't include coupon values.
There is no function in the cart.php api to calculate the real total value

@gj12 if u had done ur research properly you will know coupon discount comes before tax.

But if I put stuff worth 1€ in the shopping cart and add one -0,20€ coupon, then getTotal returns 1€ not -0,80€. I need the total value that the customer is going to pay, there is no function in the cart API to do that, they all return the useless 1€ Value without coupon and shipping.

I found that there are some extension/total files are they now used instead of this cart API?

// Totals
$this->load->model('extension/extension');

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

// Because __call can not keep var references so we put them into an array.             
$total_data = array(
    'totals' => &$totals,
    'taxes'  => &$taxes,
    'total'  => &$total
);

// Display prices
if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
    $sort_order = array();

    $results = $this->model_extension_extension->getExtensions('total');

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

    array_multisort($sort_order, SORT_ASC, $results);

    foreach ($results as $result) {
        if ($this->config->get($result['code'] . '_status')) {
            $this->load->model('extension/total/' . $result['code']);

            // We have to put the totals in an array so that they pass by reference.
            $this->{'model_extension_total_' . $result['code']}->getTotal($total_data);
        }
    }

    $sort_order = array();

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

    array_multisort($sort_order, SORT_ASC, $totals);
}

$data['total'] = $this->currency->format($total, $this->session->data['currency']);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

kurdi999 picture kurdi999  Â·  4Comments

VistaXP picture VistaXP  Â·  5Comments

RajatJain4061 picture RajatJain4061  Â·  5Comments

tarranjones picture tarranjones  Â·  6Comments

RadhikaMayani picture RadhikaMayani  Â·  3Comments