A patch is attached that provides a ShippingService CommerceCondition plugin.

Comments

oadaeh created an issue. See original summary.

jsacksick’s picture

Status: Active » Closed (won't fix)

There's no reason to have logic in Commerce shipping to integrate properly with Commerce Fee.

This should live in Commerce Fee.

jsacksick’s picture

Status: Closed (won't fix) » Needs work
Issue tags: +Needs tests

The current patch should add a condition on the shipment.

And $shipment->getShippingService() is what needs to be tested.

The commerce_fee part of the logic should be left out of this patch.

jsacksick’s picture

+++ b/src/Plugin/Commerce/Condition/ShippingService.php
@@ -0,0 +1,86 @@
+ *   entity_type = "commerce_shipping_method",

Provides the shipping service condition for shipments.

entity_type = "commerce_shipping_method",

The comment contradicts the actual entity type the condition is attached to.

We don't expose shipping method conditions anywhere.

jsacksick’s picture

So the problem is, shipment conditions are exposed in the shipping method UI.

And that is used to determine whether a particular shipping method is available, so it doesn't actually make sense to have a shipping method|service condition added to shipments.

oadaeh’s picture

Okay, where would be a better place to expose the shipment conditions?

oadaeh’s picture

Actually, maybe I'm not understanding what you're referring to, because I added the UI to the add/edit Commerce Fee page in the same way that the Order Items condition is added.

oadaeh’s picture

Project: Commerce Shipping » Commerce Fee
Version: 8.x-2.x-dev »
Status: Needs work » Needs review
StatusFileSize
new4.95 KB

Here's another patch.
This one is applied to Commerce Fee.
I haven't added tests yet, but hopefully I addressed everything else. I'll add the tests next.

jsacksick’s picture

Status: Needs review » Needs work
  1. +++ b/src/Entity/Fee.php
    @@ -6,6 +6,7 @@ use Drupal\commerce\ConditionGroup;
    +use Drupal\commerce_fee_shipping\Plugin\Commerce\Fee\ShippingCostFeeInterface;
    

    You cannot do that as there's no guarantee commerce_fee_shipping is enabled, that can cause a crash.

  2. +++ b/src/Plugin/Commerce/Condition/ShippingService.php
    @@ -0,0 +1,105 @@
    +    $form['shipping_service_condition'] = [
    

    You probably need to expose 2 settings (the shipping method, and then the service) as the same service ID can be referenced by different shipping methods.

jsacksick’s picture

  1. +++ b/src/Entity/Fee.php
    @@ -379,6 +380,14 @@ class Fee extends CommerceContentEntityBase implements FeeInterface {
    +    elseif ($plugin instanceof ShippingCostFeeInterface) {
    

    Similar to what you were doing inside commerce_shipping, it probably makes sense to swap the Fee entity class from commerce_shipping.

  2. +++ b/src/Entity/Fee.php
    @@ -379,6 +380,14 @@ class Fee extends CommerceContentEntityBase implements FeeInterface {
    +        $plugin->apply($order, $this);
    

    Similar to what's done for order items.

    The logic should loop on the shipments, and pass the shipment entity to the plugin.

jsacksick’s picture

Also, this condition should live inside commerce_shipping, otherwise it'll crash.

Because you cannot have a condition plugin targeting an entity type that doesn't exist.

So what you need to sum up:

  • Move all the logic to the commerce_fee_shipping submodule
  • Swap the entity class from there.
  • In Fee::apply() loop over shipments, and pass the shipment both to evaluate and apply()
  • Update the condition to expose a shipping_method setting and the service, this will simplify the logic as you can simply check $shipment->getShippingMethod() and $shipment->getShippingService().
  • Also, similar to Commerce core (See PurchasedEntityConditionBase), let's store the shipping method UUID, you need to use the entity UUID mapper both in the configuration form and on submit.
  • Also, you need to have a commerce_fee_shipping.schema.yml (see commerce_order.schema.yml) for the conditions defined by commerce_order.
damienmckenna’s picture

Version: » 1.0.x-dev
oadaeh’s picture

Status: Needs work » Closed (won't fix)

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.