Commit 9e499c5b authored by mikelutz's avatar mikelutz Committed by bojanz

Issue #2920242 by mikelutz, bojanz: Shipment always using last shipping method's package type

parent de2efb98
......@@ -85,16 +85,12 @@ class ShippingRateWidget extends WidgetBase implements ContainerFactoryPluginInt
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
/** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment */
$shipment = $items[$delta]->getEntity();
$use_default_package_type = empty($shipment->getPackageType());
/** @var \Drupal\commerce_shipping\ShippingMethodStorageInterface $shipping_method_storage */
$shipping_method_storage = $this->entityTypeManager->getStorage('commerce_shipping_method');
$shipping_methods = $shipping_method_storage->loadMultipleForShipment($shipment);
$options = [];
foreach ($shipping_methods as $shipping_method) {
$shipping_method_plugin = $shipping_method->getPlugin();
if ($use_default_package_type) {
$shipment->setPackageType($shipping_method_plugin->getDefaultPackageType());
}
$shipping_rates = $shipping_method_plugin->calculateRates($shipment);
foreach ($shipping_rates as $shipping_rate) {
$service = $shipping_rate->getService();
......@@ -154,7 +150,11 @@ class ShippingRateWidget extends WidgetBase implements ContainerFactoryPluginInt
$shipping_method_storage = $this->entityTypeManager->getStorage('commerce_shipping_method');
/** @var \Drupal\commerce_shipping\Entity\ShippingMethodInterface $shipping_method */
$shipping_method = $shipping_method_storage->load($shipping_method_id);
$shipping_method->getPlugin()->selectRate($shipment, $shipping_rate);
$shipping_method_plugin = $shipping_method->getPlugin();
if (empty($shipment->getPackageType())) {
$shipment->setPackageType($shipping_method_plugin->getDefaultPackageType());
}
$shipping_method_plugin->selectRate($shipment, $shipping_rate);
// Put delta mapping in $form_state, so that flagErrors() can use it.
$field_state = static::getWidgetState($form['#parents'], $field_name, $form_state);
......
......@@ -125,6 +125,24 @@ class CheckoutPaneTest extends CommerceBrowserTestBase {
'stores' => [$this->store],
]);
/** @var \Drupal\commerce_shipping\Entity\PackageType $package_type */
$package_type = $this->createEntity('commerce_package_type', [
'id' => 'package_type_a',
'label' => 'Package Type A',
'dimensions' => [
'length' => 20,
'width' => 20,
'height' => 20,
'unit' => 'mm',
],
'weight' => [
'number' => 20,
'unit' => 'g',
],
]);
\Drupal::service('plugin.manager.commerce_package_type')->clearCachedDefinitions();
// Create two flat rate shipping methods.
$first_shipping_method = $this->createEntity('commerce_shipping_method', [
'name' => 'Overnight shipping',
......@@ -132,6 +150,7 @@ class CheckoutPaneTest extends CommerceBrowserTestBase {
'plugin' => [
'target_plugin_id' => 'flat_rate',
'target_plugin_configuration' => [
'default_package_type' => 'commerce_package_type:' . $package_type->get('uuid'),
'rate_label' => 'Overnight shipping',
'rate_amount' => [
'number' => '19.99',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment