Process.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. class Laybuy_Processes_CreateOrder_WC_GT_3_6_Process extends Laybuy_Processes_CreateOrder_CreateOrderAbstract
  3. {
  4. public $quoteId;
  5. public function setQuoteId($quoteId)
  6. {
  7. $this->quoteId = $quoteId;
  8. }
  9. public function getQuoteId()
  10. {
  11. return $this->quoteId;
  12. }
  13. public function execute()
  14. {
  15. $quote_id = $this->getQuoteId();
  16. WC_Laybuy_Logger::log("Creating an WC order from a quote {$quote_id}.");
  17. # Get persisted data
  18. $checkout = WC()->checkout;
  19. $data = $this->_decode(get_post_meta( $quote_id, 'posted', true ));
  20. $_POST = $this->_decode(get_post_meta( $quote_id, '$_POST', true ));
  21. $cart = $this->_decode(get_post_meta( $quote_id, 'cart', true ));
  22. $cart_hash = $this->_decode(get_post_meta( $quote_id, 'cart_hash', true ));
  23. $chosen_shipping_methods = $this->_decode(get_post_meta( $quote_id, 'chosen_shipping_methods', true ));
  24. $shipping_packages = $this->_decode(get_post_meta( $quote_id, 'shipping_packages', true ));
  25. $customer_id = $this->_decode(get_post_meta( $quote_id, 'customer_id', true ));
  26. $order_vat_exempt = $this->_decode(get_post_meta( $quote_id, 'order_vat_exempt', true ));
  27. $currency = $this->_decode(get_post_meta( $quote_id, 'currency', true ));
  28. $prices_include_tax = $this->_decode(get_post_meta( $quote_id, 'prices_include_tax', true ));
  29. $customer_ip_address = $this->_decode(get_post_meta( $quote_id, 'customer_ip_address', true ));
  30. $customer_user_agent = $this->_decode(get_post_meta( $quote_id, 'customer_user_agent', true ));
  31. $customer_note = $this->_decode(get_post_meta( $quote_id, 'customer_note', true ));
  32. $payment_method = $this->_decode(get_post_meta( $quote_id, 'payment_method', true ));
  33. $shipping_total = $this->_decode(get_post_meta( $quote_id, 'shipping_total', true ));
  34. $discount_total = $this->_decode(get_post_meta( $quote_id, 'discount_total', true ));
  35. $discount_tax = $this->_decode(get_post_meta( $quote_id, 'discount_tax', true ));
  36. $cart_tax = $this->_decode(get_post_meta( $quote_id, 'cart_tax', true ));
  37. $shipping_tax = $this->_decode(get_post_meta( $quote_id, 'shipping_tax', true ));
  38. $total = $this->_decode(get_post_meta( $quote_id, 'total', true ));
  39. try {
  40. wp_delete_post( $quote_id, true );
  41. /**
  42. * @see WC_Checkout::create_order
  43. */
  44. $order = new WC_Order();
  45. $fields_prefix = array(
  46. 'shipping' => true,
  47. 'billing' => true,
  48. );
  49. $shipping_fields = array(
  50. 'shipping_method' => true,
  51. 'shipping_total' => true,
  52. 'shipping_tax' => true,
  53. );
  54. foreach ( $data as $key => $value ) {
  55. if ( is_callable( array( $order, "set_{$key}" ) ) ) {
  56. $order->{"set_{$key}"}( $value );
  57. } elseif ( isset( $fields_prefix[ current( explode( '_', $key ) ) ] ) ) {
  58. if ( ! isset( $shipping_fields[ $key ] ) ) {
  59. $order->update_meta_data( '_' . $key, $value );
  60. }
  61. }
  62. }
  63. $order->set_created_via( 'checkout' );
  64. $order->set_cart_hash( $cart_hash );
  65. $order->set_customer_id( $customer_id );
  66. $order->add_meta_data( 'is_vat_exempt', $order_vat_exempt );
  67. $order->set_currency( $currency );
  68. $order->set_prices_include_tax( $prices_include_tax );
  69. $order->set_customer_ip_address( $customer_ip_address );
  70. $order->set_customer_user_agent( $customer_user_agent );
  71. $order->set_customer_note( $customer_note );
  72. $order->set_payment_method( $payment_method );
  73. $order->set_shipping_total( $shipping_total );
  74. $order->set_discount_total( $discount_total );
  75. $order->set_discount_tax( $discount_tax );
  76. $order->set_cart_tax( $cart_tax );
  77. $order->set_shipping_tax( $shipping_tax );
  78. $order->set_total( $total );
  79. $checkout->create_order_line_items( $order, $cart );
  80. $checkout->create_order_fee_lines( $order, $cart );
  81. $checkout->create_order_shipping_lines( $order, $chosen_shipping_methods, $shipping_packages );
  82. $checkout->create_order_tax_lines( $order, $cart );
  83. $checkout->create_order_coupon_lines( $order, $cart );
  84. # Store the Post ID in the superglobal array so we can use it in
  85. # self::filter_woocommerce_new_order_data, which is attached to the
  86. # "woocommerce_new_order_data" hook and allows us
  87. # to inject data into the `wp_insert_post` call.
  88. $GLOBALS['laybuy_quote_id'] = $quote_id;
  89. do_action( 'woocommerce_checkout_create_order', $order, $data );
  90. $order_id = $order->save();
  91. do_action( 'woocommerce_checkout_update_order_meta', $order_id, $data );
  92. # Clear globals after use, if not already cleared.
  93. if (isset($GLOBALS['laybuy_quote_id'])) {
  94. unset($GLOBALS['laybuy_quote_id']);
  95. }
  96. WC_Laybuy_Logger::log("Order created successfully from a quote {$quote_id}");
  97. return $order;
  98. } catch ( Exception $e ) {
  99. WC_Laybuy_Logger::log("Error while WC creating an order from a quote {$quote_id} " . $e->getMessage());
  100. return new WP_Error( 'checkout-error', $e->getMessage() );
  101. }
  102. }
  103. }