Skip to content

Commit

Permalink
8022: Uncaught Error: Call to a member function addItem() on array in…
Browse files Browse the repository at this point in the history
… app/code/Magento/Sales/Model/Order/Shipment.php
  • Loading branch information
RomaKis committed Oct 27, 2017
1 parent c6e891d commit 44021e7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
18 changes: 9 additions & 9 deletions app/code/Magento/Sales/Model/Order/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class Shipment extends AbstractModel implements EntityInterface, ShipmentInterfa
*/
protected $orderRepository;

/**
* @var \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection|null
*/
private $tracksCollection = null;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand Down Expand Up @@ -333,16 +338,11 @@ public function addItem(\Magento\Sales\Model\Order\Shipment\Item $item)
*/
public function getTracksCollection()
{
if (!$this->hasData(ShipmentInterface::TRACKS)) {
$this->setTracks($this->_trackCollectionFactory->create()->setShipmentFilter($this->getId()));

if ($this->getId()) {
foreach ($this->getTracks() as $track) {
$track->setShipment($this);
}
}
if ($this->tracksCollection === null) {
$this->tracksCollection = $this->_trackCollectionFactory->create()->setShipmentFilter($this->getId());
}
return $this->getTracks();

return $this->tracksCollection;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public function processRelation(\Magento\Framework\Model\AbstractModel $object)
$this->shipmentItemResource->save($item);
}
}
if (null !== $object->getTracks()) {
foreach ($object->getTracks() as $track) {
if (null !== $object->getTracksCollection()) {
foreach ($object->getTracksCollection() as $track) {
$track->setParentId($object->getId());
$this->shipmentTrackResource->save($track);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sales\Model\Order;

/**
Expand Down Expand Up @@ -47,4 +48,35 @@ public function testPackages()
$shipment->load($shipment->getId());
$this->assertEquals($packages, $shipment->getPackages());
}

/**
* Check that getTracksCollection() always return collection instance.
*
* @magentoDataFixture Magento/Sales/_files/order.php
*/
public function testAddTrack()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

$order = $objectManager->create(\Magento\Sales\Model\Order::class);
$order->loadByIncrementId('100000001');

/** @var \Magento\Sales\Model\Order\Shipment $shipment */
$shipment = $objectManager->create(\Magento\Sales\Model\Order\Shipment::class);
$shipment->setOrder($order);

$shipment->addItem($objectManager->create(\Magento\Sales\Model\Order\Shipment\Item::class));
$shipment->save();

/** @var $track \Magento\Sales\Model\Order\Shipment\Track */
$track = $objectManager->get(\Magento\Sales\Model\Order\Shipment\Track::class);
$track->setNumber('Test Number')->setTitle('Test Title')->setCarrierCode('Test CODE');

$this->assertEmpty($shipment->getTracks());
$shipment->addTrack($track)->save();

//to empty cache
$shipment->setTracks(null);
$this->assertNotEmpty($shipment->getTracks());
}
}

0 comments on commit 44021e7

Please sign in to comment.