From e13aaed39336f63867bdc60968b6e6a78a1b74a7 Mon Sep 17 00:00:00 2001 From: "kai.t" Date: Fri, 17 May 2024 15:18:13 +0700 Subject: [PATCH] sync code server --- .../SyncOrder/Observer/CheckOrderStatus.php | 24 ++++++--- app/code/IpSupply/SyncOrder/Sync/Buyer.php | 15 ++++++ app/code/IpSupply/SyncOrder/Sync/Detail.php | 21 ++++++++ app/code/IpSupply/SyncOrder/Sync/Index.php | 49 +++++++++++++++++++ app/code/IpSupply/SyncOrder/Sync/Order.php | 29 +++++++++++ .../SyncOrder/Sync/ShippingAddress.php | 38 ++++++++++++++ cron/orderEvent.php | 20 ++++++-- 7 files changed, 184 insertions(+), 12 deletions(-) create mode 100644 app/code/IpSupply/SyncOrder/Sync/Buyer.php create mode 100644 app/code/IpSupply/SyncOrder/Sync/Detail.php create mode 100644 app/code/IpSupply/SyncOrder/Sync/Index.php create mode 100644 app/code/IpSupply/SyncOrder/Sync/Order.php create mode 100644 app/code/IpSupply/SyncOrder/Sync/ShippingAddress.php diff --git a/app/code/IpSupply/SyncOrder/Observer/CheckOrderStatus.php b/app/code/IpSupply/SyncOrder/Observer/CheckOrderStatus.php index 7eace9bc..fffcfae1 100755 --- a/app/code/IpSupply/SyncOrder/Observer/CheckOrderStatus.php +++ b/app/code/IpSupply/SyncOrder/Observer/CheckOrderStatus.php @@ -18,18 +18,28 @@ class CheckOrderStatus implements ObserverInterface $this->dir = $dir; } - public function execute(\Magento\Framework\Event\Observer $observer) + protected function _saveLog($content) { - $order = $observer->getEvent()->getOrder(); - $customerId = $order->getCustomerId(); - $OrderStatus = $order->getStatus(); $file = $this->dir->getPath('log') . '/' . 'CheckOrderStatus.log'; + if (!file_exists($file)) { $fh = fopen($file, 'w') or die("Can't create file"); fclose($fh); } - $current = file_get_contents($file); - $current .= $customerId . ':' . $OrderStatus . " \n"; - file_put_contents($file, $current); + $currentContent = file_get_contents($file); + $currentContent .= date("Y-m-d H:i:s") . ':'; + $currentContent .= $content; + $currentContent .= "\n"; + file_put_contents($file, $currentContent); + } + + public function execute(\Magento\Framework\Event\Observer $observer) + { + $order = $observer->getEvent()->getOrder(); + + $order = [ + 'customer_id' => $order->getCustomerId(), + 'status' => $order->getStatus(), + ]; } } diff --git a/app/code/IpSupply/SyncOrder/Sync/Buyer.php b/app/code/IpSupply/SyncOrder/Sync/Buyer.php new file mode 100644 index 00000000..f7928353 --- /dev/null +++ b/app/code/IpSupply/SyncOrder/Sync/Buyer.php @@ -0,0 +1,15 @@ +_config = Getter::get('config'); + } + + protected function _headers($headers = array()) + { + return array_merge([ + 'Authorization' => 'Bearer ' . '' + ], $headers); + } + + protected function _client() { + $client = new Client([ + 'headers' => $this->_headers(), + 'base_url' => $this->_config['url'], + ]); + + return $client; + } + + public function syncOrder($order): bool + { + if ($order instanceof \Magento\Sales\Model\Order\Interceptor) { + $payload = [ + 'url' => $this->_config['url'], + 'source' => 'magento', + ...(new Order($order))->jsonSerialize() + ]; + + + return true; + } else { + throw new \ErrorException('$order is not instance \Magento\Sales\Model\Order\Interceptor'); + return false; + } + } +} diff --git a/app/code/IpSupply/SyncOrder/Sync/Order.php b/app/code/IpSupply/SyncOrder/Sync/Order.php new file mode 100644 index 00000000..2ffb7c87 --- /dev/null +++ b/app/code/IpSupply/SyncOrder/Sync/Order.php @@ -0,0 +1,29 @@ +orderId = $order->getId(); + $this->statusPayment = $order->getStatus(); + $this->orderDate = $order->getUpdatedAt(); + $this->subTotal = $order->getSubtotal(); + $this->total = $order->getTotalInvoiced(); + $this->shippingPrice = $order->getShippingInvoiced(); + $this->currency = $order->getOrderCurrencyCode(); + } + + public function jsonSerialize() + { + return get_object_vars($this); + } +} diff --git a/app/code/IpSupply/SyncOrder/Sync/ShippingAddress.php b/app/code/IpSupply/SyncOrder/Sync/ShippingAddress.php new file mode 100644 index 00000000..3988cfcb --- /dev/null +++ b/app/code/IpSupply/SyncOrder/Sync/ShippingAddress.php @@ -0,0 +1,38 @@ +email = $address->getEmail(); + $this->name = $address->getName(); + $this->street1 = $address->getStreetLine(1); + $this->street2 = $address->getStreetLine(2); + $this->country = $address->getCountryId(); + $this->cityName = $address->getCity(); + $this->phone = $address->getTelephone(); + $this->postalCode = $address->getPostcode(); + $this->countryName = $address->getCountryId(); + } else { + // do something else + } + } + + public function jsonSerialize() + { + return get_object_vars($this); + } +} diff --git a/cron/orderEvent.php b/cron/orderEvent.php index 8ab53fb0..e511492a 100755 --- a/cron/orderEvent.php +++ b/cron/orderEvent.php @@ -18,17 +18,27 @@ $objectManager->get('Magento\Framework\Registry')->register('isSecureArea', true // Handle -$orderId = 000000006; +$orderId = '00000009'; // $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); -$order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId); +$orderRepository = $objectManager->create('\Magento\Sales\Model\OrderRepository'); +if ($orderRepository instanceof \Magento\Sales\Model\OrderRepository) { + $order = $orderRepository->get($orderId); +} var_dump( get_class($order), - count($order->getItems()) ); if ($order instanceof \Magento\Sales\Model\Order\Interceptor) { - foreach ($order->getItems() as $item) { - var_dump($item->discount()); + $shippingAddress = $order->getShippingAddress(); + var_dump($order->getBillingAddress()->getCity()); + if ($shippingAddress instanceof \Magento\Sales\Model\Order\Address) { + var_dump($shippingAddress->getStreet()); } + var_dump(get_class($order->getShippingAddress())); + // var_dump($order->getCustomer()->getEmail()); + // foreach ($order->getShippingAddress() as $item) { + // // var_dump($item->getTelephone()); + // // var_dump($item->get()); + // } }