9
9
use Magento \Sales \Api \Data \ShipmentInterface ;
10
10
use Magento \Sales \Model \AbstractModel ;
11
11
use Magento \Sales \Model \EntityInterface ;
12
+ use Magento \Sales \Model \ResourceModel \Order \Shipment \Comment \Collection as CommentsCollection ;
12
13
13
14
/**
14
15
* Sales order shipment model
@@ -94,9 +95,14 @@ class Shipment extends AbstractModel implements EntityInterface, ShipmentInterfa
94
95
protected $ orderRepository ;
95
96
96
97
/**
97
- * @var \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection|null
98
+ * @var \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection
98
99
*/
99
- private $ tracksCollection = null ;
100
+ private $ tracksCollection ;
101
+
102
+ /**
103
+ * @var CommentsCollection
104
+ */
105
+ private $ commentsCollection ;
100
106
101
107
/**
102
108
* @param \Magento\Framework\Model\Context $context
@@ -414,43 +420,45 @@ public function addTrack(\Magento\Sales\Model\Order\Shipment\Track $track)
414
420
public function addComment ($ comment , $ notify = false , $ visibleOnFront = false )
415
421
{
416
422
if (!$ comment instanceof \Magento \Sales \Model \Order \Shipment \Comment) {
417
- $ comment = $ this ->_commentFactory ->create ()->setComment (
418
- $ comment
419
- )->setIsCustomerNotified (
420
- $ notify
421
- )->setIsVisibleOnFront (
422
- $ visibleOnFront
423
- );
423
+ $ comment = $ this ->_commentFactory ->create ()
424
+ ->setComment ($ comment )
425
+ ->setIsCustomerNotified ($ notify )
426
+ ->setIsVisibleOnFront ($ visibleOnFront );
424
427
}
425
- $ comment ->setShipment ($ this )->setParentId ($ this ->getId ())->setStoreId ($ this ->getStoreId ());
428
+ $ comment ->setShipment ($ this )
429
+ ->setParentId ($ this ->getId ())
430
+ ->setStoreId ($ this ->getStoreId ());
426
431
if (!$ comment ->getId ()) {
427
432
$ this ->getCommentsCollection ()->addItem ($ comment );
428
433
}
434
+ $ comments = $ this ->getComments ();
435
+ $ comments [] = $ comment ;
436
+ $ this ->setComments ($ comments );
429
437
$ this ->_hasDataChanges = true ;
430
438
return $ this ;
431
439
}
432
440
433
441
/**
434
- * Retrieve comments collection.
442
+ * Retrieves comments collection.
435
443
*
436
444
* @param bool $reload
437
- * @return \Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\Collection
445
+ * @return CommentsCollection
438
446
*/
439
447
public function getCommentsCollection ($ reload = false )
440
448
{
441
- if (!$ this ->hasData (ShipmentInterface::COMMENTS ) || $ reload ) {
442
- $ comments = $ this ->_commentCollectionFactory ->create ()
443
- ->setShipmentFilter ($ this ->getId ())
444
- ->setCreatedAtOrder ();
445
- $ this ->setComments ($ comments );
446
-
449
+ if ($ this ->commentsCollection === null || $ reload ) {
450
+ $ this ->commentsCollection = $ this ->_commentCollectionFactory ->create ();
447
451
if ($ this ->getId ()) {
448
- foreach ($ this ->getComments () as $ comment ) {
452
+ $ this ->commentsCollection ->setShipmentFilter ($ this ->getId ())
453
+ ->setCreatedAtOrder ();
454
+
455
+ foreach ($ this ->commentsCollection as $ comment ) {
449
456
$ comment ->setShipment ($ this );
450
457
}
451
458
}
452
459
}
453
- return $ this ->getComments ();
460
+
461
+ return $ this ->commentsCollection ;
454
462
}
455
463
456
464
/**
@@ -514,7 +522,7 @@ public function getPackages()
514
522
}
515
523
516
524
/**
517
- * { @inheritdoc}
525
+ * @inheritdoc
518
526
* @codeCoverageIgnore
519
527
*/
520
528
public function setPackages (array $ packages = null )
@@ -611,7 +619,7 @@ public function getCreatedAt()
611
619
}
612
620
613
621
/**
614
- * { @inheritdoc}
622
+ * @inheritdoc
615
623
*/
616
624
public function setCreatedAt ($ createdAt )
617
625
{
@@ -743,95 +751,95 @@ public function setComments($comments = null)
743
751
}
744
752
745
753
/**
746
- * { @inheritdoc}
754
+ * @inheritdoc
747
755
*/
748
756
public function setStoreId ($ id )
749
757
{
750
758
return $ this ->setData (ShipmentInterface::STORE_ID , $ id );
751
759
}
752
760
753
761
/**
754
- * { @inheritdoc}
762
+ * @inheritdoc
755
763
*/
756
764
public function setTotalWeight ($ totalWeight )
757
765
{
758
766
return $ this ->setData (ShipmentInterface::TOTAL_WEIGHT , $ totalWeight );
759
767
}
760
768
761
769
/**
762
- * { @inheritdoc}
770
+ * @inheritdoc
763
771
*/
764
772
public function setTotalQty ($ qty )
765
773
{
766
774
return $ this ->setData (ShipmentInterface::TOTAL_QTY , $ qty );
767
775
}
768
776
769
777
/**
770
- * { @inheritdoc}
778
+ * @inheritdoc
771
779
*/
772
780
public function setEmailSent ($ emailSent )
773
781
{
774
782
return $ this ->setData (ShipmentInterface::EMAIL_SENT , $ emailSent );
775
783
}
776
784
777
785
/**
778
- * { @inheritdoc}
786
+ * @inheritdoc
779
787
*/
780
788
public function setOrderId ($ id )
781
789
{
782
790
return $ this ->setData (ShipmentInterface::ORDER_ID , $ id );
783
791
}
784
792
785
793
/**
786
- * { @inheritdoc}
794
+ * @inheritdoc
787
795
*/
788
796
public function setCustomerId ($ id )
789
797
{
790
798
return $ this ->setData (ShipmentInterface::CUSTOMER_ID , $ id );
791
799
}
792
800
793
801
/**
794
- * { @inheritdoc}
802
+ * @inheritdoc
795
803
*/
796
804
public function setShippingAddressId ($ id )
797
805
{
798
806
return $ this ->setData (ShipmentInterface::SHIPPING_ADDRESS_ID , $ id );
799
807
}
800
808
801
809
/**
802
- * { @inheritdoc}
810
+ * @inheritdoc
803
811
*/
804
812
public function setBillingAddressId ($ id )
805
813
{
806
814
return $ this ->setData (ShipmentInterface::BILLING_ADDRESS_ID , $ id );
807
815
}
808
816
809
817
/**
810
- * { @inheritdoc}
818
+ * @inheritdoc
811
819
*/
812
820
public function setShipmentStatus ($ shipmentStatus )
813
821
{
814
822
return $ this ->setData (ShipmentInterface::SHIPMENT_STATUS , $ shipmentStatus );
815
823
}
816
824
817
825
/**
818
- * { @inheritdoc}
826
+ * @inheritdoc
819
827
*/
820
828
public function setIncrementId ($ id )
821
829
{
822
830
return $ this ->setData (ShipmentInterface::INCREMENT_ID , $ id );
823
831
}
824
832
825
833
/**
826
- * { @inheritdoc}
834
+ * @inheritdoc
827
835
*/
828
836
public function setUpdatedAt ($ timestamp )
829
837
{
830
838
return $ this ->setData (ShipmentInterface::UPDATED_AT , $ timestamp );
831
839
}
832
840
833
841
/**
834
- * { @inheritdoc}
842
+ * @inheritdoc
835
843
*
836
844
* @return \Magento\Sales\Api\Data\ShipmentExtensionInterface|null
837
845
*/
@@ -841,7 +849,7 @@ public function getExtensionAttributes()
841
849
}
842
850
843
851
/**
844
- * { @inheritdoc}
852
+ * @inheritdoc
845
853
*
846
854
* @param \Magento\Sales\Api\Data\ShipmentExtensionInterface $extensionAttributes
847
855
* @return $this
0 commit comments