Example #1
0
 /**
  * Add item to address
  *
  * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
  * @param int $qty
  * @return $this
  */
 public function addItem(\Magento\Quote\Model\Quote\Item\AbstractItem $item, $qty = null)
 {
     if ($item instanceof \Magento\Quote\Model\Quote\Item) {
         if ($item->getParentItemId()) {
             return $this;
         }
         $addressItem = $this->_addressItemFactory->create()->setAddress($this)->importQuoteItem($item);
         $this->getItemsCollection()->addItem($addressItem);
         if ($item->getHasChildren()) {
             foreach ($item->getChildren() as $child) {
                 $addressChildItem = $this->_addressItemFactory->create()->setAddress($this)->importQuoteItem($child)->setParentItem($addressItem);
                 $this->getItemsCollection()->addItem($addressChildItem);
             }
         }
     } else {
         $addressItem = $item;
         $addressItem->setAddress($this);
         if (!$addressItem->getId()) {
             $this->getItemsCollection()->addItem($addressItem);
         }
     }
     if ($qty) {
         $addressItem->setQty($qty);
     }
     return $this;
 }