3

Starting with the Business Logic plugin from https://github.com/lindseydiloreto/craft-businesslogic I'm following 3 for 2 etc with Craft Commerce to get started.

I have my main plugin file containing:

<?php
namespace Craft;
require_once('Adjusters/BusinessLogic_TaxRemover.php');

class BusinessLogicPlugin extends BasePlugin
{

    public function getName()
    {
        return 'BusinessLogic';
    }

    public function getDescription()
    {
        return '';
    }

    public function getVersion()
    {
        return '1.0.0';
    }

    public function getDeveloper()
    {
        return craft()->getSiteName();
    }

    public function getDeveloperUrl()
    {
        return '';
    }

    public function init()
    {
    }

    public function commerce_registerOrderAdjusters(){

        return [

            new Commerce_TaxAdjuster

        ];
    }

}

And within Adjusters/BusinessLogic_TaxRemover.php I have:

<?php

namespace Commerce\Adjusters;
use Craft\Commerce_LineItemModel;
use Craft\Commerce_OrderAdjustmentModel;
use Craft\Commerce_OrderModel;

class BusinessLogic_TaxRemover implements Commerce_AdjusterInterface {

    public function adjust(Commerce_OrderModel &$order, array $lineItems = []){

        $myAdjuster = new Commerce_OrderAdjustmentModel();

        $order->baseDiscount = $order->baseDiscount - 5;

        $myAdjuster->type = "Test";
        $myAdjuster->name = "Test Adjuster";
        $myAdjuster->description = "Takes $5 off the order";
        $myAdjuster->amount = -5.0;
        $myAdjuster->orderId = $order->id;

        //if your Adjuster affects lineItems rather than the total, you record the ids here
        $myAdjuster->optionsJson = ['lineItemsAffected'=>null];
        $myAdjuster->included = false;

        return [$myAdjuster];

    }

}

But whenever doing anything to the cart, I just get the error:

Error

Class 'Craft\BusinessLogic_TaxRemover' not found

C:\Users\clive\Desktop\repos\_craft\craft\plugins\businesslogic\BusinessLogicPlugin.php(38)

26     }
27 
28     public function getDeveloperUrl()
29     {
30         return '';
31     }
32 
33     public function init()
34     {
35     }
36     public function commerce_registerOrderAdjusters(){
37            return [
38             new BusinessLogic_TaxRemover
39             
40            ];
41     }
42 
43 }
4

To get an adjuster working, it's really all about consistent naming and making sure PHP can find the class. Here are the main things to address:

Your adjuster should be defined in a file that matches it's classname

So in this case a folder off the main plugin folder:

 /craft/plugins/businesslogic/Adjusters/BusinessLogic_TaxRemover.php

Include the Adjuster class file.

You can do this (at least) two ways:

include or require the adjuster file in your main plugin class

e.g.

<?php
namespace Craft;

require_once('Adjusters/BusinessLogic_TaxRemover.php');

OR you can use Craft::import (which is probably the better way I guess?)

Craft::import('plugins.businesslogic.adjusters.BusinessLogic_TaxRemover');

** The Classname must match the filename **

So e.g. in this case

<?php

namespace Commerce\Adjusters;
use Craft\Commerce_LineItemModel;
use Craft\Commerce_OrderAdjustmentModel;
use Craft\Commerce_OrderModel;

class BusinessLogic_TaxRemover implements Commerce_AdjusterInterface {

   etc
}

You must also finally register your Adjuster with commerce

And again you must use the same classname (and be mindful of namespaces!)
So in this example:

public function commerce_registerOrderAdjusters(){
    return [
        new \Commerce\Adjusters\BusinessLogic_TaxRemover

    ];
}
  • I think I've done that already, within the init() function? – Clive Portman Apr 3 '17 at 13:42
  • I've also tried require_once('adjusters/GiftVouchers_Adjusters.php'); after defining the namespace. Neither seem to do anything so I think it must be a routing issue. – Clive Portman Apr 3 '17 at 13:45
  • Yeah looking at it on a bigger screen, Brad is right. I use e.g. ``` <?php namespace Craft; require_once('Adjusters/BusinessLogic_TaxRemover.php'); ``` ...at the top, that works. But my file name is Adjusters/BusinessLogic_TaxRemover.php – Jeremy Daalder Apr 4 '17 at 0:04
  • (the import arrpoach will work too, with the right name) – Jeremy Daalder Apr 4 '17 at 0:04
  • 1
    I don't suppose you have a project on GitHub available that has this working code? I've put mine here if that helps: github.com/cliveportman/craftcommerce-adjuster – Clive Portman Apr 4 '17 at 9:11
1

From your code examples, I believe the issue if you have a file named adjusters/BusinessLogicAdjusters.php, but the class name for that file is BusinessLogic_TaxRemover.

  • I've renamed adjusters/BusinessLogicAdjusters.php to Adjusters/BusinessLogic_TaxRemover.php, used require_once('Adjusters/BusinessLogic_TaxRemover.php'); after the namespace in the main plugin file but still no luck. I've edited my question with the updated code. – Clive Portman Apr 4 '17 at 9:07
  • Brad is still on the money here, it's a naming issue still...in this case you have the wrong classname in commerce_registerOrderAdjusters() - you need to change that to new BusinessLogic_TaxRemover - I put up an issue on your github – Jeremy Daalder Apr 5 '17 at 0:40
  • See my updated answer for a more complete answer... – Jeremy Daalder Apr 5 '17 at 0:52
  • Thanks for you help Jeremy. I've made fixed your issue on Github, correcting the register of the Adjuster, but I get the same error. I think I'd actually tried that previously and had no luck with it. I've updated my question to include the corrected code. – Clive Portman Apr 5 '17 at 8:25

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.