class TestPacker in Commerce Shipping 8.2
Creates a shipment per order item.
Only applies to shipments going to France.
Hierarchy
- class \Drupal\commerce_shipping_test\Packer\TestPacker implements PackerInterface
Expanded class hierarchy of TestPacker
1 file declares its use of TestPacker
- PackerManagerTest.php in tests/
src/ Kernel/ PackerManagerTest.php
1 string reference to 'TestPacker'
- commerce_shipping_test.services.yml in tests/
modules/ commerce_shipping_test/ commerce_shipping_test.services.yml - tests/modules/commerce_shipping_test/commerce_shipping_test.services.yml
1 service uses TestPacker
- commerce_shipping_test.test_packer in tests/
modules/ commerce_shipping_test/ commerce_shipping_test.services.yml - Drupal\commerce_shipping_test\Packer\TestPacker
File
- tests/
modules/ commerce_shipping_test/ src/ Packer/ TestPacker.php, line 19
Namespace
Drupal\commerce_shipping_test\PackerView source
class TestPacker implements PackerInterface {
/**
* {@inheritdoc}
*/
public function applies(OrderInterface $order, ProfileInterface $shipping_profile) {
return $shipping_profile->address->country_code == 'FR';
}
/**
* {@inheritdoc}
*/
public function pack(OrderInterface $order, ProfileInterface $shipping_profile) {
$proposed_shipments = [];
foreach ($order
->getItems() as $index => $order_item) {
$purchased_entity = $order_item
->getPurchasedEntity();
// Ship only shippable purchasable entity types.
if (!$purchased_entity || !$purchased_entity
->hasField('weight')) {
continue;
}
// The weight will be empty if the shippable trait was added but the
// existing entities were not updated.
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;
}
/** @var \Drupal\physical\Weight $weight */
$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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
TestPacker:: |
public | function |
Determines whether the packer applies to the given order. Overrides PackerInterface:: |
|
TestPacker:: |
public | function |
Packs the given order. Overrides PackerInterface:: |