Error when addService.before is attached

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
kdim95
Advanced
Posts: 213
Joined: 26 Aug 2022, 12:17

Error when addService.before is attached

Post by kdim95 » 14 Sep 2023, 13:37

Laravel framework version: 10.16.1
Aimeos Laravel version: 2023.04.*
PHP Version: 8.2.8
Environment: Linux

Hello,

I get the following exception when adding a service using the jsonapi:
exception: Error
file: /var/www/vhosts/<domain>/httpdocs/vendor/aimeos/aimeos-core/src/MShop/Order/Item/Base.php
line: 655
message: Call to a member function getPrice() on bool

I'm not even doing anything when the addService.before event occurs.

This is the structure of my plugin:

Code: Select all

<?php

namespace Aimeos\MShop\Plugin\Provider\Order;

use Illuminate\Support\Facades\Http;

class MyPlugin
    extends \Aimeos\MShop\Plugin\Provider\Factory\Base
    implements \Aimeos\MShop\Plugin\Provider\Iface, \Aimeos\MShop\Plugin\Provider\Factory\Iface
{
    private $singleton;

    /**
     * Register plugin at publisher object
     */
    public function register( \Aimeos\MW\Observer\Publisher\Iface $publisher ) : \Aimeos\MW\Observer\Listener\Iface
    {
        $plugin = $this->object();
		$publisher->attach( $plugin, 'addService.before' );
        return $this;
    }

    public function update( \Aimeos\MW\Observer\Publisher\Iface $basket, $event, $service = null )
    {
        return true;
    }
}
This is the request:

Request URL - https://<domain>/jsonapi/basket?id=default&related=service&relatedid=delivery&_token=<token>
Payload:

Code: Select all

id: default
related: service
relatedid: delivery
_token: <token>
form data: {"data":[{"id":"service","attributes":{"service.id":"2"}}]}: 

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

Re: Error when addService.before is attached

Post by aimeos » 15 Sep 2023, 06:07

Seems like you service has no price and the plugin is not responsible for the exception:
https://github.com/aimeos/aimeos-core/b ... 23-L655C30
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

kdim95
Advanced
Posts: 213
Joined: 26 Aug 2022, 12:17

Re: Error when addService.before is attached

Post by kdim95 » 15 Sep 2023, 08:21

I've checked all of the services in the admin, they all have a price set.

I solved it, but I think that the documentation needs improvement, it doesn't specify this.

This is the line that causes the error.
$service = $this->notify( 'addService.before', $service );

The update() method of the plugin must return the service item, instead of "true", that's what causes the error "getPrice() on bool", because update() returned "true" instead of returning the service item.

kdim95
Advanced
Posts: 213
Joined: 26 Aug 2022, 12:17

Re: Error when addService.before is attached

Post by kdim95 » 15 Sep 2023, 08:33

This documentation needs more updating:
https://aimeos.org/docs/2023.x/provider ... ate-plugin

Please look at the notification names here:
Aimeos\MShop\Order\Item\Base

Some notifications specified in the documentation do not exist, or are wrong.
For example "setService.before" and "setService.after", I think they should be "addService.before" and "addService.after".

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

Re: Error when addService.before is attached

Post by aimeos » 15 Sep 2023, 09:49

You are right and the articles has been updated now
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply