private function serializeToArray(CustomerOrder $order) { $array = array(); $array['ID'] = $order->getID(); $array['totalAmount'] = $order->totalAmount->get(); $array['isCancelled'] = (int) $order->isCancelled->get(); $array['status'] = (int) $order->status->get(); $array['shipments'] = array(); foreach ($order->getShipments() as $shipment) { $shippingServiceArray = null; if ($shipment->shippingService->get() && (int) $shipment->shippingService->get()->getID()) { $shippingServiceArray = $shipment->shippingService->get()->toArray(); } else { $shippingService = unserialize($shipment->shippingServiceData->get()); $shippingServiceArray = null; if (is_object($shippingService)) { $shippingService->setApplication($order->getApplication()); $shippingServiceArray = $shippingService->toArray(); } } $array['shipments'][$shipment->getID()] = array('ID' => $shipment->getID(), 'status' => (int) $shipment->status->get(), 'ShippingService' => $shippingServiceArray); } $array['items'] = array(); foreach ($order->getOrderedItems() as $item) { $array['items'][$item->getID()] = array('ID' => $item->getID(), 'shipmentID' => $item->shipment->get() ? $item->shipment->get()->getID() : null, 'count' => (int) $item->count->get(), 'price' => $item->price->get(), 'Product' => array('ID' => (int) $item->getProduct()->getID(), 'sku' => $item->getProduct()->sku->get(), 'name' => $item->getProduct()->getName($order->getApplication()->getDefaultLanguageCode()), 'isDownloadable' => (int) $item->getProduct()->isDownloadable()), 'Shipment' => $item->shipment->get() && isset($array['shipments'][$item->shipment->get()->getID()]) ? $array['shipments'][$item->shipment->get()->getID()] : array('ID' => 0)); } // @todo: dirty fix if ($order->shippingAddress->get()) { if ($order->shippingAddress->get()->state->get()) { $order->shippingAddress->get()->state->get()->load(); } $array['ShippingAddress'] = $order->shippingAddress->get()->toArray(); } else { $array['ShippingAddress'] = array(); } if ($order->billingAddress->get()) { if ($order->billingAddress->get()->state->get()) { $order->billingAddress->get()->state->get()->load(); } $array['BillingAddress'] = $order->billingAddress->get()->toArray(); } else { $array['BillingAddress'] = array(); } return $array; }
/** * Update product quantities */ public function update() { // TOS if ($this->isTosInCartPage()) { $this->session->set('tos', $this->request->get('tos')); } // coupon code if ($this->request->get('coupon')) { $code = $this->request->get('coupon'); if ($condition = DiscountCondition::getInstanceByCoupon($code)) { if (!$this->order->hasCoupon($code)) { $coupon = OrderCoupon::getNewInstance($this->order, $code); $coupon->save(); $this->order->getCoupons(true); if ($this->order->hasCoupon($code)) { $this->setMessage($this->makeText('_coupon_added', array($code))); } } } else { $this->setErrorMessage($this->makeText('_coupon_not_found', array($code))); } $this->order->getCoupons(true); } $this->updateEstimateAddress(); $this->order->loadItemData(); $validator = $this->buildCartValidator($this->order, $this->getItemOptions()); if (!$validator->isValid()) { return new ActionRedirectResponse('order', 'index'); } $this->order->loadRequestData($this->request); foreach ($this->order->getOrderedItems() as $item) { if ($this->request->isValueSet('item_' . $item->getID())) { foreach ($item->getProduct()->getOptions(true) as $option) { $this->modifyItemOption($item, $option, $this->request, $this->getFormFieldName($item, $option)); } $item->save(); $this->order->updateCount($item, $this->request->get('item_' . $item->getID(), 0)); } } if ($this->order->isMultiAddress->get()) { $addresses = $this->user->getShippingAddressSet(); $this->order->getShipments(); foreach ($this->order->getOrderedItems() as $item) { if ($addressId = $this->request->get('address_' . $item->getID())) { if (!$item->shipment->get() || !$item->shipment->get()->shippingAddress->get() || $item->shipment->get()->shippingAddress->get()->getID() != $addressId) { foreach ($this->order->getShipments() as $shipment) { if ($shipment->shippingAddress->get() && $shipment->shippingAddress->get()->getID() == $addressId) { if (!$item->shipment->get() || $item->shipment->get()->getID() != $shipment->getID()) { if ($item->shipment->get()) { $item->shipment->get()->removeItem($item); } $shipment->addItem($item); break; } } $shipment = null; } if (!isset($shipment) || !$shipment) { $address = ActiveRecordModel::getInstanceById('UserAddress', $addressId, true); $shipment = Shipment::getNewInstance($this->order); $shipment->shippingAddress->set($address); $shipment->save(); $this->order->addShipment($shipment); $shipment->addItem($item); } $item->save(); } } if ($item->shipment->get()) { $item->shipment->get()->shippingAmount->set(0); $item->shipment->get()->shippingServiceData->set(null); $item->shipment->get()->save(); } } } $this->order->mergeItems(); SessionOrder::save($this->order); // proceed with the checkout if ($this->request->get('proceed')) { return new ActionRedirectResponse('checkout', 'index'); } // redirect to payment gateway if ($url = $this->request->get('redirect')) { return new RedirectResponse($url); } return new ActionRedirectResponse('order', 'index', array('query' => 'return=' . $this->request->get('return'))); }
public function saveCustomerOrder(CustomerOrder $order) { $order->shippingAddress->get()->save(); $order->billingAddress->get()->save(); $order->save(); $shipment = $order->getShipments()->get(0); $shipment->shippingAmount->set($order->rawData['shipping_cost']); $shipment->save(); if ($order->rawData['tax'] > 0) { $tax = ActiveRecordModel::getNewInstance('ShipmentTax'); $tax->shipment->set($shipment); $tax->amount->set($order->rawData['tax']); $tax->save(); $shipment->addFixedTax($tax); $shipment->status->set(Shipment::STATUS_SHIPPED); $shipment->save(); } return parent::saveCustomerOrder($order); }
public static function save(CustomerOrder $order) { // mark shipment data as modified - to force saving $order->getShipments(); $order->save(); self::setOrder($order); }
public function __construct(CustomerOrder $order, Currency $currency) { parent::__construct(); $this->order = $order; $order->loadAll(); // billing address if ($address = $order->billingAddress->get()) { $fields = array('firstName', 'lastName', 'companyName', 'phone', 'city', 'postalCode', 'countryID' => 'country'); foreach ($fields as $key => $field) { $addressField = is_numeric($key) ? $field : $key; $this->{$field}->set($address->{$addressField}->get()); } $this->state->set($this->getStateValue($address)); $this->address->set($address->address1->get() . ' ' . $address->address2->get()); } // shipping address $address = $order->shippingAddress->get(); if (!$address) { $address = $order->billingAddress->get(); } if ($address) { foreach ($fields as $key => $field) { $addressField = is_numeric($key) ? $field : $key; $field = 'shipping' . ucfirst($field); $this->{$field}->set($address->{$addressField}->get()); } $this->shippingState->set($this->getStateValue($address)); $this->shippingAddress->set($address->address1->get() . ' ' . $address->address2->get()); } // amount $order->currency->set($currency); $this->amount->set(round($order->getDueAmount(), 2)); $this->currency->set($currency->getID()); // transaction identification $this->invoiceID->set($order->getID()); if (isset($_SERVER['REMOTE_ADDR'])) { $this->ipAddress->set($_SERVER['REMOTE_ADDR']); } // customer identification if ($order->user->get()) { $order->user->get()->load(); $this->shippingEmail->set($order->user->get()->email->get()); $this->email->set($order->user->get()->email->get()); $this->clientID->set($order->user->get()->getID()); } // order details // load variation data $variations = new ProductSet(); foreach ($order->getShoppingCartItems() as $item) { if ($item->product->get() && $item->product->get()->parent->get()) { $variations->unshift($item->product->get()); } } if ($variations->size()) { $variations->loadVariations(); } foreach ($order->getShoppingCartItems() as $item) { $product = $item->getProduct(); $variations = array(); foreach ($product->getRegisteredVariations() as $variation) { $variations[] = $variation->getValueByLang('name'); } $ri = RecurringItem::getInstanceByOrderedItem($item); if ($ri && $ri->isExistingRecord()) { $ri->load(); } else { $ri = null; } $this->addLineItem($product->getName() . ($variations ? ' (' . implode(' / ', $variations) . ')' : ''), $item->getPrice(false), $item->count->get(), $product->sku->get(), $ri); } if ($discount = $order->getFixedDiscountAmount()) { $this->addLineItem(CustomerOrder::getApplication()->translate('_discount'), $discount * -1, 1, 'discount'); } foreach ($order->getShipments() as $shipment) { if ($rate = $shipment->getSelectedRate()) { $rate = $rate->toArray(); $name = empty($rate['ShippingService']['name_lang']) ? $rate['serviceName'] : $rate['ShippingService']['name_lang']; $this->addLineItem($name, $shipment->getShippingTotalBeforeTax(), 1, 'shipping'); } } if ($taxes = $order->getTaxBreakdown()) { foreach ($taxes as $id => $amount) { $tax = Tax::getInstanceById($id, true); $this->addLineItem($tax->getValueByLang('name', null), $amount, 1, 'tax'); } } }
protected function removeEmptyShipments(CustomerOrder $order) { foreach ($order->getShipments() as $shipment) { if (count($shipment->getItems()) == 0) { $shipment->delete(); } } }
private function assertDefaultZoneOrder(CustomerOrder $order, Currency $currency) { $this->assertEquals(100, $order->getTotal()); $shipment = $order->getShipments()->get(0); $shipmentArray = $shipment->toArray(); $this->assertEquals(9.09, round($shipmentArray['taxAmount'], 2), 'shipment array, taxAmount'); $this->assertEquals(90.91, round($shipmentArray['amount'], 2), 'shipment array, amount'); $this->assertEquals(100.0, round($shipmentArray['amount'] + $shipmentArray['taxAmount'], 2), 'shipment array, amount + taxAmount'); $this->assertEquals(100, round($shipment->getSubTotal(true), 2), '$shipment->getSubTotal(<with taxes>)'); $this->assertEquals(90.91, round($shipment->getSubTotal(false), 2), '$shipment->getSubTotal(<without taxes>)'); $arr = $order->toArray(); $this->assertEquals(100, $arr['cartItems'][0]['displayPrice']); $this->assertEquals(100, $arr['cartItems'][0]['displaySubTotal']); // $this->assertEqual(round($arr['cartItems'][0]['itemSubTotal'], 2), 90.91); }
/** * Determines if the necessary steps have been completed, so the order could be finalized * * @return RedirectResponse * @return ActionRedirectResponse * @return false */ protected function validateOrder(CustomerOrder $order, $step = 0) { if ($order->isFinalized->get()) { return false; } // no items in shopping cart if (!count($order->getShoppingCartItems())) { if ($this->request->isValueSet('return')) { return new RedirectResponse($this->router->createUrlFromRoute($this->request->get('return'))); } else { return new ActionRedirectResponse('index', 'index'); } } // order is not orderable (too few/many items, etc.) $isOrderable = $order->isOrderable(true, false); if (!$isOrderable || $isOrderable instanceof OrderException) { return new ActionRedirectResponse('order', 'index'); } $valStep = $this->config->get('CHECKOUT_CUSTOM_FIELDS'); $validateFields = 'CART_PAGE' == $valStep || 'BILLING_ADDRESS_STEP' == $valStep && self::STEP_ADDRESS <= $step || 'SHIPPING_ADDRESS_STEP' == $valStep && (self::STEP_ADDRESS == $step && 'shipping' == $this->request->get('step') || self::STEP_ADDRESS < $step) || 'SHIPPING_METHOD_STEP' == $valStep && self::STEP_SHIPPING < $step; $isOrderable = $order->isOrderable(true, $validateFields); // custom fields selected in cart page? if ('CART_PAGE' == $valStep && !$isOrderable) { return new ActionRedirectResponse('order', 'index'); } // shipping address selected if ($step >= self::STEP_SHIPPING) { if (!$order->shippingAddress->get() && $order->isShippingRequired() && !$order->isMultiAddress->get() || !$order->billingAddress->get() || !$isOrderable) { return new ActionRedirectResponse('checkout', 'selectAddress', $this->request->get('step') ? array('step' => $this->request->get('step')) : null); } } // shipping method selected if ($step >= self::STEP_PAYMENT && $order->isShippingRequired() || !$isOrderable) { foreach ($order->getShipments() as $shipment) { if (!$shipment->getSelectedRate() && $shipment->isShippable()) { return new ActionRedirectResponse('checkout', 'shipping'); } } } return false; }
private function getOrderShipments(CustomerOrder $order) { $order->loadItems(); $shipments = $order->getShipments(); $downloadable = $order->getDownloadShipment(false); if ($downloadable && count($shipments) == 1 && !count($downloadable->getItems())) { $downloadable = null; } $shipmentsArray = array(); foreach ($shipments as $key => $shipment) { // one shipment is reserved for downloadable items if ($shipment === $downloadable || $shipment->isShipped()) { continue; } $shipmentsArray[$shipment->getID()] = $shipment->toArray(); $rate = unserialize($shipment->shippingServiceData->get()); if (is_object($rate)) { $rate->setApplication($this->application); $shipmentsArray[$shipment->getID()] = array_merge($shipmentsArray[$shipment->getID()], $rate->toArray()); $shipmentsArray[$shipment->getID()]['ShippingService']['ID'] = $shipmentsArray[$shipment->getID()]['serviceID']; } else { $shipmentsArray[$shipment->getID()]['ShippingService']['name_lang'] = $this->translate('_shipping_service_is_not_selected'); } } return $shipmentsArray; }
public function saveCustomerOrder(CustomerOrder $order) { $order->shippingAddress->get()->save(); $order->billingAddress->get()->save(); $order->save(); $shipment = $order->getShipments()->get(0); if ($shipment) { $shipment->shippingAmount->set($order->rawData['shipping_cost']); if ($order->status->get() == CustomerOrder::STATUS_SHIPPED) { $shipment->status->set(Shipment::STATUS_SHIPPED); } $shipment->save(); } return parent::saveCustomerOrder($order); }
private function getShippingRates(CustomerOrder $order, $addresses) { if (!$order->isShippingRequired()) { return array(); } $shipment = $order->getShipments()->get(0); $rates = array(); foreach ($addresses as $id => $address) { $order->shippingAddress->set($address); $tax = $order->getTaxAmount(); $rates[$id] = $shipment->getShippingRates()->toArray(); foreach ($rates[$id] as &$rate) { $rate['orderTax'] = $tax; } } $order->shippingAddress->set(null); return $rates; }