I had to create a custom shipping méthod with some conditions to be valid. I need a validation step during my checkout, and I found a method without override Magento core.

In your config.xml, add the following observer :

<?xml version="1.0"?>
<config>
    <!-- ... -->
	<global>
        <!-- ... -->
        <events>
			<!-- ... -->
            <controller_action_postdispatch_checkout_onepage_saveShippingMethod>
                <observers>
                    <monmodule_shipping_method_observer_address>
                        <class>Mypackage_Mymodule_Model_Checkout_ShippingMethod_Observer</class>
                        <method>myMethod</method>
                    </monmodule_shipping_method_observer_address>
                </observers>
            </controller_action_postdispatch_checkout_onepage_saveShippingMethod>
			<!-- ... -->
        </events>
        <!-- ... -->
	</global>
	<!-- ... -->
</config>

And in the Observer’s class (/app/code/local/Mypackage/Mymodule/Model/Checkout/ShippingMethod/Observer.php) :

<?php

class Mypackage_Mymodule_Model_Checkout_ShippingMethod_Observer
{
	public function myMethod($observer)
    {
		/* Your logic */
		if($error){
			$message = Mage::helper('mymodule')->__('Your error message');
			$result = array('error' => 1, 'message' => $message);
			$controllerAction = $observer->getEvent()->getControllerAction();
			$controllerAction->getResponse()->setBody(Zend_Json::encode($result));
		}
		
		return $this
    }
}

You will have an error message in the checkout, and the visitor need to have no error to continue.

Add validation on a custom shipping method
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies to ensure that we give you the best experience on our website.
Ok