I have a page, almost exactly the default Commerce shipping.html template and flow. With just the default shipping method (Free), the user can simply click through without being required to select a shipping method. I've found how to use a Business Logic plugin to address required fields for the address/billing section, but there doesn't seem to be an event I can hook to so I can yell and provide a flash error message if the user clicks "Select Shipping Method" without actually selecting one.
I know I can redirect back to the shipping method page on the payment page by checking the cart (ex: {% if not cart.shippingMethod %}...) but this doesn't provide a flash error unless I use some hacky method like a GET string error message.
Is there an event I can hook to I'm not finding in the docs that will let me force the choice of a shipping method? Or a setting in the plugin I'm missing?
Edit
So I'm close, here's my event code that isn't working as I expect it to (did I tell you I'm new to Craft yet?):
craft()->on( 'commerce_orders.onSaveOrder', function ( Event $event ) {
$order = $event->params['order'];
if(craft()->request->getPost('validateShipping')){
// add error to shippingMethod and see if we can get it
$order->addError('shippingMethod',Craft::t('Please select a shipping option.'));
$event->performAction = false;
// pass a flash for testing
craft()->userSession->setFlash('error', 'No shipping elected');
}
});
So this moves on to the payment page and doesn't return to the shipping page with the added error. However, I am able to retrieve the set flash message on the payment page but {{ dump(cart.getErrors()) }} results in an empty array, so either the error is not attached to the cart properly in my event, or it is lost for some reason.
Edit
I see I'm getting some up views; If anyone can still help I'd appreciate it. I have yet to get this working as per the only answer, and would love to know what I'm doing wrong in my Craft Commerce event.
updateCartyou can simply POST the shipping method to automatically set the shipping method and simply remove shipping from your flow altogether. Or do you in fact have multiple shipping methods - it's not so clear above... – Jeremy Daalder Mar 24 '17 at 22:46shippingMethodradio array should be required and validated in the back end that some radio input has been selected. – jrothafer Mar 24 '17 at 23:01Please select a shipping method, etc...). – jrothafer Mar 24 '17 at 23:04updateCartwill always re-direct on success if there are no errors...you can't currently stop that. And setting theperformActionto false will stop it saving the order. (and therefore I guess the error). You're going to have to re-think how you do this and again, front end stuff is the way to go here most likely. – Jeremy Daalder Mar 30 '17 at 05:21