This is a Craft Commerce question and it's very similar to this question, but I believe it's different.

I need to validate some custom fields inside product variant before saving, so I added following script to my plugin but got no luck:

craft()->on('commerce_products.onBeforeSaveProduct', function (Event $event) {
  if (condition) {
    $variant = $event->params['product']->variants[0];
    if ($variant) {
      $variant->addError('customFieldHandler', 'Some Error Message');
      $event->performAction = false;

With the code above, you can't save the entry if the variant is invalidated, but the error message is not showing up without any error. The addError method works fine for all craft build-in fields, just not for the field in variant. Any help would be appreciated.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.