Example #1
0
 /**
  * Get All customers from catalog
  *
  * @param int|null $mage_store_id
  * @param string|null $updated_at_min
  * @param string|null $updated_at_max
  * @param int|null $limit
  * @param int|null $page
  * @param int|null $since_id
  * @param string|null $created_at_min
  * @param string|null $created_at_max
  * @param string|null $order_status
  * @param int|null $order_id
  * @return array $orders
  */
 public function getOrders($mage_store_id, $updated_at_min = null, $updated_at_max = null, $limit = null, $page = null, $since_id = null, $created_at_min = null, $created_at_max = null, $order_status = null, $order_id = null)
 {
     $pageNumber = null;
     $pageSize = null;
     $orders = $this->_salesOrderResourceCollectionFactory->create();
     $orders->addFieldToFilter('main_table.store_id', array('eq' => $mage_store_id));
     if ($updated_at_min != null) {
         $orders->addAttributeToFilter('main_table.updated_at', array('gt' => $updated_at_min));
     }
     if ($updated_at_max != null) {
         $orders->addAttributeToFilter('main_table.updated_at', array('lt' => $updated_at_max));
     }
     if ($since_id != null) {
         $orders->addAttributeToFilter('main_table.entity_id', array('gt' => $since_id));
     }
     if ($created_at_min != null) {
         $orders->addAttributeToFilter('main_table.created_at', array('gt' => $created_at_min));
     }
     if ($created_at_max != null) {
         $orders->addAttributeToFilter('main_table.created_at', array('lt' => $created_at_max));
     }
     if ($order_status != null) {
         $orders->addAttributeToFilter('main_table.status', $order_status);
     }
     if ($order_id != null) {
         $orders->addAttributeToFilter('main_table.entity_id', $order_id);
     }
     if ($limit != null) {
         $pageNumber = 1;
         // Note that page numbers begin at 1
         $pageSize = $limit;
     }
     if ($page != null) {
         if (!is_null($pageSize)) {
             $pageNumber = $page + 1;
             // Note that page numbers begin at 1
         }
     }
     if (!is_null($pageSize)) {
         $orders->setPage($pageNumber, $pageSize);
     }
     $map = $this->response_mask;
     $ordersArray = [];
     foreach ($orders as $order) {
         $items = $order->getAllItems();
         $ord = [];
         $orderDetails = $order->getData();
         foreach ($map['orders'] as $element => $value) {
             if (!is_array($value)) {
                 if ($value == 'coupon_code') {
                     if ($orderDetails['coupon_code']) {
                         $ord['discount_codes'] = [['code' => $orderDetails['coupon_code'], 'amount' => isset($orderDetails['discount_amount']) ? $orderDetails['discount_amount'] : 0]];
                     } else {
                         $ord['discount_codes'] = [];
                     }
                 } elseif (array_key_exists($value, $orderDetails)) {
                     $ord[$element] = $orderDetails[$value];
                 }
             }
         }
         if ($order->getCustomerId()) {
             $ord['customer'] = $this->getCustomerDataById($order->getCustomerId());
         } else {
             $billingAddressData = $this->getGuestBillingAddressData($order->getBillingAddressId());
             $ord['customer']['email'] = $billingAddressData->getEmail();
             $ord['customer']['first_name'] = $billingAddressData->getFirstname();
             $ord['customer']['last_name'] = $billingAddressData->getLastname();
             $ord['customer']['title'] = $billingAddressData->getPrefix();
             $ord['customer']['default_address']['country'] = $billingAddressData->getCountryId();
             $ord['customer']['default_address']['country_code'] = $billingAddressData->getCountryId();
             $ord['customer']['default_address']['province_code'] = $billingAddressData->getRegionId();
             $ord['customer']['default_address']['zip'] = $billingAddressData->getPostcode();
             $ord['customer']['default_address']['phone'] = $billingAddressData->getTelephone();
         }
         $ord['line_items'] = [];
         foreach ($items as $item) {
             $newItem = [];
             $itemElement = $item->getData();
             foreach ($map['orders']['line_items'] as $element => $value) {
                 if (!is_array($value)) {
                     if (array_key_exists($value, $itemElement)) {
                         $newItem[$element] = $itemElement[$value];
                     }
                 }
             }
             $ord['line_items'][] = $newItem;
         }
         $ord['status'] = $this->getStoreOrderStatusesByCode($orderDetails['status']);
         $ordersArray[] = $ord;
     }
     $object = new DataObject();
     $object->setOrders($ordersArray);
     return $object;
 }