PHP API

This documentation will give you a brief overview of how you can interact with Simple Commerce in PHP & what methods are available and most useful to you. If you can't find what you're looking for in here, you might want to source-dive the Simple Commerce codebase or ask for help on GitHub.

If you've ever used Statamic's Entry facade, a lot of the syntax and methodologies are the same in Simple Commerce.

Products

You can use the Product facade to get products out of Simple Commerce.

1use DoubleThreeDigital\SimpleCommerce\Facades\Product;
2 
3$product = Product::find('id-of-product');

The facade will return an instance of the Product class which represents products internally in Simple Commerce. It includes methods for things like price, product variants, etc.

1// Get the price (returns an integer)
2$product->price();
3 
4// Set the price
5$product->price(2599);
6 
7// Get the product's variant options (returns an Illuminate Collection)
8$product->variantOptions();
9 
10// Get a specific variant (returns a ProductVariant instance)
11$product->variant('Red_Large');
12 
13// Set the product title (works the same for any Entry data)
14$product->set('title', 'Tartan Kilt');

You can use $product->resource() to get the product entry.

If you make any changes to the product, you can call the $product->save() method to save those changes. $product->delete() is also there for deleting products.

To make your own product, you can use the following syntax:

1$product = Product::make()
2 ->data([
3 'title' => 'Sporran',
4 ])
5 ->price(1599);
6 
7$product->save();

Orders

You can use the Order facade to get orders out of Simple Commerce.

1use DoubleThreeDigital\SimpleCommerce\Facades\Order;
2 
3$order = Order::find('id-of-order');

The facade will return an instance of the Order class which represents orders internally in Simple Commerce. It includes methods for things like order numbers, statuses, totals, etc.

1// Get the order number
2$order->orderNumber();
3 
4// Get the order status (returns the OrderStatus enum)
5$order->status();
6 
7// Set the order status
8$order->updateOrderStatus(OrderStatus::Placed);
9 
10// Get the payment status (returns the PaymentStatus enum)
11$order->paymentStatus();
12 
13// Set the payment status
14$order->updatePaymentStatus(PaymentStatus::Paid);
15 
16// Get the grand total (returns an integer)
17$order->grandTotal();
18 
19// Get the items total (returns an integer)
20$order->itemsTotal();
21 
22// Get the related customer (returns a Customer instance)
23$order->customer();
24 
25// Set the customer (either pass in the ID of a customer or a Customer instance)
26$order->customer($customer);
27 
28// Get the used coupon (returns a Coupon instance)
29$order->coupon();
30 
31// Set the coupon (either pass in the ID of a coupon or a Coupon instance)
32$order->coupon($coupon);
33 
34// Get the current gateway
35$order->currentGateway();
36 
37// Get the shipping address (returns an Address instance)
38$shippingAddress = $order->shippingAddress();
39 
40$shippingAddress->fullName();
41$shippingAddress->firstName();
42$shippingAddress->lastName();
43$shippingAddress->addressLine1();
44$shippingAddress->addressLine2();
45$shippingAddress->city();
46$shippingAddress->region();
47$shippingAddress->country();
48$shippingAddress->zipCode();
49$shippingAddress->toArray();
50(string) $shippingAddress;
51 
52// Redeem a coupon
53$order->redeemCoupon('coupon-code');
54 
55// Get the status log (you may optionally pass in a status as a parameter to get the timestamp)
56$order->statusLog();
57 
58// Recalculate order totals
59$order->recalculate();
60 
61// Get an order's line items (returns an Illuminate Collection)
62$lineItems = $order->lineItems();
63 
64$lineItems->first()->id();
65$lineItems->first()->product();
66$lineItems->first()->variant();
67$lineItems->first()->quantity();
68$lineItems->first()->total();
69$lineItems->first()->tax();
70$lineItems->first()->metadata();
71$lineItems->first()->toArray();
72 
73// Add a line item
74$order->addLineItem([
75 'product' => 'product-id',
76 'quantity' => 1,
77 'total' => 1500,
78]);
79 
80// Update a line item
81$order->updateLineItem($lineItemId, [
82 'quantity' => 2,
83]);
84 
85// Remove a line item
86$order->removeLineItem($lineItemId);
87 
88// Clear all line items from the order
89$order->clearLineItems();

You can use $order->resource() to get the Entry or the Eloquent model of the order.

If you make any changes to the order, you can call the $order->save() method to save those changes. $order->delete() is also there for deleting orders.

To make your own order, you can use the following syntax:

1$order = Order::make()
2 ->addLineItem([
3 'product' => 'product-id',
4 'quantity' => 1,
5 'total' => 1500,
6 ]);
7 
8$order->save();

When you ->save(), the order's totals will be automatically recalculated. You may bypass the recalculation by wrapping the operations inside the withoutRecalculating method:

1$order->withoutRecalculating(function () use (&$order) {
2 return $cart->addLineItem([
3 'product' => 'another-product-id',
4 'quantity' => 1,
5 'total' => 1000,
6 ]);
7});

Customers

You can use the Customer facade to get customers out of Simple Commerce.

1use DoubleThreeDigital\SimpleCommerce\Facades\Customer;
2 
3$customer = Customer::find('id-of-customer');

You can also use the findByEmail method on the Customer facade to find a customer by their email address:

1use DoubleThreeDigital\SimpleCommerce\Facades\Customer;
2 
3$customer = Customer::findByEmail('email@example.com');

The facade will return an instance of the Customer class which represents customers internally in Simple Commerce. It includes methods for things like price, customers variants, etc.

1// Get the customer's name
2$customer->name();
3 
4// Get the customer's email
5$customer->email();
6 
7// Get the customer's orders (returns an Illuminate Collection)
8$customer->orders();
9 
10// Set additional data on the customer
11$customer->set('is_vip_customer', true);

You can use $customer->resource() to get the customer entry/user/Eloquent model.

If you make any changes to the customer, you can call the $customer->save() method to save those changes. $customer->delete() is also there for deleting customers.

To make your own customer, you can use the following syntax:

1$customer = Customer::make()
2 ->set('name', 'Joe Smith')
3 ->email('joe.smith@example.com');
4 
5$customer->save();