CommerceFedExPacker.php in Commerce FedEx 8
File
src/Packer/CommerceFedExPacker.php
View source
<?php
namespace Drupal\commerce_fedex\Packer;
use Drupal\commerce_fedex\Event\BeforePackEvent;
use Drupal\commerce_fedex\Event\CommerceFedExEvents;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\Entity\OrderItemInterface;
use Drupal\commerce_shipping\Packer\DefaultPacker;
use Drupal\commerce_shipping\Packer\PackerInterface;
use Drupal\commerce_shipping\ProposedShipment;
use Drupal\commerce_shipping\ShipmentItem;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\physical\Weight;
use Drupal\physical\WeightUnit;
use Drupal\profile\Entity\ProfileInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class CommerceFedExPacker extends DefaultPacker implements PackerInterface {
use StringTranslationTrait;
protected $eventDispatcher;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher) {
$this->entityTypeManager = $entity_type_manager;
$this->eventDispatcher = $event_dispatcher;
}
public function pack(OrderInterface $order, ProfileInterface $shipping_profile) {
$shipments = [
[
'title' => $this
->t('Primary Shipment'),
'items' => [],
],
];
foreach ($this
->getOrderItems($order, $shipping_profile) as $order_item) {
$purchased_entity = $order_item
->getPurchasedEntity();
if (!$purchased_entity || !$purchased_entity
->hasField('weight')) {
continue;
}
$quantity = $order_item
->getQuantity();
$shipments[0]['items'][] = new ShipmentItem([
'order_item_id' => $order_item
->id(),
'title' => $order_item
->getTitle(),
'quantity' => $quantity,
'weight' => $this
->getWeight($order_item)
->multiply($quantity),
'declared_value' => $order_item
->getUnitPrice()
->multiply($quantity),
]);
}
$proposed_shipments = [];
foreach ($shipments as $shipment) {
if (!empty($shipment['items'])) {
$proposed_shipments[] = new ProposedShipment([
'type' => $this
->getShipmentType($order),
'order_id' => $order
->id(),
'title' => $shipment['title'],
'items' => $shipment['items'],
'shipping_profile' => $shipping_profile,
]);
}
}
return $proposed_shipments;
}
protected function getWeight(OrderItemInterface $order_item) {
$purchasedEntity = $order_item
->getPurchasedEntity();
if ($purchasedEntity
->get('weight')
->isEmpty()) {
$weight = new Weight(1, WeightUnit::GRAM);
}
else {
$weight_item = $purchasedEntity
->get('weight')
->first();
$weight = $weight_item
->toMeasurement();
}
return $weight;
}
protected function getOrderItems(OrderInterface $order, ProfileInterface $shipping_profile) {
$event = new BeforePackEvent($order
->getItems(), $order, $shipping_profile);
$this->eventDispatcher
->dispatch(CommerceFedExEvents::BEFORE_PACK, $event);
return $event
->getOrderItems();
}
}