6

Can someone break down how I should validate empty address fields as a plugin for Craft Commerce and take the following answer further?

The Craft Commerce documentation provides the following example for the name field - but I'm not sure what my next steps should be...

public function init()
{
    craft()->on('commerce_addresses.onBeforeSaveAddress', function($event){
            $address = $event->params['address'];
            $address->addError('firstName','Not a cool name man.');
            $event->performAction = false;
    });
}
4

You'll need to add some kind of validation query to the field first. Something along the lines of this would work:

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

   if (empty($address->address1))
   {
     $address->addError('address1', Craft::t('Address Line 1 is required'));
     $event->performAction = false;
   }

   if ($address->city == 'London')
   {
     $address->addError('city', Craft::t('Sorry, you can\'t be from London'));
     $event->performAction = false;
   }
});  
| improve this answer | |
  • Has someone got this to work? I can’t seem to. Using the business logic plugin and registering the action, the form submits but gets redirected to itself without throwing an errors. Has anything changed? Stil trying to find my way around regarding plugins. – Marcus Scheller Dec 19 '16 at 14:17
  • You won't need to register an action for this to work. The event is triggered by commerce updating an address on an order/cart. Make sure the event is being triggered by debugging with a simple Craft::dd('address about to be saved') – Luke Pearce Dec 19 '16 at 14:21
  • Alright, dumb question:: that goes into to businessLogicController.php in this case, right? Now it completes the step without validation. – Marcus Scheller Dec 19 '16 at 14:26
  • The function in my original answer would go inside the init() function in BusinessLogicPlugin.php. A lot of the time errors happen due to filename formatting - using something like pluginfactory.io to generate your plugin will help avoid this. – Luke Pearce Dec 19 '16 at 14:28
3

In Craft3 you can do it like this:

use Craft;
use craft\base\Plugin;
use craft\commerce\models\Address;
use yii\base\Event;

class Custom extends Plugin
{
    public function init()
    {
        parent::init();

        Event::on(Address::class, Address::EVENT_BEFORE_VALIDATE, function (Event $event) {
            $address = $event->sender;
            if (empty($address->address1)) {
                $address->addError('address1', Craft::t('app', 'Address Line 1 is required'));
                $event->handled = true; // stop other handlers for this event
            }
        });
    }
}
| improve this answer | |
1

I've used the Business Logic plugin to check certain fields before payment processing rather than on regular validation. This way I can save the address while entering the address-data with JavaScript, but payments fail until these fields have been validated properly.

Adding this to BusinessLogic.php

use craft\commerce\services\Payments;
use craft\commerce\events\ProcessPaymentEvent;
use craft\commerce\errors\PaymentException;

And this in the init() function

Event::on(Payments::class, Payments::EVENT_BEFORE_PROCESS_PAYMENT, function(ProcessPaymentEvent $event) {
  $shippingAddress = $event->order['shippingAddress'];
  if(empty($shippingAddress->address1) || empty($shippingAddress->zipCode) || empty($shippingAddress->city)) {
    throw new PaymentException(Craft::t('commerce', 'No valid address entered.'));
  }
});
| improve this answer | |

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.