/** * 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); } }
public function getOrders($customerToken, $customerLanguage, $limit, $offset, $orderDateFrom, $sortOrder) { $shopgateCustomerModel = new ShopgateCustomerPrestashop(); $this->_currentCustomer = $shopgateCustomerModel->getCustomerByToken($customerToken); if (!$this->_currentCustomer->validateFields(false)) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_CUSTOMER_TOKEN_INVALID); } $orders = $this->getCustomerOrders($this->_currentCustomer->id, $limit, $offset, $orderDateFrom, $sortOrder); foreach ($orders as $orderItem) { /** @var OrderCore $orderCore */ $orderCore = new Order($orderItem['id_order']); $order = new ShopgateExternalOrder(); $order->setOrderNumber($orderCore->id); $order->setExternalOrderNumber($orderCore->reference); $order->setExternalOrderId($orderCore->id); /** @var OrderStateCore $orderStatus */ $orderStatus = new OrderState($orderCore->getCurrentState()); $order->setStatusName($orderStatus->name[$this->getPlugin()->getLanguageId()]); $order->setStatusColor($orderStatus->color); $order->setCreatedTime($orderCore->date_add); $order->setMail($this->_currentCustomer->email); $order->setDeliveryAddress($this->_getAddress($orderCore->id_address_delivery, ShopgateCustomerPrestashop::DEFAULT_CUSTOMER_ADDRESS_IDENTIFIER_DELIVERY)); $order->setInvoiceAddress($this->_getAddress($orderCore->id_address_invoice, ShopgateCustomerPrestashop::DEFAULT_CUSTOMER_ADDRESS_IDENTIFIER_DELIVERY)); $order->setItems($this->_getOrderItems($orderCore)); $order->setExternalCoupons($this->_getCartRules($orderCore)); /** @var CurrencyCore $currency */ $currency = Currency::getCurrency($orderCore->id_currency); $order->setCurrency($currency['iso_code']); $order->setAmountComplete(isset($orderCore->total_paid_tax_incl) ? $orderCore->total_paid_tax_incl : $orderCore->total_paid); $order->setIsPaid($orderCore->hasBeenPaid()); $order->setPaymentMethod($orderCore->payment); $order->setIsShippingCompleted($orderCore->hasBeenShipped()); $order->setShippingCompletedTime($orderCore->hasBeenShipped() ? $orderCore->delivery_date : null); $order->setDeliveryNotes($this->_getDeliveryNotes($orderCore)); $order->setExtraCosts($this->_getExtraCost($orderCore)); $order->setOrderTaxes($this->_getOrderTaxes()); $this->_result[] = $order; } return $this->_result; }