How to add Orders to Razor Commerce Programmatically

This tutorial is over a year old and may not apply to your version of Concrete CMS.
May 2, 2015

Add Orders to Razor Commerce programmatically starts by using the Order object, namespace Razor\Core\Order\Order. The add() method will add an order to the database. Note that the order will initially have no OrderItems, so in practice we will normally be adding item(s) immediately after.

The Order::add() method returns the full Order object.

use Razor\Core\Order\Order;

/*
 * $customerID C5 User ID. For testing you can use 1 which is admin. 
 * $orderDate date in MySQL format e.g. 2015-03-30
 * $status string, use "cart" to put the order in the user cart
  */

 // $order = Order::add( $customerID, $orderDate, $status );

 $order = Order::add( 1, '2015-03-30', 'cart' );

Adding OrderItems

What's an Order without OrderItems? Well, it's perfectly valid to have an order without items and we have already created a fully-formed Order object. Adding OrderItems simply gives us a more practical example and we do need OrderItems to be able to do further work with the Order such as calculating an order total or adjusting that total with taxes and shipping.

The Order class we've already used to add the Order has the method addItem(). We'll use that method now in the example below.

/*
* $productID product id
 * $quantity quantity of this item to add to the order
* $priceEach price per quantity of the item, this should normally match the price of the product however it could be a sale price or discounted price
 */

// $order->addItem( $productID, $quantity, $priceEach );

$order->addItem( 1, 1, 7.5 );

How to get the Product ID?

There is a method getByPath() for the Product object which you can use as $product = \Razor\Core\Product\Product::getByPath( '/path/to/product' );.

This is a wrapper for the core Page::getByPath so it works the same way, the path must be preceded by a forward slash. It returns the full Product object and you can use $product->getCollectionID() to get the Product ID, which is also the collection ID.

How to get the product price?

To get the product price you need to load the Product which you can do with Razor\Core\Product\Product::getByID( $productID ) if you have the Product ID or Razor\Core\Product\Product::getByPath( 'path/to/product' ) if you don't.


To learn more about Razor Commerce the free eCommerce suite for Concrete CMS 5.7 visit http://razorcommerce.org and to download visit https://github.com/RazorCommerce/razor-commerce.

Recent Tutorials
Customize locale icons
Oct 29, 2024
By myq.

How to customize locale (language region) flags

Concrete CMS Caching Guide
Oct 16, 2024

An overview of types of caching in Concrete and considerations when using them.

Redirect all requests to HTTPS
Oct 9, 2024
By myq.

How to follow best practices for a secure web

Upgrade Concrete versions 9.3.1 and 9.3.2
Sep 10, 2024
By myq.

How to get past a bug in versions 9.3.1 and 9.3.2 that prevents upgrading the Concrete core through the Dashboard

How to use Composer with Marketplace extensions
Aug 22, 2024

Composer can be used to manage third-party extensions from the marketplace

Controlling Google Tag Manager Tags Based on Concrete CMS Edit Toolbar Visibility
Aug 13, 2024

This document provides a step-by-step guide on how to control the firing of Google Tag Manager (GTM) tags based on the visibility of the Concrete CMS edit toolbar. It explains how to create a custom JavaScript variable in GTM to detect whether the edit toolbar is present on a page and how to set up a trigger that ensures GTM tags only fire when the toolbar is not visible. This setup is particularly useful for developers and marketers who want to ensure that tracking and analytics tags are not activated during content editing sessions, thereby preserving the accuracy of data collected.

Improvements?

Let us know by posting here.