Dynamic delivery services

How to configure and adapt Aimeos based shops as developer
Forum rules
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
createanet
Posts: 72
Joined: 22 Mar 2021, 16:56

Dynamic delivery services

Post by createanet » 11 May 2021, 08:22

Hello,

A customer has options to choose a delivery service.

Im aware we can use a decorator around the service to calculate a price via a 3rd party api based on the basket contents.

Could you please tell me if it's possible to provide user with options with this approach also?

We are integrating with DHL, they have dynamic delivery methods based on delivery region, contents, weight etc. There can and will be more than one delivery option available to the customer. Meaning that a request would need to be made with the basket, a response is received with options for the customer to select and then assign.

Either current delivery service list would need to be replaced or appended with options available from an API request, or a list of options for the chosen service need to be presented to the customer.

Thanks in advance.

User avatar
aimeos
Administrator
Posts: 7866
Joined: 01 Jan 1970, 00:00

Re: Dynamic delivery services

Post by aimeos » 12 May 2021, 07:02

It's not possible to create delivery options dynamically I think but you can add option the customer can select from to the delivery option your decorator is applied to. The same happens in the time and supplier decorator:
- https://github.com/aimeos/aimeos-core/t ... r/Time.php
- https://github.com/aimeos/aimeos-core/t ... pplier.php
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

createanet
Posts: 72
Joined: 22 Mar 2021, 16:56

Re: Dynamic delivery services

Post by createanet » 12 May 2021, 11:29

Perfect, Ive just had a look at the default example for the supplier provider.

This seems like it will allow me to present the delivery options to the customer over a generic service.

Thank you

createanet
Posts: 72
Joined: 22 Mar 2021, 16:56

Re: Dynamic delivery services

Post by createanet » 17 May 2021, 13:14

How is it best to avoid multiple request to the API for this data?

Obviously it looks like I 'll need to cache this response and possibly store in the session, but at what point can I decide that it needs busting and a fresh request being made? For me this would be anytime the user changes their address or modifies their basket would require an updated rate from the API.

Im sure ive read previous advice regarding this in the forums, but im having problems locating the threads.

Thanks,

createanet
Posts: 72
Joined: 22 Mar 2021, 16:56

Re: Dynamic delivery services

Post by createanet » 18 May 2021, 06:58

createanet wrote: 11 May 2021, 08:22 Hello,

A customer has options to choose a delivery service.

Im aware we can use a decorator around the service to calculate a price via a 3rd party api based on the basket contents.

Could you please tell me if it's possible to provide user with options with this approach also?

We are integrating with DHL, they have dynamic delivery methods based on delivery region, contents, weight etc. There can and will be more than one delivery option available to the customer. Meaning that a request would need to be made with the basket, a response is received with options for the customer to select and then assign.

Either current delivery service list would need to be replaced or appended with options available from an API request, or a list of options for the chosen service need to be presented to the customer.

Thanks in advance.
For now I am calling the API from the constructor of a service decorator, this serializes the response, stores in the session and updates the service config required to construct the options for the customer to select.

If there are any changes to products, or address I am using a basket plugin to check for the existence of the session and clear, which will then force a new request for the updated rates of the service deliveries.

If there is another approach or something glaringly obvious I am missing would it be possible to share please aimeos?

User avatar
aimeos
Administrator
Posts: 7866
Joined: 01 Jan 1970, 00:00

Re: Dynamic delivery services

Post by aimeos » 19 May 2021, 07:54

You don't need the basket plugin if you create a hash over the address and products. You can use

Code: Select all

$hash = md5( json_encode( $address->toArray() ) . json_encode( $products->call( 'toArray' ) ) );
Then, store that hash in your order service item as attribute using setAttribute() method and check the stored hash against the hash you create when the service object is used.

We would be happy if you would share your code in an Aimeos extension because you are not the first one implementing a DHL decorator :-)
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

createanet
Posts: 72
Joined: 22 Mar 2021, 16:56

Re: Dynamic delivery services

Post by createanet » 19 May 2021, 08:15

Arghh this is good, thank you :)

Once im complete id be happy to wrap in an extension and share with you for review.

I will look to make these changes you have suggested.

createanet
Posts: 72
Joined: 22 Mar 2021, 16:56

Re: Dynamic delivery services

Post by createanet » 21 May 2021, 10:29

For anyone else reading this and wanting to use this hash, the correct hash appears to be

Code: Select all

$hash = md5( json_encode( $address->toArray() ) . json_encode( $products->call( 'toArray' )->toArray() ) );
The initial call to toArray returns a Map which when encoded will return an empty object, thus changing quantities of the existing products in the basket will not trigger a negative comparison on the hash.

Ive not yet figured out how I can call setAttributes from my constructor, as the $serviceItem passed into the constructor is an instance of \Aimeos\MShop\Service\Item\Iface and the setAttributes requires \Aimeos\MShop\Order\Item\Base\Service\Iface. I don't know if this is because the order doesn't yet exist as im working with the basket from the session.

User avatar
aimeos
Administrator
Posts: 7866
Joined: 01 Jan 1970, 00:00

Re: Dynamic delivery services

Post by aimeos » 23 May 2021, 08:47

You can also store the hash in the session if no basket is available yet:

Code: Select all

$context->session()->set( 'aimeos/dhl-hash', $hash );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

createanet
Posts: 72
Joined: 22 Mar 2021, 16:56

Re: Dynamic delivery services

Post by createanet » 24 May 2021, 06:32

This was the route I had taken on the friday, with a view to change if there was something I had missed relating the the order/basket. Thanks for the confirmation :)

Post Reply