Post by twist360 » Fri Jun 13, 2014 11:54 pm

Hi Everyone,

I am helping a client out by installing some tracking on their order successful process.

We have managed to get the tracking installed and access order_number & total_amount via the success.php & success.tpl

We are using version 1.5.4

What the client would now like to do is manipulate this code based on the number of times the customer has ordered. Obviously this would only apply to customers logged in (not guest orders).

What I would like to know is if we can get the total number of past orders for a customer on the success.php process.

This will allow me to use some php to send different values based on this integer.

Thanks for all your help in advanced.

Newbie

Posts

Joined
Fri Jun 13, 2014 11:49 pm

Post by straightlight » Thu Jun 19, 2014 7:19 pm

In catalog/controller/checkout/success.php file,

find:

Code: Select all

if (isset($this->session->data['order_id'])) {
add right below:

Code: Select all

if ($this->customer->isLogged()) {
    $total_orders = $this->db->query("SELECT COUNT(`order_id`) AS `total` FROM `" . DB_PREFIX . "order` WHERE `order_id` = '" . (int)$this->session->data['order_id'] . "' AND `customer_id` = '" . (int)$this->customer->getId() . "'");

    $this->data['total_orders'] = (int)$total_orders->row['total'];
}
Then, you could use the $total_orders variable into your custom success.tpl template file.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by Dhaupin » Thu Oct 02, 2014 1:16 am

Late reply, but since we are also messing with this on the admin side, here is a thought.

This may work different catalog side, but the post above seems to get orders without considering order status 0 which is "missing orders". So if a customer doesnt complete an order twice, then checks out successfully, the count may show 3 orders when you only want it to show the 1 successful (not the other 2 missing orders of status 0).


If you need order counts on admin side, here is a snippet. In admin/model/sale/order.php you can make a new function to get total orders by customer id:

Code: Select all

	public function getTotalOrdersByCustomerId($customer_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` WHERE order_status_id > 0 AND customer_id = '" . (int)$customer_id . "'");

		return $query->row['total'];
	}

Now anything that uses the order model is able to quickly pull a count of a customers orders with status greater than 0. To use it, it would look like this (replace customer_id variable with whatever makes customer id in your controller/view):

Code: Select all

$this->model_sale_order->getTotalOrdersByCustomerId($customer_id)
Keep in mind, there is a customer id of 0 that is sometimes used if customer is deleted. So its a good idea to check against that too, otherwise it may show inaccurate counts for deleted customers :)

https://creadev.org | support@creadev.org - Opencart Extensions, Integrations, & Development. Made in the USA.


User avatar
Active Member

Posts

Joined
Tue May 13, 2014 3:45 am
Location - PA

Post by 3PO2017 » Tue Feb 25, 2020 11:33 pm

straightlight wrote:
Thu Jun 19, 2014 7:19 pm
In catalog/controller/checkout/success.php file,

find:

Code: Select all

if (isset($this->session->data['order_id'])) {
add right below:

Code: Select all

if ($this->customer->isLogged()) {
    $total_orders = $this->db->query("SELECT COUNT(`order_id`) AS `total` FROM `" . DB_PREFIX . "order` WHERE `order_id` = '" . (int)$this->session->data['order_id'] . "' AND `customer_id` = '" . (int)$this->customer->getId() . "'");

    $this->data['total_orders'] = (int)$total_orders->row['total'];
}
Then, you could use the $total_orders variable into your custom success.tpl template file.
Hi,
I'm trying to get the total number of the orders to display on user account page but its not working.

OC 3.x
its shows error ParseError: syntax error, unexpected 'public' (T_PUBLIC)

code that i place in controller/account/account.php

if ($this->customer->isLogged()) {
$total_orders = $this->db->query("SELECT COUNT(`order_id`) AS `total` FROM `" . DB_PREFIX . "order` WHERE `order_id` = '" . (int)$this->session->data['order_id'] . "' AND `customer_id` = '" . (int)$this->customer->getId() . "'");
$data['total_orders'] = (int)$total_orders->row['total'];
}

and
{{ total_orders in }} theme account.twig file.

New member

Posts

Joined
Fri Feb 24, 2017 10:27 pm

Post by straightlight » Wed Feb 26, 2020 1:33 am

OC 3.x
its shows error ParseError: syntax error, unexpected 'public' (T_PUBLIC)
Replace:

Code: Select all

$this->data
with:

Code: Select all

$data
replace:

Code: Select all

{{ total_orders in }}
with:

Code: Select all

{{ total_orders }}

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 118 guests