Created
October 24, 2016 09:25
-
-
Save sjelfull/2a30b0abd88ec92be2cc96c01d0dfe57 to your computer and use it in GitHub Desktop.
Creating test order in Craft Commerce
This file contains 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 | |
$customerId = 1; | |
$productId = 8; | |
$productQuantity = 5; | |
$orderStatusId = 1; | |
$paymentMethodId = 1; | |
$shippingMethodHandle = 'bring'; | |
$cart = craft()->commerce_cart->getCart(); | |
$error = ""; | |
if ( !craft()->commerce_cart->setEmail($cart, "[email protected]", $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