다음 구문을 가진 Commerce Drush 라는 새 모듈을 테스트 할 수 있습니다 .
drush commerce-order-add 1
drush --user=admin commerce-order-add MY_SKU123
수동 솔루션
Commerce에서 프로그래밍 방식으로 주문을 작성하려면 다음 코드를 사용할 수 있습니다 (예 : drush에서도 작동 drush -vd -u "$1" scr order_code-7.php
). 주의하시기 바랍니다 commerce_payment_example
모듈이 필요합니다.
<?php
if (!function_exists('drush_print')) {
function drush_print ($text) {
print $text . "\n";
}
}
$is_cli = php_sapi_name() === 'cli';
global $user;
// Add the product to the cart
$product_id = 5;
$quantity = 1;
if ($is_cli) {
drush_print('Creating new order for ' . $quantity . ' item(s) of product ' . $product_id . '...');
}
// Create the new order in checkout; you might also check first to
// see if your user already has an order to use instead of a new one.
$order = commerce_order_new($user->uid, 'checkout_checkout');
// Save the order to get its ID.
commerce_order_save($order);
if ($is_cli) {
drush_print('Order created. Commerce order id is now ' . $order->order_id);
drush_print('Searching product ' . $product_id . ' in a Commerce system...');
}
// Load whatever product represents the item the customer will be
// paying for and create a line item for it.
$product = commerce_product_load((int)$product_id);
if((empty($product->product_id)) || (!$product->status)){
if ($is_cli) {
drush_print(' Cannot match given product id with a Commerce product id.');
}
drupal_set_message(t('Invalid product id'));
drupal_goto(); // frontpage
return FALSE;
}
if ($is_cli) {
drush_print(' Found a Commerce product ' . $product->product_id . '.');
}
// Create new line item based on selected product
$line_item = commerce_product_line_item_new($product, 1, $order->order_id);
if ($is_cli) {
drush_print(' Added product to the cart.');
}
// Save the line item to get its ID.
commerce_line_item_save($line_item);
// Add the line item to the order using fago's rockin' wrapper.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper->commerce_line_items[] = $line_item;
if ($is_cli) {
drush_print('Saving order...');
}
// Save the order again to update its line item reference field.
commerce_order_save($order);
// Redirect to the order's checkout form. Obviously, if this were a
// form submit handler, you'd just set $form_state['redirect'].
if ($is_cli) {
drush_print('Checking out the order...');
}
commerce_checkout_complete($order);
if ($is_cli) {
drush_print('Marking order as fully paid...');
}
$payment_method = commerce_payment_method_instance_load('commerce_payment_example|commerce_payment_commerce_payment_example');
if (!$payment_method) {
if ($is_cli) {
drush_print(" No example payment method found, we can't mark order as fully paid. Please enable commerce_payment_example module to use this feature.");
}
}
else {
if ($is_cli) {
drush_print(" Creating example transaction...");
}
// Creating new transaction via commerce_payment_example module.
$charge = $order->commerce_order_total['und'][0];
$transaction = commerce_payment_transaction_new('commerce_payment_example', $order->order_id);
$transaction->instance_id = $payment_method['instance_id'];
$transaction->amount = $charge['amount'];
$transaction->currency_code = $charge['currency_code'];
$transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
$transaction->message = 'Name: @name';
$transaction->message_variables = array('@name' => 'Example payment');
if ($is_cli) {
drush_print(" Notifying Commerce about new transaction...");
}
commerce_payment_transaction_save($transaction);
commerce_payment_commerce_payment_transaction_insert($transaction);
}
if ($is_cli) {
drush_print("Marking order as completed...");
}
commerce_order_status_update($order, 'completed');
if ($is_cli) {
drush_print("\nDone.");
}
참고 : 의견에서 제안한 대로 주문을 저장하는 동안 결제 수단에 대한 오류를 알 수없는 경우 주문 방법을 지정했는지 확인하십시오.
$order->data['payment_method'] = 'commerce_payment_example|commerce_payment_commerce_payment_example';
commerce_order_save($order);