Saltar al contenido

Drupal: creación de un pedido mediante programación en Drupal Commerce para usuarios anónimos que redirigen a la página de pago

Encontramos la contestación a este inconveniente, o por lo menos eso esperamos. Si sigues con alguna duda dínoslo y sin tardar

Solución:

Puede intentar probar un nuevo módulo llamado Commerce Drush que tiene la siguiente sintaxis:

drush commerce-order-add 1
drush --user=admin commerce-order-add MY_SKU123

Solución manual

Para crear un pedido de forma programática en Commerce, puede utilizar el siguiente código (también funciona con drush, p. Ej. drush -vd -u "$1" scr order_code-7.php). Tenga en cuenta que commerce_payment_example Se requiere módulo.

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.");
  

Nota: Como se sugiere en el comentario, si tiene un error sobre el método de pago es desconocido mientras guarda el pedido, asegúrese de haberlo especificado, p. ej.

$order->data['payment_method'] = 'commerce_payment_example|commerce_payment_commerce_payment_‌​example';
commerce_order_save($order); 

Este script modificado también funciona para los usuarios anónimos:

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

Puntuaciones y comentarios

Si aceptas, tienes la libertad de dejar un tutorial acerca de qué te ha impresionado de esta crónica.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)


Tags : / /

Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *