1

In Craft Commerce, is it possible to make the product variant weight a required field?

We are using weight to calculate shipping so it would be good if we could make it a required field to ensure that the store admin doesn't forget to enter it.

Thanks

3

Yes you can, using the Model::EVENT_DEFINE_RULES Put this in your customer module or plugin.

        use craft\commerce\elements\Variant;
        use craft\base\Model;
       use craft\events\DefineRulesEvent;
        
        Event::on(
            Variant::class,
            Model::EVENT_DEFINE_RULES,
            function(DefineRulesEvent $event) {
                $rules = $event->rules;
                $rules[] = [['weight'], 'required'];
                $event->rules = $rules;
            }
        );

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.