2

I am putting together my Business Logic plugin, as I want to validate the address fields throughout the checkout process of my Cart. I understand in order to do this I need to create a plugin and us the Craft Commerce commerce_addresses.onBeforeSaveAddress event.

I decided to try and use the https://github.com/lindseydiloreto/craft-businesslogic as a starting place.

My code is as follows;

public function init()
{
    craft()->on('commerce_addresses.onBeforeSaveAddress', function(Event $event){
        $address = $event->params['address'];

        if (empty($address->firstName))
        {
            $address->addError('firstName', Craft::t('Please enter your Name'));
             $event->performAction = false;
        }

        if (empty($address->address1))
        {
            $address->addError('address1', Craft::t('Please enter an Address'));
            $event->performAction = false;
        }

        if (empty($address->city))
        {
            $address->addError('city', Craft::t('Please enter your Town/City'));
            $event->performAction = false;
        }

        if (empty($address->zipCode))
        {
            $address->addError('zipCode', Craft::t('Please enter your Post Code'));
            $event->performAction = false;
        }

        if (empty($address->countryId))
        {
            $address->addError('countryId', Craft::t('Please select your Country'));
            $event->performAction = false;
        }
    });
}

If I have this code inside my to level plugin file named BusinessLogicPlugin.php it seems to work and it validates some of the fields as specified.

However, if I move this code into the controllers folder inside /controllers/BusinessLogicController.php it doesn't.

I am unsure what I need to do to move this out of the main/top level file, as I see this file getting very logon/messy if I need to add several 'business logics' to the site.

Can anyone shed light on this as to why it might not work within /controllers/

Thanks.

4

You can move it all into a Service and then call it from your main plugin file if you find it is getting unwieldy.

file: craft/plugins/businesslogic/BusinessLogicPlugin.php:

class BusinessLogicPlugin extends BasePlugin
{
    public function init()
    {
        craft()->on('commerce_addresses.onBeforeSaveAddress', function(Event $event)
        {
            craft()->businessLogic_events->onBeforeSaveAddress($event);
        });
    }
}

file: craft/plugins/businesslogic/services/BusinessLogic_EventsService.php:

class BusinessLogic_EventsService extends BaseApplicationComponent
{
    public function onBeforeSaveAddress(Event $event)
    {
        $address = $event->params['address'];

        if (empty($address->firstName))
        {
            $address->addError('firstName', Craft::t('Please enter your Name'));
            $event->performAction = false;

            // ...
        }
    }
}

Because $event is an object it will be passed by reference, and you don't need to return anything.

  • Thanks Mike. Should it not just work inside the controllers file? Or a better question might be what is the controllers folder/file for? – Terry Upton Aug 3 '16 at 22:08
  • 2
    Controllers can be though of as providing end point in to your plugin - i.e. they give you an action url you can hit to do something. That's not what you want/need here - here you instead register a method to run just before saveAddress is run, and in that method you're implementing some logic to do the validation - and services are the place you store your plugins logic. So Mike's srtucture above is the way to go (you can bung it all in the main plugin file but that soon gets messy, this is a better way to go for all but the most simple examples). – Jeremy Daalder Aug 4 '16 at 3:00

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.