コード例 #1
0
ファイル: Orders.php プロジェクト: buttasg/cowgirlk
 /**
  * getting orders for the customer filtered by given data 
  * 
  * @param $customerToken
  * @param $limit
  * @param $offset
  * @param $orderDateFrom
  * @param $sortOrder
  * @return array
  * @throws ShopgateLibraryException
  */
 public function getOrders($customerToken, $limit, $offset, $orderDateFrom, $sortOrder)
 {
     $relation = Mage::getModel('shopgate/customer')->loadByToken($customerToken);
     $response = array();
     if ($relation->getId()) {
         $sort = str_replace('created_', '', $sortOrder);
         $_orders = Mage::getModel('sales/order')->getCollection()->addFieldToSelect('*');
         if ($orderDateFrom) {
             $_orders->addFieldToFilter('created_at', array('from' => date('Y-m-d H:i:s', strtotime($orderDateFrom))));
         }
         $_orders->addFieldToFilter('customer_id', $relation->getCustomerId())->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))->setOrder('created_at', $sort);
         $_orders->getSelect()->limit($limit, $offset);
         if ($_orders->count() > 0) {
             /** @var Mage_Sales_Model_Order $order */
             foreach ($_orders as $order) {
                 /** @var Shopgate_Framework_Model_Shopgate_Order $shopgateOrder */
                 $shopgateOrder = $this->_getShopgateOrderNumber($order->getId());
                 $shopgateExternalOrder = new ShopgateExternalOrder();
                 $shopgateExternalOrder->setOrderNumber($shopgateOrder ? $shopgateOrder->getShopgateOrderNumber() : null);
                 $shopgateExternalOrder->setExternalOrderId($order->getId());
                 $shopgateExternalOrder->setExternalOrderNumber($order->getIncrementId());
                 $shopgateExternalOrder->setCreatedTime(date(DateTime::ISO8601, strtotime($order->getCreatedAt())));
                 $shopgateExternalOrder->setMail($order->getCustomerEmail());
                 $shopgateExternalOrder->setCurrency($order->getOrderCurrencyCode());
                 $shopgateExternalOrder->setPaymentMethod($order->getPayment()->getMethodInstance()->getTitle());
                 $shopgateExternalOrder->setIsPaid($shopgateOrder ? $shopgateOrder->getIsPaid() : null);
                 $shopgateExternalOrder->setPaymentTransactionNumber($this->_getPaymentTransactionNumber($order));
                 $shopgateExternalOrder->setAmountComplete($order->getGrandTotal());
                 $shopgateExternalOrder->setInvoiceAddress($this->_getShopgateAddressFromOrderAddress($order->getBillingAddress()));
                 $shopgateExternalOrder->setDeliveryAddress($this->_getShopgateAddressFromOrderAddress($order->getShippingAddress()));
                 $shopgateExternalOrder->setItems($this->_getOrderItemsFormatted($order));
                 $shopgateExternalOrder->setOrderTaxes($this->_getOrderTaxFormatted($order));
                 $shopgateExternalOrder->setDeliveryNotes($this->_getDeliveryNotes($order));
                 $shopgateExternalOrder->setExternalCoupons($this->_getCouponsFormatted($order));
                 $shopgateExternalOrder->setStatusName(ucwords(str_replace('_', ' ', $order->getStatus())));
                 $shopgateExternalOrder->setExtraCosts($this->_getExtraCost($order));
                 array_push($response, $shopgateExternalOrder);
             }
         }
         return $response;
     } else {
         throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_CUSTOMER_TOKEN_INVALID);
     }
 }