Answers.Body: <p>The error indicates that you're type-hinting the wrong class. Note this line in the error message:</p>
  <blockquote>
  <p>Argument 1 passed to amss\amss\AMSS::amss\amss{closure}() must be an instance of amss\amss\RegisterComponentTypesEvent, instance of craft\events\RegisterComponentTypesEvent given</p>
  </blockquote>
  <p>This comes from the type-hint in your function:</p>
  <pre class="lang-php prettyprint-override"><code>    function(RegisterComponentTypesEvent $event) {
          $event-&gt;types[] = Amss_StripeSurcharge::class;
      }
  </code></pre>
  <p>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 <code>amss\amss</code>, which means your type-hint essentially says <em>'This function expects an object of class <code>amss\amss\RegisterComponentTypesEvent</code>'</em>. But it receives an object of type <code>craft\events\RegisterComponentTypesEvent</code>, so the runtime type-check throws an error.</p>
  <p>To fix this, either specifiy a fully qualified namespace (<code>\craft\events\RegisterComponentTypesEvent</code>) or import the class at the top of the file with a <code>use</code> statement:</p>
  <pre class="lang-php prettyprint-override"><code>use craft\events\RegisterComponentTypesEvent;
  </code></pre>
  <p>This tells PHP that in this file, the class name <code>RegisterComponentTypesEvent</code> refers to <code>craft\events\RegisterComponentTypesEvent</code>.</p>
  <hr />
  <p>Once you've fixed this, you're might get another error because of this use-statement:</p>
  <pre class="lang-php prettyprint-override"><code>use Amss_StripeSurcharge;
  </code></pre>
  <p>This would indicate that your <code>Amss_StripeSurcharge</code> class has no namespace (i.e. lives in the root namespace). Good practice would be to namespace that file and class (e.g. as <code>amss\amss\adjuster\Amss_StripeSurcharge</code>. So your <code>Amss_StripeSurcharge</code> class would look something like this:</p>
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  namespace amss\amss\adjuster;
  
  class Amss_StripeSurcharge { /* ... */ }
  </code></pre>
  <p>Then you can add a <code>use</code> statement to import that class:</p>
  <pre class="lang-php prettyprint-override"><code>use \amss\amss\adjustor\Amss_StripeSurcharge;
  </code></pre>
  <p>Make sure to read up on <a href="https://www.php.net/manual/en/language.namespaces.php" rel="nofollow noreferrer">how namespaces work in PHP</a> to avoid errors like these!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-23T12:12:19.823Z
Answers.LastActivityDate: 2021-08-23T12:17:27.903Z
Answers.LastEditDate: 2021-08-23T12:17:27.903Z
Answers.UserDisplayName: MoritzLost
Body: <p>following <a href="https://craftcms.com/docs/commerce/3.x/extend/adjusters.html#writing-an-adjuster" rel="nofollow noreferrer">https://craftcms.com/docs/commerce/3.x/extend/adjusters.html#writing-an-adjuster</a> I am trying to make a custom adjuster:</p>
  <p>I placed</p>
  <pre><code>class myPlugin extends Plugin{
  Event::on(
      OrderAdjustments::class,
      OrderAdjustments::EVENT_REGISTER_ORDER_ADJUSTERS,
      function(RegisterComponentTypesEvent $event) {
          $event-&gt;types[] = Amss_StripeSurcharge::class;
      }
  );
  }
  </code></pre>
  <p>inside myPlugin.php</p>
  <p>and in the same file I have</p>
  <pre><code>use craft\commerce\services\OrderAdjustments;
  use yii\base\Event;
  use Amss_StripeSurcharge;
  </code></pre>
  <p>included in the top.</p>
  <p>I have the file Amss_StripeSurcharge.php in the same folder as myPlugin.php,</p>
  <p>which currently just has code copied and pasted from the documentation.</p>
  <p>However, when I press add items to cart, I get the error:</p>
  <blockquote>
  <p>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-&gt;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-&gt;trigger('registerOrderAd...', Object(craft\events\RegisterComponentTypesEvent))
  #4 C:\xampp\htdocs\newamss\craft\vendor\craftcms\commerce\src\elements\Order.php(1804): craft\commerce\services\OrderAdjustments-&gt;getAdjusters()
  #5 C:\xampp\htdocs\newamss\craft\vendor\craftcms\commerce\src\elements\Order.php(1905): craft\commerce\elements\Order-&gt;recalculate()
  #6 C:\xampp\htdocs\newamss\craft\vendor\craftcms\cms\src\services\Elements.php(2666): craft\commerce\elements\Order-&gt;afterSave(false)
  #7 C:\xampp\htdocs\newamss\craft\vendor\craftcms\cms\src\services\Elements.php(773): craft\services\Elements-&gt;_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-&gt;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-&gt;_returnCart()
  #10 [internal function]: craft\commerce\controllers\CartController-&gt;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-&gt;runWithParams(Array)
  #13 C:\xampp\htdocs\newamss\craft\vendor\craftcms\cms\src\web\Controller.php(190): yii\base\Controller-&gt;runAction('update-cart', Array)
  #14 C:\xampp\htdocs\newamss\craft\vendor\yiisoft\yii2\base\Module.php(534): craft\web\Controller-&gt;runAction('update-cart', Array)
  #15 C:\xampp\htdocs\newamss\craft\vendor\craftcms\cms\src\web\Application.php(276): yii\base\Module-&gt;runAction('commerce/cart/u...', Array)
  #16 C:\xampp\htdocs\newamss\craft\vendor\craftcms\cms\src\web\Application.php(585): craft\web\Application-&gt;runAction('commerce/cart/u...', Array)
  #17 C:\xampp\htdocs\newamss\craft\vendor\craftcms\cms\src\web\Application.php(255): craft\web\Application-&gt;_processActionRequest(Object(craft\web\Request))
  #18 C:\xampp\htdocs\newamss\craft\vendor\yiisoft\yii2\base\Application.php(392): craft\web\Application-&gt;handleRequest(Object(craft\web\Request))
  #19 C:\xampp\htdocs\newamss\craft\web\index.php(26): yii\base\Application-&gt;run()
  #20 {main}</p>
  </blockquote>
  <p>myPlugin and myPlugin.php are just madeup names for what the name of the actual plugin is called, which is called amss</p>
  <p>Any help on how I can fix this error would be appreciated!</p>
  
Tags: <craft3><plugin-development><plugin-craftcommerce><adjusters>
Title: Can't register custom adjuster with Craft CMS commerce
ThreadText: # Can't register custom adjuster with Craft CMS commerce
  
  (Asked by: username_0 on 2021-08-23)
  
  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!
  
  **Tags:** craft3, plugin-development, plugin-craftcommerce, adjusters
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-08-23. Score: 4 | Accepted answer
  
  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:
  
  ```php
      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:
  
  ```php
  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:
  
  ```php
  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
  <?php
  namespace amss\amss\adjuster;
  
  class Amss_StripeSurcharge { /* ... */ }
  
  ```
  
  Then you can add a `use` statement to import that class:
  
  ```php
  use \amss\amss\adjustor\Amss_StripeSurcharge;
  
  ```
  
  Make sure to read up on [how namespaces work in PHP](https://www.php.net/manual/en/language.namespaces.php) to avoid errors like these!
  
  
  ---
  
  