Understanding Order & Cart Data Objects in Saber Commerce

As a developer integrating Saber Commerce or an extension developer building the next great SACOM extension, you’ll likely need to work with orders and carts. In Saber Commerce the way order and cart data is handled is quite different from WooCommerce and possibly other systems your familiar with.

We can summarize this different in data structure by saying that in Saber Commerce we give more priority and great standing to the “Cart Objects” than to the “Order Objects”. More specifically, cart data is stored permanently, and records are locked after checkout. We do not convert cart data into order data, instead we store a reference to the original cart in the order. At this point of course the cart is “locked” so that it cannot be altered again, thus enabling it to serve as a permanent record of the order items (products) purchased, as well as the total and any price adjustments, order cost adjustments and taxes etc.

Cart Data Structure

In Saber Commerce there are 2 models in the cart data structure.

  • CartModel
  • CartItemModel

CartModel loads CartItemModel’s in an array $items, for instance we can loop over a CartModel instance items as shown below:

$cartId = 89;
$m = new CartModel();
$cart = $m->fetchOne( $cartId );

// Check for items because an empty cart will return an empty array in $cart->items here.
if( !empty( $cart->items ) ) {
  foreach( $cart->items as $item ) {
    // Here $item is an instance of \CartItemModel.
  }
}

The decision to make Order Data “reference only” results in a simpler Order Data Structure, comprised of a single model OrderModel.

namespace: \SaberCommerce\Component\Order\OrderModel;

OrderModel’s load the full $cart (not just the cart ID) so you can access properties of the cart as shown below:

Note this example shows class initialization using the full namespace . We recommend instead that you utilize the “use” statement in your class structure, which then allows you to initialize using simply new OrderModel();

$m = new \SaberCommerce\Component\Order\OrderModel();
$order = $m->fetchOne( 345 ); // Load order ID 345.

// Get cart ID. 
$cartId = $order->cart->cartId; // Note that ID's are uniquely named in SACOM, where the ID matches the model name.

// Get all cart items in the order. 
$cartItems = $order->cart->items; // This will be an array of CartItemModel instances. 

// Get the SACOM account associated with the order. 
$account = $order->account;

// Get the WP User ID associate with the order. 
// Because WP User is not a SACOM object, it is not loaded with the model (we only load internal referenced objects automatically). Use regular WordPress user functions to load user data with the user ID. 
$userId = $order->wpUserId;

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *