public function PackerManager::packToShipments in Commerce Shipping 8.2
Packs the given order and populates the given shipments.
Parameters
\Drupal\commerce_order\Entity\OrderInterface $order: The order.
\Drupal\profile\Entity\ProfileInterface $shipping_profile: The shipping profile.
\Drupal\commerce_shipping\Entity\ShipmentInterface[] $shipments: The shipments to populate.
Return value
array An array with the populated shipments as the first element, and the removed shipments as the second.
Overrides PackerManagerInterface::packToShipments
File
- src/
PackerManager.php, line 70
Class
Namespace
Drupal\commerce_shippingCode
public function packToShipments(OrderInterface $order, ProfileInterface $shipping_profile, array $shipments) {
$shipment_storage = $this->entityTypeManager
->getStorage('commerce_shipment');
$proposed_shipments = $this
->pack($order, $shipping_profile);
$populated_shipments = [];
foreach ($proposed_shipments as $index => $proposed_shipment) {
$shipment = NULL;
// Take the first existing shipment of the matching type.
foreach ($shipments as $existing_index => $existing_shipment) {
if ($existing_shipment
->bundle() == $proposed_shipment
->getType()) {
$shipment = $existing_shipment;
unset($shipments[$existing_index]);
break;
}
}
if (!$shipment) {
$shipment = $shipment_storage
->create([
'type' => $proposed_shipment
->getType(),
]);
}
$shipment
->populateFromProposedShipment($proposed_shipment);
$shipment
->setData('owned_by_packer', TRUE);
$populated_shipments[$index] = $shipment;
}
$removed_shipments = array_filter($shipments, function ($shipment) {
/** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment */
return !$shipment
->isNew();
});
return [
$populated_shipments,
(array) $removed_shipments,
];
}