1

I wrote a little commerce 2 price adjuster that works fine if i place it in /vendor/craftcms/commerce/src/adjusters/FriendsDiscountAdjuster.php and register it manually in /vendor/craftcms/commerce/src/services/OrderAdjustments.php

However, i wanted to port it to a plugin, but i do fail registering the adjuster to commerce.

I placed the adjuster in my vendor/pluginName/src/adjusters/ folder and added it to my plugin.

It fails with: Cannot declare class craft\commerce\adjusters\FriendsDiscountAdjuster, because the name is already in use

this is my main plugin file Friendsdiscount.php:

<?php
namespace hendrikeng\friendsdiscount;

use Craft;
use craft\base\Plugin;
use craft\services\Plugins;
use craft\events\PluginEvent;
use craft\commerce\services\OrderAdjustments;
use craft\events\RegisterComponentTypesEvent;
use hendrikeng\friendsdiscount\adjusters\FriendsDiscountAdjuster;

use yii\base\Event;

class Friendsdiscount extends Plugin
{
    public static $plugin;

    public $schemaVersion = '1.0.0';

    public function init()
    {
        parent::init();
        self::$plugin = $this;

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

the src/adjusters/FriendDiscountAdjuster.php:

<?php

namespace craft\commerce\adjusters;

use Craft;
use craft\commerce\base\AdjusterInterface;
use craft\commerce\elements\Order;
use craft\commerce\models\OrderAdjustment;
use craft\commerce\Plugin;

class FriendsDiscountAdjuster implements AdjusterInterface
{
  // ...Adjust something
}

1 Answer 1

2

the adjuster had the wrong namespace, changing it to:

namespace hendrikeng\friendsdiscount\adjusters

fixed the issue

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.