Last active
August 1, 2016 11:00
-
-
Save sjelfull/898147a348c7b344bea4ec7873d4edfe to your computer and use it in GitHub Desktop.
Create a test order in Craft Commerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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