You are here

public function DryIcePlugin::adjustPackage in Commerce FedEx 8

Adjust a package based on the items, shipment and profile.

Parameters

\NicholasCreativeMedia\FedExPHP\Structs\RequestedPackageLineItem $package: The package to adjust.

array $items: An array of shipment items.

\Drupal\commerce_shipping\Entity\ShipmentInterface $shipment: The shipment.

Return value

\NicholasCreativeMedia\FedExPHP\Structs\RequestedPackageLineItem The adjusted Package.

Overrides FedExPluginBase::adjustPackage

File

modules/dry_ice/src/Plugin/Commerce/FedEx/DryIcePlugin.php, line 162

Class

DryIcePlugin
Provides the Fedex Dry Ice Service.

Namespace

Drupal\commerce_fedex_dry_ice\Plugin\Commerce\FedEx

Code

public function adjustPackage(RequestedPackageLineItem $package, array $shipment_items, ShipmentInterface $shipment) {
  $type = $this
    ->getType($shipment);
  if (!$this
    ->verifyPackage($shipment_items, $type)) {
    throw new \Exception("Package cannot be shipped, mix of Dry Ice and non Dry Ice Items");
  }
  if ($this
    ->isDryIceItem(reset($shipment_items), $type)) {
    $special_services_requested = $package
      ->getSpecialServicesRequested();
    if (empty($special_services_requested)) {
      $special_services_requested = new PackageSpecialServicesRequested();
    }
    $dry_ice_weight = new Weight($this->configuration[$type]['weight']['number'], $this->configuration[$type]['weight']['unit']);
    $special_services_requested
      ->addToSpecialServiceTypes(PackageSpecialServiceType::VALUE_DRY_ICE);
    $special_services_requested
      ->setDryIceWeight(FedEx::physicalWeightToFedex($dry_ice_weight));

    /** @var \Drupal\commerce_shipping\Plugin\Commerce\PackageType\PackageType $package_type */
    $package_type = $this->packageTypeManager
      ->createInstance($this->configuration[$type]['package_type']);
    $package
      ->setDimensions(Fedex::packageToFedexDimensions($package_type));
    $package
      ->setWeight(FedEx::packageTotalWeight($shipment_items, $package_type, $dry_ice_weight));
    $package
      ->setSpecialServicesRequested($special_services_requested);
  }
  $package = parent::adjustPackage($package, $shipment_items, $shipment);
  return $package;
}