Ejemplo n.º 1
0
 /**
  * @param array $giftMessages
  * @param \Magento\Sales\Model\Quote $quote
  * @return $this
  */
 public function add($giftMessages, $quote)
 {
     if (!is_array($giftMessages)) {
         return $this;
     }
     // types are 'quote', 'quote_item', etc
     foreach ($giftMessages as $type => $giftMessageEntities) {
         foreach ($giftMessageEntities as $entityId => $message) {
             $giftMessage = $this->messageFactory->create();
             switch ($type) {
                 case 'quote':
                     $entity = $quote;
                     break;
                 case 'quote_item':
                     $entity = $quote->getItemById($entityId);
                     break;
                 case 'quote_address':
                     $entity = $quote->getAddressById($entityId);
                     break;
                 case 'quote_address_item':
                     $entity = $quote->getAddressById($message['address'])->getItemById($entityId);
                     break;
                 default:
                     $entity = $quote;
                     break;
             }
             if ($entity->getGiftMessageId()) {
                 $giftMessage->load($entity->getGiftMessageId());
             }
             if (trim($message['message']) == '') {
                 if ($giftMessage->getId()) {
                     try {
                         $giftMessage->delete();
                         $entity->setGiftMessageId(0)->save();
                     } catch (\Exception $e) {
                     }
                 }
                 continue;
             }
             try {
                 $giftMessage->setSender($message['from'])->setRecipient($message['to'])->setMessage($message['message'])->save();
                 $entity->setGiftMessageId($giftMessage->getId())->save();
             } catch (\Exception $e) {
             }
         }
     }
     return $this;
 }
Ejemplo n.º 2
0
 /**
  * @dataProvider dataProviderGetAddress
  */
 public function testGetAddressById($addressId, $expected)
 {
     $id = 1;
     $this->quoteAddressCollectionMock->expects($this->once())->method('setQuoteFilter')->with($id)->will($this->returnSelf());
     $this->quoteAddressMock->expects($this->once())->method('getId')->will($this->returnValue($id));
     $iterator = new \ArrayIterator([$this->quoteAddressMock]);
     $this->quoteAddressCollectionMock->expects($this->any())->method('getIterator')->will($this->returnValue($iterator));
     $this->quote->setId($id);
     $result = $this->quote->getAddressById($addressId);
     $this->assertEquals((bool) $expected, (bool) $result);
 }