verbb / postie

A Craft Commerce shipping calculator plugin.
Other
12 stars 18 forks source link

Is it possible to add a new service to a provider on the fly ? #86

Closed seandelaney closed 2 years ago

seandelaney commented 2 years ago

What are you trying to do? I've got a few providers setup in the postie config file. Each provider has an array of services.

Is it possible to add a new service to the list of services on the fly ?

engram-design commented 2 years ago

I'd probably not, strictly speaking. Mostly because services have to match the providers external services. You couldn't just add a "SPECIAL_GROUND" service to say UPS, because it wouldn't be able to fetch any rates for this service code, as it doesn't exist.

But it depends what you want to do. You could use the EVENT_MODIFY_SHIPPING_METHODS which runs after all enabled services for a provider are run.

Under the hood, Postie creates ShippingMethod models for each service. You can see this here:

https://github.com/verbb/postie/blob/5a16e35d8f143929734aa5542208d357e22f6e39/src/base/Provider.php#L328-L371

So what you could do is add your own shipping methods at this point.

use verbb\postie\base\Provider;
use verbb\postie\events\ModifyShippingMethodsEvent;
use verbb\postie\models\ShippingMethod;
use verbb\postie\providers\USPS;
use yii\base\Event;

Event::on(USPS::class, Provider::EVENT_MODIFY_SHIPPING_METHODS, function(ModifyShippingMethodsEvent $event) {
    $shippingMethod = new ShippingMethod();
    $shippingMethod->handle = 'something';
    $shippingMethod->provider = $event->provider;
    $shippingMethod->name = 'Some Name';
    $shippingMethod->enabled = true;

    $event->shippingMethods[] = $shippingMethod;
});

But as I've mentioned, it depends what you're wanting to achieve here, whether its modifying an amount, or just being able to add a new option for users to pick during checkout. If you're just wanting to add another option for users to pick from during checkout, creating your own shipping method in the Commerce control panel would be easier.

Let me know if that's not on the right track of what you need, and if you can expand more on your use-case.

seandelaney commented 2 years ago

@engram-design

Thanks for your reply. This really helps.

Postie creates ShippingMethod models for each service.

Yes, I actually saw this and when I tried to add a new service to the list of services during some investigation/discovery. I noticed each service was a ShippingMethod model instance.

I guess to give you more context to my question above. I've built a custom provider specific to a clients shipping service API, that is also heavily integrated into Salesforce. The clients shipping service API will always return a service to use in the checkout step but somethings the cost is so low that the client is bleeding money when physically shipping the item as the cost to ship the item is more than the shipping service cost coming from the API. Sounds wrong/incorrect and something needs to change on the shipping service API side of things but its really specific to when certain conditions apply. Defo an edge case.

So what you could do is add your own shipping methods at this point.

This looks exactly like what I need. When looking at the codebase y'day, I actually saw this event but I didn't put 1 + 1 together and realise it was what I needed to register "an the fly" service as a ShippingMethod model instance.

All that said, when I couldn't figure it out y'day and after asking you guys, I ended up just adding the service as a provider via the Postie config and overwrote the cost when said specific conditions occurred and seems to be doing what the client needs, for now.

engram-design commented 2 years ago

No problem, and sounds good! I think that'll be the best way to handle it, but always open to suggestions to make things easier - if need be,