Skip to content

Instantly share code, notes, and snippets.

@sjelfull
Last active August 1, 2016 11:00
Show Gist options
  • Save sjelfull/898147a348c7b344bea4ec7873d4edfe to your computer and use it in GitHub Desktop.
Save sjelfull/898147a348c7b344bea4ec7873d4edfe to your computer and use it in GitHub Desktop.
Create a test order in Craft Commerce
<?php
public function actionCreateTestOrder ()
{
// Configuration
// Id of customer for this order
$customerId = 1;
$productId = 8;
$orderStatusId = 1;
$paymentMethodId = 1;
$shippingMethodHandle = 'bring';
// Get products
$product = craft()->commerce_products->getProductById($productId);
$customer = craft()->commerce_customers->getCustomerById($customerId);
// Get first address
$address = $customer->getAddresses()[0];
// Order item lines
$items = [ ];
// Add product
$item = new Commerce_LineItemModel();
$item->fillFromPurchasable($product->getDefaultVariant());
$items[] = $item;
// Order model
$order = new Commerce_OrderModel();
$order->customerId = $customerId;
$order->orderStatusId = $orderStatusId;
$order->paymentMethodId = $paymentMethodId;
$order->shippingMethod = $shippingMethodHandle;
$order->setLineItems($items);
$order->setShippingAddress($address);
$order->setBillingAddress($address);
// Create order
craft()->commerce_orders->saveOrder($order);
craft()->end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment