sync code server
This commit is contained in:
		
							parent
							
								
									d8e59641f9
								
							
						
					
					
						commit
						e13aaed393
					
				|  | @ -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(), | ||||
|         ]; | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -0,0 +1,15 @@ | |||
| <?php | ||||
| namespace IpSupply\SyncOrder\Sync; | ||||
| 
 | ||||
| class Buyer implements \JsonSerializable | ||||
| { | ||||
|     public $username = ''; | ||||
|     public $fullName = ''; | ||||
|     public $primaryPhone = ''; | ||||
|     public $email = ''; | ||||
| 
 | ||||
|     public function jsonSerialize() | ||||
|     { | ||||
|         return get_object_vars($this); | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,21 @@ | |||
| <?php | ||||
| namespace IpSupply\SyncOrder\Sync; | ||||
| 
 | ||||
| class Detail implements \JsonSerializable { | ||||
|     public $productId = 0; | ||||
|     public $title = ''; | ||||
|     public $sku = ''; | ||||
|     public $condition = ''; | ||||
|     public $qty = 0; | ||||
|     public $price = 0; | ||||
|     public $amount = 0; | ||||
| 
 | ||||
|     function __construct() { | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     public function jsonSerialize() | ||||
|     { | ||||
|         return get_object_vars($this); | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,49 @@ | |||
| <?php | ||||
| namespace IpSupply\SyncOrder\Sync; | ||||
| 
 | ||||
| use GuzzleHttp\Client; | ||||
| use GuzzleHttp\Psr7\Request as GuzzRequest; | ||||
| use IpSupply\SyncOrder\Config\Getter; | ||||
| 
 | ||||
| final class Index | ||||
| { | ||||
|     protected $_config; | ||||
| 
 | ||||
|     function __construct() | ||||
|     { | ||||
|         $this->_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; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,29 @@ | |||
| <?php | ||||
| namespace IpSupply\SyncOrder\Sync; | ||||
| 
 | ||||
| class Order implements \JsonSerializable | ||||
| { | ||||
|     public $orderId = 0; | ||||
|     public $statusPayment = ''; | ||||
|     public $orderDate = ''; | ||||
|     public $subTotal = ''; | ||||
|     public $total = ''; | ||||
|     public $shippingPrice = ''; | ||||
|     public $currency = ''; | ||||
| 
 | ||||
|     function __construct(\Magento\Sales\Model\Order\Interceptor $order) | ||||
|     { | ||||
|         $this->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); | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,38 @@ | |||
| <?php | ||||
| namespace IpSupply\SyncOrder\Sync; | ||||
| 
 | ||||
| class ShippingAddress implements \JsonSerializable | ||||
| { | ||||
|     public $name = ''; | ||||
|     public $street1 = ''; | ||||
|     public $street2 = ''; | ||||
|     public $cityName = 0; | ||||
|     public $stateOrProvince = ''; | ||||
|     public $country = ''; | ||||
|     public $countryName = ''; | ||||
|     public $phone = ''; | ||||
|     public $email = ''; | ||||
|     public $postalCode = ''; | ||||
| 
 | ||||
|     function __construct($address) | ||||
|     { | ||||
|         if ($address instanceof \Magento\Sales\Model\Order\Address) { | ||||
|             $this->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); | ||||
|     } | ||||
| } | ||||
|  | @ -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());
 | ||||
|     // }
 | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue