DELETING ALL BASKET SERVICES BY TYPE

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!
sixbynine
Posts: 93
Joined: 10 Jan 2018, 11:22

DELETING ALL BASKET SERVICES BY TYPE

Post by sixbynine » 02 Oct 2018, 12:21

I would like to delete all payment services of my current basket in one time :

1/ I tried :

Code: Select all

    $context = app()->make('\Aimeos\Shop\Base\Context')->get();
    $controller = \Aimeos\Controller\Frontend\Factory::createController($context, 'basket' );
    $basket = $controller->get();
    $basket->deleteService('payment' );
But it doesn't seem to work... do you know what is wrong?

2/ Is there a way to do that with JSON API?

Thank you very much,

sbn

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

Re: DELETING ALL BASKET SERVICES BY TYPE

Post by aimeos » 03 Oct 2018, 17:13

In principle, your code is correct. The only thing that might be a problem is that you might not have the right locale available if you don't pass the same site/language/currency parameters to your controller action that are available in Aimeos controller actions.

Sure, you can use the JSON REST API to remove the delivery or payment service from the basket. The JSON basket representation you get contains something like this:

Code: Select all

    "included": [{
        "id": "delivery",
        "type": "basket/service",
        "attributes": {
            "order.base.service.id": null,
            "order.base.service.code": "demo-dhl",
            "order.base.service.serviceid": "10",
            "order.base.service.name": "DHL",
            "order.base.service.mediaurl": "http://demo.aimeos.org/media/service/dhl.png",
            "order.base.service.type": "delivery",
            "order.base.service.price": "0.00",
            "order.base.service.costs": "5.90",
            "order.base.service.rebate": "0.00",
            "order.base.service.taxrate": "20.00"
        },
        "links": {
            "self": {
                "href": "http://localhost/jsonapi/basket/default/service/delivery",
                "allow": ["DELETE"]
            }
        }
    }]
Use the links/self/href URL in combination with the DELETE HTTP method to remove the service item from the basket.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

sixbynine
Posts: 93
Joined: 10 Jan 2018, 11:22

Re: DELETING ALL BASKET SERVICES BY TYPE

Post by sixbynine » 08 Oct 2018, 09:30

Ok thank you very much !

If I well understand, with JSON API, I need to :

- make an OPTIONS request
- then make another request to get the current basket content
- then create a loop on the response data and send a DELETE request for each 'basket/service' item

Is there a way to delete all services in only one request?

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

Re: DELETING ALL BASKET SERVICES BY TYPE

Post by aimeos » 09 Oct 2018, 19:52

You always need the OPTIONS request first to know what are your available resources but that request is only required once during/after the page load.

You are right, you need to get the basket but you can remove all delivery or payment service items in the basket at once using that link provided in the basket:

Code: Select all

        "links": {
            "self": {
                "href": "http://localhost/jsonapi/basket/default/service/delivery",
                "allow": ["DELETE"]
            }
        }
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

sixbynine
Posts: 93
Joined: 10 Jan 2018, 11:22

Re: DELETING ALL BASKET SERVICES BY TYPE

Post by sixbynine » 10 Oct 2018, 08:10

Ok !! Thank you very much :)

Post Reply