3

I am working with Drupal commerce and I want to create a new order with the Rules module, when the Completing checkout process event is fired.

The second order should have all the same commerce line items the first order has.

Could anyone help me?

2

Perhaps you can do the following (not entirely sure though)

Create a rule that triggers on Checkout completion and execute custom php code.

How it works: On the event Completing the checkout process the order that is involved in this checkout process is available through the variable $commerce_order. Then we assign this order to a new variable and use it in the code provided by the function referenced below. We save the uid and profile_id from the order we want to clone so we can set the user and the billing information on the clone.

Import code of rule

{ "rules_clone_order" : {
    "LABEL" : "Clone order",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "custom" ],
    "REQUIRES" : [ "php", "rules", "commerce_checkout" ],
    "ON" : { "commerce_checkout_complete" : [] },
    "DO" : [
      { "php_eval" : { "code" : "$previous_order = $commerce_order;\r\n$uid = $previous_order-\u003Euid;\r\n$profile_id = $previous_order-\u003Ecommerce_customer_billing[LANGUAGE_NONE][0][\u0027profile_id\u0027];\r\n\r\nforeach (commerce_order_load_multiple(array(), array(\u0027status\u0027 =\u003E \u0027pending\u0027), TRUE) as $previous_order) {\r\n  $product_ids = array();\r\n  foreach (entity_metadata_wrapper(\u0027commerce_order\u0027, $previous_order)-\u003Ecommerce_line_items as $delta =\u003E $line_item_wrapper) {\r\n    if (in_array($line_item_wrapper-\u003Etype-\u003Evalue(), commerce_product_line_item_types())) {\r\n      $product_ids[] = $line_item_wrapper-\u003Ecommerce_product-\u003Eraw();\r\n    }\r\n  }\r\n}\r\n\r\n    $order = commerce_order_new ( $uid , \u0027pending\u0027 );\r\n    \/\/ Save the order so its ID is assigned.\r\n    commerce_order_save ( $order );\r\n\r\n    $order_wrapper = entity_metadata_wrapper ( \u0027commerce_order\u0027 , $order );\r\n\r\n    \/\/add products to order by ids array\r\n    foreach ( $product_ids as $product_id ) {\r\n        $product = commerce_product_load ( $product_id );\r\n\r\n        \/\/ Create a line item with quantity 1 and this product.\r\n        $line_item = commerce_product_line_item_new ( $product , 1 , $order-\u003Eorder_id );\r\n\r\n        \/\/ Save the line item to get its ID.\r\n        commerce_line_item_save ( $line_item );\r\n\r\n        \/\/ Add the line item to the order using the wrapper.\r\n        $order_wrapper-\u003Ecommerce_line_items[ ] = $line_item;\r\n    }\r\n\r\n\r\n    $profile_object = array ( \r\n        \u0027und\u0027 =\u003E array ( array ( \u0027profile_id\u0027 =\u003E $profile_id , ) , ) , ); \r\n    \r\n    \r\n    $order-\u003Ecommerce_customer_billing = $profile_object;\r\n\r\n    \/\/ Save the order.\r\n    commerce_order_save ( $order );" } }
    ]
  }
}

Credits: The code to save an order comes from this post: https://drupalcommerce.org/comment/3491#comment-3491

  • I still have an issue if you could help me again ! – Media WEB Feb 19 '17 at 20:46
  • Great, good to hear :) – undersound Feb 19 '17 at 20:46
  • I just have another question , shall I tell you that here , or post another question ? – Media WEB Feb 19 '17 at 21:04
  • Better to post another question I guess :) – undersound Feb 19 '17 at 21:13
  • Already done , I will be very very pleased if you could help me in this issue , you will save my life if you do : Link : drupal.stackexchange.com/questions/229093/… . Thank you – Media WEB Feb 19 '17 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.