1

following https://craftcms.com/docs/commerce/3.x/extend/adjusters.html#writing-an-adjuster I am trying to make a custom adjuster:

I placed

class myPlugin extends Plugin{
Event::on(
    OrderAdjustments::class,
    OrderAdjustments::EVENT_REGISTER_ORDER_ADJUSTERS,
    function(RegisterComponentTypesEvent $event) {
        $event->types[] = Amss_StripeSurcharge::class;
    }
);
}

inside myPlugin.php

and in the same file I have

use craft\commerce\services\OrderAdjustments;
use yii\base\Event;
use Amss_StripeSurcharge;

included in the top.

I have the file Amss_StripeSurcharge.php in the same folder as myPlugin.php,

which currently just has code copied and pasted from the documentation.

However, when I press add items to cart, I get the error:

TypeError: Argument 1 passed to amss\amss\AMSS::amss\amss{closure}() must be an instance of amss\amss\RegisterComponentTypesEvent, instance of craft\events\RegisterComponentTypesEvent given in C:\xampp\htdocs\newamss\craft\plugins\craft-AMSS\src\AMSS.php:205 Stack trace: #0 [internal function]: amss\amss\AMSS->amss\amss{closure}(Object(craft\events\RegisterComponentTypesEvent)) #1 C:\xampp\htdocs\newamss\craft\vendor\yiisoft\yii2\base\Event.php(312): call_user_func(Object(Closure), Object(craft\events\RegisterComponentTypesEvent)) #2 C:\xampp\htdocs\newamss\craft\vendor\yiisoft\yii2\base\Component.php(637): yii\base\Event::trigger('craft\commerce\...', 'registerOrderAd...', Object(craft\events\RegisterComponentTypesEvent)) #3 C:\xampp\htdocs\newamss\craft\vendor\craftcms\commerce\src\services\OrderAdjustments.php(103): yii\base\Component->trigger('registerOrderAd...', Object(craft\events\RegisterComponentTypesEvent)) #4 C:\xampp\htdocs\newamss\craft\vendor\craftcms\commerce\src\elements\Order.php(1804): craft\commerce\services\OrderAdjustments->getAdjusters() #5 C:\xampp\htdocs\newamss\craft\vendor\craftcms\commerce\src\elements\Order.php(1905): craft\commerce\elements\Order->recalculate() #6 C:\xampp\htdocs\newamss\craft\vendor\craftcms\cms\src\services\Elements.php(2666): craft\commerce\elements\Order->afterSave(false) #7 C:\xampp\htdocs\newamss\craft\vendor\craftcms\cms\src\services\Elements.php(773): craft\services\Elements->_saveElementInternal(Object(craft\commerce\elements\Order), false, false, true) #8 C:\xampp\htdocs\newamss\craft\vendor\craftcms\commerce\src\controllers\CartController.php(403): craft\services\Elements->saveElement(Object(craft\commerce\elements\Order), false, false, true) #9 C:\xampp\htdocs\newamss\craft\vendor\craftcms\commerce\src\controllers\CartController.php(238): craft\commerce\controllers\CartController->_returnCart() #10 [internal function]: craft\commerce\controllers\CartController->actionUpdateCart() #11 C:\xampp\htdocs\newamss\craft\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array) #12 C:\xampp\htdocs\newamss\craft\vendor\yiisoft\yii2\base\Controller.php(181): yii\base\InlineAction->runWithParams(Array) #13 C:\xampp\htdocs\newamss\craft\vendor\craftcms\cms\src\web\Controller.php(190): yii\base\Controller->runAction('update-cart', Array) #14 C:\xampp\htdocs\newamss\craft\vendor\yiisoft\yii2\base\Module.php(534): craft\web\Controller->runAction('update-cart', Array) #15 C:\xampp\htdocs\newamss\craft\vendor\craftcms\cms\src\web\Application.php(276): yii\base\Module->runAction('commerce/cart/u...', Array) #16 C:\xampp\htdocs\newamss\craft\vendor\craftcms\cms\src\web\Application.php(585): craft\web\Application->runAction('commerce/cart/u...', Array) #17 C:\xampp\htdocs\newamss\craft\vendor\craftcms\cms\src\web\Application.php(255): craft\web\Application->_processActionRequest(Object(craft\web\Request)) #18 C:\xampp\htdocs\newamss\craft\vendor\yiisoft\yii2\base\Application.php(392): craft\web\Application->handleRequest(Object(craft\web\Request)) #19 C:\xampp\htdocs\newamss\craft\web\index.php(26): yii\base\Application->run() #20 {main}

myPlugin and myPlugin.php are just madeup names for what the name of the actual plugin is called, which is called amss

Any help on how I can fix this error would be appreciated!

3

The error indicates that you're type-hinting the wrong class. Note this line in the error message:

Argument 1 passed to amss\amss\AMSS::amss\amss{closure}() must be an instance of amss\amss\RegisterComponentTypesEvent, instance of craft\events\RegisterComponentTypesEvent given

This comes from the type-hint in your function:

    function(RegisterComponentTypesEvent $event) {
        $event->types[] = Amss_StripeSurcharge::class;
    }

Since you're not specifying a full qualified namespace, the class name is evaluated relative to the current namespace. Looks like your file is namespaced as amss\amss, which means your type-hint essentially says 'This function expects an object of class amss\amss\RegisterComponentTypesEvent'. But it receives an object of type craft\events\RegisterComponentTypesEvent, so the runtime type-check throws an error.

To fix this, either specifiy a fully qualified namespace (\craft\events\RegisterComponentTypesEvent) or import the class at the top of the file with a use statement:

use craft\events\RegisterComponentTypesEvent;

This tells PHP that in this file, the class name RegisterComponentTypesEvent refers to craft\events\RegisterComponentTypesEvent.


Once you've fixed this, you're might get another error because of this use-statement:

use Amss_StripeSurcharge;

This would indicate that your Amss_StripeSurcharge class has no namespace (i.e. lives in the root namespace). Good practice would be to namespace that file and class (e.g. as amss\amss\adjuster\Amss_StripeSurcharge. So your Amss_StripeSurcharge class would look something like this:

<?php
namespace amss\amss\adjuster;

class Amss_StripeSurcharge { /* ... */ }

Then you can add a use statement to import that class:

use \amss\amss\adjustor\Amss_StripeSurcharge;

Make sure to read up on how namespaces work in PHP to avoid errors like these!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.