Skip to content

Commit

Permalink
Optionally allow a shippingMethod to be associated with
Browse files Browse the repository at this point in the history
a ShippingMethodOption

Resolves #3271

Craft Commerce's ShippingMethodOption erases the information of any
custom ShippingMethod's that were used to create them. While not ideal,
this change sets a new field, shippingMethod on the ShippingMethodOption
class and also proxies the call to getShippingRules to use the attached
ShippingMethod if it is present.

Of course, other functions such as getName, getType, etc will not be
overridden and would need to be accessed like
$option->shippingMethod->getType().
  • Loading branch information
johnnynotsolucky committed Sep 12, 2023
1 parent 64f9d46 commit e9d5cb2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/elements/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -1984,6 +1984,7 @@ public function getAvailableShippingMethodOptions(): array
$option->handle = $method->getHandle();
$option->matchesOrder = ArrayHelper::isIn($method->getHandle(), $matchingMethodHandles);
$option->price = $method->getPriceForOrder($this);
$option->shippingMethod = $method;

// Add all methods if completed, and only the matching methods when it is not completed.
if ($this->isCompleted || $option->matchesOrder) {
Expand Down
15 changes: 15 additions & 0 deletions src/models/ShippingMethodOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace craft\commerce\models;

use craft\commerce\base\ShippingMethod as BaseShippingMethod;
use craft\commerce\behaviors\CurrencyAttributeBehavior;
use craft\commerce\elements\Order;
use craft\commerce\Plugin;
Expand Down Expand Up @@ -39,6 +40,20 @@ class ShippingMethodOption extends ShippingMethod
*/
public bool $matchesOrder;

/**
* @var BaseShippingMethod|null The shipping method this option was derived from.
*/
public ?BaseShippingMethod $shippingMethod = null;

public function getShippingRules(): array
{
if ($this->shippingMethod !== null) {
return $this->shippingMethod->getShippingRules();
}

return parent::getShippingRules();
}

/**
* @throws InvalidConfigException
*/
Expand Down

0 comments on commit e9d5cb2

Please sign in to comment.