public function showOrderDetails($id) { // init model $ordersModel = new WPLA_OrdersModel(); // get amazon_order record $amazon_order = $ordersModel->getItem($id); // get WooCommerce order $wc_order_notes = $amazon_order['post_id'] ? $this->get_order_notes($amazon_order['post_id']) : false; $aData = array('amazon_order' => $amazon_order, 'wc_order_notes' => $wc_order_notes); $this->display('order_details', $aData); }
public function importOrder($order, $account) { global $wpdb; $table = $wpdb->prefix . self::TABLENAME; // skip processing if requests are throttled already if ($this->throttling_is_active == true) { return false; } // if ( ! is_object($order) ) // echo "<pre>order is not an object: ";print_r($order);echo"</pre>";die(); // check if order exists in WPLA and is already up to date (TODO: optimize) if ($id = $this->order_id_exists($order->AmazonOrderId)) { $om = new WPLA_OrdersModel(); $amazon_order = $om->getItem($id); if ($amazon_order['LastTimeModified'] == $this->convertIsoDateToSql($order->LastUpdateDate)) { WPLA()->logger->info('Order ' . $order->AmazonOrderId . ' has not been modified since ' . $amazon_order['LastTimeModified'] . ' and is up to date.'); wpla_show_message('Order ' . $order->AmazonOrderId . ' has not been modified since ' . $amazon_order['LastTimeModified'] . ' and is up to date.'); return null; } } $data = array('order_id' => $order->AmazonOrderId, 'status' => $order->OrderStatus, 'total' => isset($order->OrderTotal->Amount) ? $order->OrderTotal->Amount : '', 'currency' => isset($order->OrderTotal->CurrencyCode) ? $order->OrderTotal->CurrencyCode : '', 'buyer_name' => isset($order->BuyerName) ? $order->BuyerName : '', 'buyer_email' => isset($order->BuyerEmail) ? $order->BuyerEmail : '', 'PaymentMethod' => isset($order->PaymentMethod) ? $order->PaymentMethod : '', 'ShippingAddress_City' => isset($order->ShippingAddress->City) ? $order->ShippingAddress->City : '', 'date_created' => $this->convertIsoDateToSql($order->PurchaseDate), 'LastTimeModified' => $this->convertIsoDateToSql($order->LastUpdateDate), 'account_id' => $account->id, 'details' => json_encode($order)); // fetch order line items from Amazon - required for both new and updated orders $this->api = new WPLA_AmazonAPI($account->id); $items = $this->api->getOrderLineItems($order->AmazonOrderId); $data['items'] = maybe_serialize($items); // check if ListOrderItems request is throttled // if true, skip ALL further requests / order processing until next cron run if (is_object($items) && isset($items->Error->Message)) { $this->throttling_is_active = true; wpla_show_message('ListOrderItems requests are throttled. Skipping further order processing until next run.', 'warn'); return false; } // check if order exists in WPLA if ($id = $this->order_id_exists($order->AmazonOrderId)) { // load existing order record from wp_amazon_orders $ordersModel = new WPLA_OrdersModel(); $wpla_order = $ordersModel->getItem($id); // check if order status was updated // if pending -> Canceled: revert stock reduction by processing history records // if pending -> Shipped / Unshipped: create WooCommerce order if enabled (done in createOrUpdateWooCommerceOrder()) if ($order->OrderStatus != $wpla_order['status']) { $old_order_status = $wpla_order['status']; $new_order_status = $order->OrderStatus; // add history record $history_message = "Order status has changed from " . $old_order_status . " to " . $new_order_status; $history_details = array('id' => $id, 'new_status' => $new_order_status, 'old_status' => $old_order_status, 'LastTimeModified' => $data['LastTimeModified']); $this->addHistory($data['order_id'], 'order_status_changed', $history_message, $history_details); // if pending -> Canceled: revert stock reduction by processing history records if ($old_order_status == 'Pending' && $new_order_status == 'Canceled') { // revert stock reduction $this->revertStockReduction($wpla_order); // add history record $history_message = "Stock levels have been replenished"; $history_details = array('id' => $id); $this->addHistory($data['order_id'], 'revert_stock', $history_message, $history_details); } } // if status changed // update existing order $wpdb->update($table, $data, array('order_id' => $order->AmazonOrderId)); $this->updated_count++; // TODO: update WooCommerce order! // add history record $history_message = "Order details were updated - " . $data['LastTimeModified']; $history_details = array('id' => $id, 'status' => $data['status'], 'LastTimeModified' => $data['LastTimeModified']); $this->addHistory($data['order_id'], 'order_updated', $history_message, $history_details); } else { // insert new order $wpdb->insert($table, $data); $this->imported_count++; $id = $wpdb->insert_id; echo $wpdb->last_error; // add history record $history_message = "Order was added with status: " . $data['status']; $history_details = array('id' => $id, 'status' => $data['status'], 'LastTimeModified' => $data['LastTimeModified']); $this->addHistory($data['order_id'], 'order_inserted', $history_message, $history_details); // process ordered items - unless order has been cancelled if ($data['status'] != 'Canceled') { foreach ($items as $item) { // process each item and reduce stock level $success = $this->processListingItem($item, $order); } } } // if order does not exist return $id; }