/**
  * Removes any virtual gift card items from the order create request and
  * re-adds them as parts of ship groups with destinations using the recipient
  * email address as the destination.
  *
  * @param Mage_Sales_Model_Order
  * @param IOrderCreateRequest
  * @return self
  */
 public function fixOrderCreateVirtualGiftCards(Mage_Sales_Model_Order $order, IOrderCreateRequest $orderCreateRequest)
 {
     $virtualGiftCards = $this->getOrderedVirtualGiftCards($order);
     if ($virtualGiftCards) {
         $this->logger->debug('Order contains {count} virtual gift cards', $this->logContext->getMetaData(__CLASS__, ['count' => count($virtualGiftCards)]));
         $shipGroups = $orderCreateRequest->getShipGroups();
         $destinations = $orderCreateRequest->getDestinations();
         // Assume virtual gift cards were added to the wrong ship group
         // with the wrong destination. Remove them from where they are to
         // re-add them in the right place.
         $removedItemPayloads = $this->removeVirtualGiftCardPayloads($shipGroups, $virtualGiftCards);
         $this->logger->debug('Removed {count} virtual gift card item payloads', $this->logContext->getMetaData(__CLASS__, ['count' => count($removedItemPayloads)]));
         // Each of the removed items needs to be added back into the OCR
         // as part of a ship group with the recipient's email address as the
         // destination of the ship group.
         foreach ($removedItemPayloads as $itemRefPair) {
             $shipGroup = $this->createShipGroupForVirtualGiftCard($itemRefPair, $shipGroups, $destinations);
             $shipGroups->offsetSet($shipGroup);
         }
         // After pulling virtual gift cards out of existing ship groups, make
         // sure the OCR isn't left with any empty ship groups.
         $this->removeEmptyShipGroups($shipGroups, $destinations);
         $orderCreateRequest->setShipGroups($shipGroups);
     } else {
         $this->logger->debug('Order does not contain any virtual gift cards', $this->logContext->getMetaData(__CLASS__));
     }
     return $this;
 }
 /**
  * Validate ship group references to a destination and order items.
  *
  * @param IOrderCreateRequest
  * @return self
  */
 protected function validateShipGroupReferences(IOrderCreateRequest $payload)
 {
     $shipGroups = $payload->getShipGroups();
     $destinations = $payload->getDestinations();
     $orderItems = $payload->getOrderItems();
     foreach ($shipGroups as $shipGroup) {
         $this->validateShipGroupDestination($shipGroup, $destinations)->validateOrderItemReferences($shipGroup, $orderItems);
     }
     return $this;
 }
 /**
  * Add ship groups to the request. For each address in the order, dispatch
  * an event for handling ship group destinations. For each ship group
  * destination added, trigger additional events for each item in the ship
  * group.
  *
  * @param Mage_Sales_Model_Order
  * @param IOrderCreateRequest
  * @return self
  */
 protected function _setShipGroups(Mage_Sales_Model_Order $order, IOrderCreateRequest $request)
 {
     $shipGroups = $request->getShipGroups();
     $orderItems = $request->getOrderItems();
     $destinations = $request->getDestinations();
     $itemCollection = $order->getItemsCollection();
     foreach ($order->getAddressesCollection() as $address) {
         $items = $this->_getItemsForAddress($address, $itemCollection);
         if ($items) {
             $shipGroups->offsetSet($this->_buildShipGroupForAddress($address, $items, $order, $shipGroups, $destinations, $orderItems));
         }
     }
     return $this;
 }