Skip to content

Instantly share code, notes, and snippets.

@sjelfull
Created October 24, 2016 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sjelfull/2a30b0abd88ec92be2cc96c01d0dfe57 to your computer and use it in GitHub Desktop.
Save sjelfull/2a30b0abd88ec92be2cc96c01d0dfe57 to your computer and use it in GitHub Desktop.
Creating test order in Craft Commerce
<?php
public function actionCreateTestOrder ()
{
// Configuration
$customerId = 1;
$productId = 8;
$productQuantity = 5;
$orderStatusId = 1;
$paymentMethodId = 1;
$shippingMethodHandle = 'bring';
$cart = craft()->commerce_cart->getCart();
$error = "";
if ( !craft()->commerce_cart->setEmail($cart, "example@test.com", $error) ) {
// see $error
}
$error = "";
// Get products
$product = craft()->commerce_products->getProductById($productId);
$customer = craft()->commerce_customers->getCustomerById($customerId);
// Get first address
$address = $customer->getAddresses()[0];
// Order model
$order = $cart;
$order->customerId = $customerId;
//$order->orderStatusId = $orderStatusId;
$order->paymentMethodId = $paymentMethodId;
$order->shippingMethod = $shippingMethodHandle;
//$order->setLineItems($items);
$order->setShippingAddress($address);
$order->setBillingAddress($address);
// Create order
$addedToCart = craft()->commerce_cart->addToCart($order, $product->getDefaultVariant()->id, $productQuantity, '', $options = [ ], $error);
if ( !$addedToCart ) {
die('Not added to cart');
}
else {
echo "Completing order";
// Complete order
craft()->commerce_orders->completeOrder($order);
}
craft()->end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment