Magento – How to reset or reinitialize the shipping method on cart change

shipping-methods

Is there a way to reinitialise the shipping method. Sometimes customers wants to back and add a product to the shopping cart, after they have already chosen a shipping method. I would like to remove the already selected shipping methode.

I'm using the following shipping methodes:

<input name="shipping_method" type="radio" value="freeshipping_freeshipping" id="s_method_freeshipping_freeshipping" class=" radio">

<input name="shipping_method" type="radio" value="tablerate_bestway" id="s_method_tablerate_bestway" class="radio">

Other usefull info:

save: function(){
    if (checkout.loadWaiting!=false) return;
    var validator = new Validation(this.form);
    if (validator.validate()) {
        checkout.setLoadWaiting('shipping');
        var request = new Ajax.Request(
            this.saveUrl,
            {
                method:'post',
                onComplete: this.onComplete,
                onSuccess: this.onSave,
                onFailure: checkout.ajaxFailure.bind(checkout),
                parameters: Form.serialize(this.form)
            }
        );
    }
},

resetLoadWaiting: function(transport){
    checkout.setLoadWaiting(false);
},
nextStep: function(transport){
    if (transport && transport.responseText){
        try{
            response = eval('(' + transport.responseText + ')');
        }
        catch (e) {
            response = {};
        }
    }
    if (response.error){
        if ((typeof response.message) == 'string') {
            alert(response.message);
        } else {
            if (window.shippingRegionUpdater) {
                shippingRegionUpdater.update();
            }
            alert(response.message.join("\n"));
        }

        return false;
    }

    checkout.setStepResponse(response);

    /*
     var updater = new Ajax.Updater(
     'checkout-shipping-method-load',
     this.methodsUrl,
     {method:'get', onSuccess: checkout.setShipping.bind(checkout)}
     );
     */
    //checkout.setShipping();
}
}

// shipping method
var ShippingMethod = Class.create();
ShippingMethod.prototype = {
    initialize: function(form, saveUrl){
        this.form = form;
        if ($(this.form)) {
            $(this.form).observe('submit', function(event){this.save();Event.stop(event);}.bind(this));
        }
        this.saveUrl = saveUrl;
        this.validator = new Validation(this.form);
        this.onSave = this.nextStep.bindAsEventListener(this);
        this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
    },

    validate: function() {
        var methods = document.getElementsByName('shipping_method');
        if (methods.length==0) {
            alert(Translator.translate('Your order cannot be completed at this time as there is no shipping methods available for it. Please make necessary changes in your shipping address.').stripTags());
            return false;
        }

        if(!this.validator.validate()) {
            return false;
        }

        for (var i=0; i<methods.length; i++) {
            if (methods[i].checked) {
                return true;
            }
        }
        alert(Translator.translate('Please specify shipping method.').stripTags());
        return false;
    },

Can you also provide the file that I need to modify? I'm quit new to magento and not sure of what I need to modify. I did some research and found YourModule/etc/config.xml in the section and YourModule/Model/Observer.php.

Best Answer

Use checkout_cart_update_items_before event:

<checkout_cart_update_items_before>
    <observers>
        <mymodule_observer>
            <type>singleton</type>
            <class>Mynamespace_Mymodule_Model_Observer</class>
            <method>updateMethod</method>
        </mymodule_observer>
    </observers>
</checkout_cart_update_items_before>

In the observer -> updateMethod() write following code :

$quote = Mage::helper('checkout/cart')->getCart()->getQuote(); 
$shippingMethod = $quote->getShippingAddress()->getShippingMethod();
if($shippingMethod){
    $quote->getShippingAddress()->setShippingMethod(null);  //setting method to null
    $quote->save(); 
}
Related Topic