/** * Get the quote address the ship group is the destination for. * * @param ITaxedShipGroup * @return Mage_Sales_Model_Quote_Address|null */ protected function _getQuoteAddressForShipGroup(ITaxedShipGroup $shipGroup) { // If the destination id can be guaranteed to match between request and // response, the id generated for the request could be captured with the // address to link the destination payload back up with the quote address. return $this->_quote->getAddressesCollection()->getItemByColumnValue('destination_id', $shipGroup->getDestination()->getId()); }
/** * Add data to the request for addresses in the quote. * * @return self */ protected function _injectAddressData() { $destinationIterable = $this->_payload->getDestinations(); $shipGroupIterable = $this->_payload->getShipGroups(); foreach ($this->_quote->getAddressesCollection() as $address) { // Defer responsibility for building ship group and destination // payloads to address request builders. $addressBuilder = $this->_taxFactory->createRequestBuilderAddress($shipGroupIterable, $destinationIterable, $address); $destinationPayload = $addressBuilder->getDestinationPayload(); // Billing addresses need to be set separately in the request payload. // The addres request builder should have created the destination // for the billing address, even if there were no items to add to // to the ship group for the billing address. E.g. a billing address // is still a destination so the returned payload will still have a // destination but may not be a valid ship group (checked separately). if ($address->getAddressType() === Mage_Sales_Model_Quote_Address::TYPE_BILLING) { $this->_payload->setBillingInformation($destinationPayload); } $shipGroupPayload = $addressBuilder->getShipGroupPayload(); if ($shipGroupPayload) { $shipGroupIterable[$shipGroupPayload] = $shipGroupPayload; } } $this->_payload->setShipGroups($shipGroupIterable); return $this; }
/** * Gets a PakjeGemak address for either a quote or an order object. * * @param Mage_Sales_Model_Quote|Mage_Sales_Model_Order $object * * @return false|Mage_Sales_Model_Order_Address|Mage_Sales_Model_Quote_Address|TIG_MyParcel2014_Model_Shipment */ public function getPgAddress($object) { /** * Get all addresses for the specified object. */ if ($object instanceof Mage_Sales_Model_Quote) { $addressCollection = $object->getAllAddresses(); } elseif ($object instanceof Mage_Sales_Model_Order) { $addressCollection = $object->getAddressesCollection(); } elseif ($object instanceof TIG_MyParcel2014_Model_Shipment) { $order = $object->getOrder(); if (!$order) { return false; } $addressCollection = $order->getAddressesCollection(); } else { return false; } /** * Go through each address and check if it's a PakjeGemak address. * * @var Mage_Sales_Model_Quote_Address|Mage_Sales_Model_Order_Address $address */ $pgAddress = false; foreach ($addressCollection as $address) { if ($address->getAddressType() == self::PG_ADDRESS_TYPE) { $pgAddress = $address; break; } } /** * Return the PakjeGemak address or false if none was found. */ return $pgAddress; }
/** * Create duplicate of quote preserving all data (items, addresses, payment etc.) * * @param Mage_Sales_Model_Quote $quote Original Quote * @param bool $active Create active quote or not * @return Mage_Sales_Model_Quote New created quote */ public function copyQuote(Mage_Sales_Model_Quote $quote, $active = false) { if (!$quote->getId()) { return $quote; } $newQuote = clone $quote; $newQuote->setId(null); $newQuote->setIsActive($active ? 1 : 0); $newQuote->save(); // copy items with their options $newParentItemIds = array(); foreach ($quote->getItemsCollection() as $item) { // save child items later if ($item->getParentItem()) { continue; } $oldItemId = $item->getId(); $newItem = clone $item; $newItem->setQuote($newQuote); $newItem->save(); $newParentItemIds[$oldItemId] = $newItem->getId(); } // save childs with new parent id foreach ($quote->getItemsCollection() as $item) { if (!$item->getParentItem() || !isset($newParentItemIds[$item->getParentItemId()])) { continue; } $newItem = clone $item; $newItem->setQuote($newQuote); $newItem->setParentItemId($newParentItemIds[$item->getParentItemId()]); $newItem->save(); } // copy billing and shipping addresses foreach ($quote->getAddressesCollection() as $address) { $address->setQuote($newQuote); $address->setId(null); $address->save(); } // copy payment info foreach ($quote->getPaymentsCollection() as $payment) { $payment->setQuote($newQuote); $payment->setId(null); $payment->save(); } return $newQuote; }