Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
shipment_comments_for_search.php
Go to the documentation of this file.
1 <?php
13 
14 require 'default_rollback.php';
15 require __DIR__ . '/order.php';
16 
18 $payment = $order->getPayment();
19 $paymentInfoBlock = Bootstrap::getObjectManager()->get(Data::class)
20  ->getInfoBlock($payment);
21 $payment->setBlockMock($paymentInfoBlock);
22 
23 $items = [];
24 foreach ($order->getItems() as $orderItem) {
25  $items[$orderItem->getId()] = $orderItem->getQtyOrdered();
26 }
27 $shipment = Bootstrap::getObjectManager()->get(ShipmentFactory::class)->create($order, $items);
28 
29 $shipment->setPackages([['1'], ['2']]);
30 $shipment->setShipmentStatus(\Magento\Sales\Model\Order\Shipment::STATUS_NEW);
31 $shipment->save();
32 
34  [
35  'comment' => 'comment 1',
36  'is_visible_on_front' => 1,
37  'is_customer_notified' => 1,
38  ],
39  [
40  'comment' => 'comment 2',
41  'is_visible_on_front' => 1,
42  'is_customer_notified' => 1,
43  ],
44  [
45  'comment' => 'comment 3',
46  'is_visible_on_front' => 1,
47  'is_customer_notified' => 1,
48  ],
49  [
50  'comment' => 'comment 4',
51  'is_visible_on_front' => 1,
52  'is_customer_notified' => 1,
53  ],
54  [
55  'comment' => 'comment 5',
56  'is_visible_on_front' => 0,
57  'is_customer_notified' => 1,
58  ],
59 ];
60 
62 $shipmentCommentRepository = Bootstrap::getObjectManager()->get(ShipmentCommentRepositoryInterface::class);
63 
64 foreach ($comments as $data) {
66  $comment = Bootstrap::getObjectManager()->create(Comment::class);
67  $comment->setParentId($shipment->getId());
68  $comment->setComment($data['comment']);
69  $comment->setIsVisibleOnFront($data['is_visible_on_front']);
70  $comment->setIsCustomerNotified($data['is_customer_notified']);
71  $shipmentCommentRepository->save($comment);
72 }
$orderItem
Definition: order.php:30
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$order
Definition: order.php:55
foreach($order->getItems() as $orderItem) $shipment