No results found

Try a different search query

Popular searches:

Add to Cart

Cart

You have no purchases yet

Browse Marketplace
Original Subtotal:
Product discounts:
Quantity Discount:
Total:
View Cart
Cart, checkout and orders in code

OpenCart Checkout Code: Steps, Session Data and Order Creation in 3.0 and 4.1

How OpenCart 3.0 and 4.1 checkout works in code: step controllers, session keys, checkout/confirm, orders with status 0, and where a module should intervene.

10 min read OC 3.0 OC 4.1

The checkout code in OpenCart is a set of controllers catalog/controller/checkout/*, each responsible for its own step: customer, addresses, shipping, and payment. The steps don't write anything to the order table. They put data into the session ($this->session->data), and the order appears only in checkout/confirm, which checks the session, calculates totals, and calls model_checkout_order->addOrder(). The order status is zero at this point. The real status is set by the payment module after payment or confirmation.

Let's look at how this chain works in OpenCart 3.0 (3.0.4.1 and 3.0.5.1) and 4.1 (4.1.0.4), what changed in the session structure, and where it is safe for a module to intervene. Examples are for OpenCart 4.1; in 4.0.x, model triggers and event handling are different.

What steps make up checkout?

The checkout/checkout page is a shell for the steps, which are handled by separate controllers. In 3.0, the steps are loaded into it via AJAX requests. In 4.1, checkout/checkout embeds the step blocks via $this->load->controller(), and AJAX requests go only to the save methods and to reload blocks (for example, checkout/confirm.confirm). Each step has a method that renders the block and a save method that validates the POST and writes the data into the session. The response is JSON with or without errors, as described in the article about AJAX in OpenCart controllers.

StepOpenCart 3.0OpenCart 4.1
Login / guest / registrationcheckout/login, checkout/guest, checkout/guest_shipping, checkout/registerone checkout/register (method save) for guest and registration
Payment addresscheckout/payment_address — requiredcheckout/payment_address — only if config_checkout_payment_address is enabled
Shipping addresscheckout/shipping_addresscheckout/shipping_address
Shipping methodcheckout/shipping_method (index, save)checkout/shipping_method (quote, save)
Payment method, commentcheckout/payment_methodcheckout/payment_method (get, save, comment, agree)
Confirmationcheckout/confirmcheckout/confirm (index, confirm)

Before checkout begins, the cart checkout/cart is in effect: it shows the items, and with config_stock_checkout it decides whether to proceed without remaining stock. How item prices are calculated is described in the article about the Cart library. Also keep in mind the method separator in the route: in 3.0 checkout/shipping_method/save, in 4.1 checkout/shipping_method.save (see routing route=).

What is stored in the session between steps?

The session is the only place where an unfinished checkout lives, so it is the most often read and modified by third-party modules (one-click checkout, shipping, payment). The keys in the two branches are different.

DataOpenCart 3.0OpenCart 4.1
Customerfor a guest — guest (name, email, phone, group); for a logged-in customer it is taken from $this->customer and the databasecustomer — the same for guests and customers (customer_id, customer_group_id, name, email, phone, custom_field)
Addressespayment_address, shipping_addresspayment_address (optional), shipping_address with required address_id
Shipping options and selectedshipping_methods, shipping_methodthe same
Payment options and selectedpayment_methods, payment_method (by code)payment_methods, payment_method (code in the form cod.cod)
Othercomment, coupon, reward, voucher, voucherscomment, agree, coupon, reward
Order numberorder_idorder_id

In both branches, the shipping methodAn extension that calculates the cost: "Extensions → Extensions," type "Shipping." More in the glossary → is stored as an element quote of the selected module: shipping_methods[код]['quote'][підкод]. The option name is in the title field in 3.0 and name in 4.1, while code, cost, and tax_class_id exist in both. In 4.1, the payment methodAn extension such as card or cash payment. More in the glossary → is also two-level: payment_methods[код]['option'][підкод], and the code is stored as код.підкод. A module that fills these keys itself (for example, one-click checkout) must reproduce exactly this structure, otherwise confirm will not accept the order or will save an empty shipping name.

What does checkout/confirm do?

checkout/confirm is the heart of the process. It performs four things: it checks that the session is complete, calculates totals, builds the $order_data array, and passes it to the order model. At the end, it renders the confirmation block and embeds the controller of the selected payment module into it, which displays the payment button or form.

Checks

OpenCart 3.0 generates an $redirect on checkout/checkout or checkout/cart when any data is missing. OpenCart 4.1 checks the $status flag and without it simply does not create an order or show payment. Both branches check the minimum product quantity: 3.0 does it manually in a loop in confirm (if it is not met, a redirect to checkout/cart happens), and 4.1 does it through $this->cart->hasMinimum(). New in 4.1 are only the checks for agreeing to the terms (config_checkout_id and agree) and the "prices for logged-in customers only" mode (config_customer_price).

catalog/controller/checkout/confirm.php · OpenCart 4.1.0.4 · lines 36–39

		// Validate cart has products and has stock.
		if (!$this->cart->hasProducts() || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout')) || !$this->cart->hasMinimum()) {
			$status = false;
		}

Building $order_data

The array includes: store data (store_id, store_name, store_url), the customer, both addresses, payment and shipping methods, the comment, cart items with the calculated tax per unit (tax->getTax()), subtotals (totals), the final amount (total), affiliate and marketing data from tracking, language, currency with the exchange rate at checkout time, IP, user agent, and Accept-Language. Totals are calculated by the same chain of total modules as in the cart; how it works is described separately in the article about order totals.

The difference in the format of payment and shipping methods: OpenCart 3.0 writes the name into payment_method/shipping_method and the code into payment_code/shipping_code. OpenCart 4.1 passes the entire method array, and the model stores it as JSON in the payment_method and shipping_method columns (type TEXT). Code that reads order.payment_code in 3.0 must, in 4.1, decode payment_method and take code from it.

flowchart TD
  A["Steps: register, addresses, shipping, payment"] --> B["session->data"]
  B --> C["checkout/confirm: checks"]
  C -->|"Data incomplete"| D["No order / redirect"]
  C -->|"All present"| E["Totals + order_data"]
  E --> F["addOrder or editOrder, status 0"]
  F --> G["Payment module controller"]
  G --> H["addOrderHistory / addHistory: real status"]
  H --> I["checkout/success: clear cart and session"]

When is the order created, and where do "Missing Orders" come from?

The order is created from order_status_id = 0. By default, the order lists in the admin of both branches show only orders with a status greater than zero, while zero-status orders are available through the status filter "Missing Orders". These are customers who reached confirmation but did not complete payment, or orders for which the payment module did not trigger a status change.

Sales → Orders: zero-status orders are visible only with the «Missing Orders» status filter
Sales → Orders: zero-status orders are visible only with the «Missing Orders» status filter · OpenCart 4.1.0.4

The key difference between the versions is what happens when the confirmation step is shown again.

catalog/controller/checkout/confirm.php · OpenCart 3.0.5.1 · lines 322–324

			$this->load->model('checkout/order');

			$this->session->data['order_id'] = $this->model_checkout_order->addOrder($order_data);

catalog/controller/checkout/confirm.php · OpenCart 4.1.0.4 · lines 279–283

			if (!$order_id) {
				$this->session->data['order_id'] = $this->model_checkout_order->addOrder($order_data);
			} elseif ($order_info && !$order_info['order_status_id']) {
				$this->model_checkout_order->editOrder($order_id, $order_data);
			}

OpenCart 3.0 creates a new order every time the confirmation step is rendered: the customer returned to change shipping, and the database now has two zero-status orders with different numbers. That is why 3.0 has so many "Missing Orders" and why the numbering has gaps. OpenCart 4.1 keeps order_id in the session and, while the status is zero, updates the same order via editOrder(). If the order already has a status (payment was completed in another tab), it is not overwritten.

How does the order get a status, and what does checkout/success do?

The payment module changes the zero status. For simple methods (cash on deliveryPayment upon receipt. It increases the risk of parcel refusals. More in the glossary →, bank transfer), its controller calls addOrderHistory() in 3.0 or addHistory() in 4.1 on the "Confirm" button, with the status from the module settings, for example payment_cod_order_status_id. For online payments, the status is set by the callback from the payment systemA service that accepts cards: LiqPay, WayForPay, Monobank, and others. The commission depends on the contract. More in the glossary →. This is when stock is deducted, coupons and bonuses are confirmed, and the email is sent. Details are in the articles on addOrderHistory and payment modules, and typical payment problems are described in the article order status did not change after payment.

After that, the customer is redirected to checkout/success. If the session contains order_id, the controller clears the cart (cart->clear()) and removes from the session the order number, shipping and payment methods, comment, coupon, and bonuses. In 3.0 it also removes guest, voucher, vouchers, and totals; in 4.1, agree. The important consequence is this: if the customer did not return to success after payment (they closed the tab on the bank's page), the cart will remain full even though the order is paid. Payment modules that want to avoid this clear the cart themselves or redirect to success even from the callback page.

Where should a module intervene in checkout?

Here are the points that work without editing core files, from the safest:

  • Additional customer and address fields — standard Custom FieldsYour own fields in registration or address, for example a tax ID number or a post office branch. More in the glossary →. They already pass through the session and end up in the custom_field, payment_custom_field, and shipping_custom_field of the order.
  • Changing amounts — your own totals module, not editing confirm.
  • Custom order data (branch number, delivery time) — save it to the session at your step, and after the order is created, write it to your own table keyed by order_id through the after event on addOrder and editOrder. Do not add columns to the order table: they are easily lost when OpenCart is updated. For how to design such a table, see the article custom module tables.
  • Reacting to "order placed" — use the status change event, not addOrder, because an order with status 0 is not yet placed.

Example: event after order creation · OpenCart 4.1

// Тригер: catalog/model/checkout/order.addOrder/after
// (у 3.0: catalog/model/checkout/order/addOrder/after)
public function saveDeliveryData(string &$route, array &$args, mixed &$output): void {
	$order_id = (int)$output;

	if (!$order_id || empty($this->session->data['mymodule_delivery'])) {
		return;
	}

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

	$this->model_extension_mymodule_delivery->save($order_id, $this->session->data['mymodule_delivery']);
}

In 4.1, repeated confirmation goes through editOrder(), so register a second event on catalog/model/checkout/order.editOrder/after and take the number from $args[0]. In 4.1, the save() method of your model must update the record by order_id (insert or update), because editOrder/after is called repeatedly for the same order. In 3.0, every display of the confirmation step produces a new order_id, so your table will also keep records for abandoned zero-status orders. How to register and order events is covered in the articles on addEvent and model events.

Summary

  • Checkout steps only fill the session; the order is created by checkout/confirm through addOrder().
  • 4.1 merged guest and registration into checkout/register and the session key customer; the payment address became optional (config_checkout_payment_address).
  • In 4.1, payment and shipping methods are stored in the order as a JSON array; in 3.0, as a name plus a separate code.
  • A new order has status 0 and is visible in "Missing Orders". 3.0 creates a new zero-status order each time confirmation is shown, while 4.1 updates the same one.
  • The real status is set by the payment module, and the cart and session are cleared by checkout/success.
  • A module is better off intervening through additional fields, total modules, and after events on the order model, rather than by editing controllers.

Next, it makes sense to look at what exactly happens when the order status changes. All articles in the section are on the page Cart, checkout, and orders in code.

FAQ

Is it safe to delete Missing Orders?
Zero-status orders don't affect stock, reports or coupons, because these actions happen only when an order moves to the processing status. Before deleting them, make sure none of them are paid orders where the payment gateway callback failed.
How do you make a one-step checkout without changing the core?
You need a custom controller that collects the customer's details, address and shipping/payment methods into the same session structure as the standard steps, then calls checkout/confirm. The key is to reproduce the shipping_method and payment_method format for your version.
Why doesn't the order in 3.0 have a language code, unlike 4.1?
In 3.0 the order only stores language_id. In 4.1, checkout/confirm also writes language_code from config_language, so a 4.1 module doesn't need to look up the language code by its id.
Where is the comment the customer chose for the order stored?
Between steps it's stored in session->data['comment'], and after confirmation it's in the comment column of the order table. Comments on status changes are written separately to order_history.
Was this article helpful?
Thanks for your feedback!

Related articles

Didn't find the answer?

Ask the community: OpenCart developers answer on the forum.

Ask on the forum
We use cookies

We use cookies and similar technologies to improve your experience, analyse traffic, and show personalised ads. Read our Cookie Policy for details.