在Drupal Commerce中以编程方式为匿名用户重定向到付款页面创建订单


19

Ryan有一些很棒的代码,您可以以编程方式创建订单

<?php
global $user;
$product_id = 1;
// 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);

// Load whatever product represents the item the customer will be
// paying for and create a line item for it.
$product = commerce_product_load($product_id);
$line_item = commerce_product_line_item_new($product, 1, $order->order_id);

// 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;

// 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'].
drupal_goto('checkout/' . $order->order_id);
?>

http://www.drupalcommerce.org/questions/3259/it-possible-drupal-commerce-work-without-cart-module

我有一个要匿名捐款的网站,所以有两个问题。

  1. 如果用户未登录站点,则会收到拒绝访问消息
  2. 结帐过程会询问姓名,地址等。

我想要做的是有一个页面,您可以在其中确认金额,然后转到付款页面。在这种情况下,我使用的是PayPal WPS,因此重定向会很棒。

您可以提供的任何建议将不胜感激。


太好了,您的问题阻止了我提出问题并迷人地解决了我的问题:)
Yusef

@zhilevan感谢您发表评论,因为我可以正常工作,所以只需要提醒自己答案即可。我还要添加一下
user13134

我在另一个项目中实现了此代码,但是当root用户也没有运行它时,找不到返回页面!
2014年

找不到请求的页面“ / nashrtest / checkout / 12”。
优素福2014年

Answers:


12

您可以尝试测试名为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); 

2
Commerce Drush模块听起来像一个很棒的工具。
Francisco Luz 2014年

关于手动解决方案部分,订单电子邮件通知存在问题。付款方式为“未知”我不确定为什么,我已经使用示例付款方式进行了测试,并且为“未知”
fkaufusi 17-10-27

@fkaufusi您必须提出新的问题,然后才能检查发生了什么。
kenorb

我现在在订单电子邮件上找到了针对“未知”付款方式的解决方案。我需要先将付款方式添加到订单中,然后再保存订单。这将允许令牌系统提取付款方式并在订单电子邮件中使用。$ order-> data ['payment_method'] ='commerce_payment_example | commerce_payment_commerce_payment_example'; commerce_order_save($ order);
fkaufusi

5

此修改后的脚本也适用于匿名用户:

<?php
global $user;

$product_id = 2;
// 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);

// Link anonymous user session to the cart
if (!$user->uid) {
    commerce_cart_order_session_save($order->order_id);
}

// Load whatever product represents the item the customer will be
// paying for and create a line item for it.
$product = commerce_product_load($product_id);
$line_item = commerce_product_line_item_new($product, 1, $order->order_id);

// 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;

// 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'].
drupal_goto('checkout/' . $order->order_id);


-1

1.如果用户未登录站点,则会收到拒绝访问消息

我有一些工作要做,但我高度怀疑这是最佳实践。

最后我被骗了。在我将您的详细信息(包括电子邮件地址)放在表格上的过程中,我即时创建了一个用户帐户,然后登录该用户。如果一个电子邮件地址已准备就绪,请登录该用户。(我确保您没有使用管理员电子邮件地址)。

由于我的网站只有在您访问捐赠页面时才显示该页面,因此请确保您已注销(如果您不是管理员)。如果交易成功,它将注销您。我已关闭了订单历史记录/已将重定向放置到位,因此您只能在登录时进入我所了解的页面。没有存储任何个人详细信息,也看不到过去的捐赠

以我的情况,我对它的工作方式感到满意。这不是理想的,仅在少数情况下有效。

2.结帐过程要求输入姓名,地址等。

我去了

/ admin / commerce / config / checkout

和残疾人

  • 帐户信息
  • 账单信息
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.