TestPacker.php in Commerce Shipping 8.2        
                          
                  
                        
  
  
  
  
  
File
  tests/modules/commerce_shipping_test/src/Packer/TestPacker.php
  
    View source  
  <?php
namespace Drupal\commerce_shipping_test\Packer;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_shipping\Packer\PackerInterface;
use Drupal\commerce_shipping\ProposedShipment;
use Drupal\commerce_shipping\ShipmentItem;
use Drupal\physical\Calculator;
use Drupal\physical\Weight;
use Drupal\physical\WeightUnit;
use Drupal\profile\Entity\ProfileInterface;
class TestPacker implements PackerInterface {
  
  public function applies(OrderInterface $order, ProfileInterface $shipping_profile) {
    return $shipping_profile->address->country_code == 'FR';
  }
  
  public function pack(OrderInterface $order, ProfileInterface $shipping_profile) {
    $proposed_shipments = [];
    foreach ($order
      ->getItems() as $index => $order_item) {
      $purchased_entity = $order_item
        ->getPurchasedEntity();
      
      if (!$purchased_entity || !$purchased_entity
        ->hasField('weight')) {
        continue;
      }
      
      if ($purchased_entity
        ->get('weight')
        ->isEmpty()) {
        $purchased_entity
          ->set('weight', new Weight(0, WeightUnit::GRAM));
      }
      $quantity = $order_item
        ->getQuantity();
      if (Calculator::compare($order_item
        ->getQuantity(), '0') == 0) {
        continue;
      }
      
      $weight = $purchased_entity
        ->get('weight')
        ->first()
        ->toMeasurement();
      $proposed_shipments[] = new ProposedShipment([
        'type' => 'default',
        'order_id' => $order
          ->id(),
        'title' => t('Shipment #@index', [
          '@index' => $index + 1,
        ]),
        'items' => [
          new ShipmentItem([
            'order_item_id' => $order_item
              ->id(),
            'title' => $order_item
              ->getTitle(),
            'quantity' => $quantity,
            'weight' => $weight
              ->multiply($quantity),
            'declared_value' => $order_item
              ->getUnitPrice()
              ->multiply($quantity),
          ]),
        ],
        'shipping_profile' => $shipping_profile,
      ]);
    }
    return $proposed_shipments;
  }
}