1

I'm trying to create a module that lets me disable other shipping modes when free shipping is available.

I understand how to create a module and such, I can understand php relatively well I'm just lost as to how to filter this in regard to magento. I'm aware that you someone overwrite some existing rules within the module I'm making.

If someone could guide me as to how I do this within the xml file and the functions I need to use I'd be very grateful.

I've taken some steps into creating one myself after bits of googling.

<?xml version="1.0"?>
    <config>
        <modules>
            <Shipping_Onepage_Method>
                <version>1.0.0</version>
            </Shipping_Onepage_Method>
        </modules>

                    <blocks>
                        <checkout>
                            <rewrite>
                                <shipping_onepage_model_method>Shipping_Onepage_Model_Method</onepage_shipping_model_method>
                            </rewrite>
                        </checkout>
                    </blocks>

    </config>

Then I've got located in /public_html/app/code/local/Markshust/Helloworld/model/available.php

I've got the following code which I'm led to believe will filter the shipping prices as I wish.

 <?Php
class Shipping_Onepage_Model_Method extends Mage_Checkout_Block_Onepage_Shipping_Method_Available
{
    public function getShippingRates()
    {
        $rates = parent::getShippingRates();
        if (array_key_exists('freeshipping', $rates)) {
            $rates = array('freeshipping' => $rates['freeshipping']);
        }

        return $rates;
    }
}

?> 

This doesn't seem to work, I've made amends on my xml file since made aware of the faults in the latest awnser.

Thanks

4
  • just to clarify. With 'disabling other shipping methods', is your intention to not show other methods in checkout if free shipping is also an option available? – ProxiBlue Jan 24 '14 at 15:13
  • Yes Proxi, did I not explain it clearly enough if so I will edit my post. – dannyw24 Jan 24 '14 at 15:15
  • well, disable != not show - something can be disabled, and still appear visually;) – ProxiBlue Jan 24 '14 at 15:17
  • True, how would I go about disabled the other shipping options and visually making them not appear – dannyw24 Jan 24 '14 at 15:20
2

Config.xml code is wrong

   <?xml version="1.0"?>
    <config>
        <modules>
            <Shipping_Onepage>
                <version>1.0.0</version>
            </Shipping_Onepage>
        </modules>
<global>

                    <blocks>
            <shippingonepage>
            <class>Amit_RemoveShip_Model</class>
            </shippingonepage>

                        <checkout>
                            <rewrite>
                                <onepage_shipping_method_available>Shipping_Onepage_Block_Method</onepage_shipping_method_available>
                            </rewrite>
                        </checkout>
                    </blocks>
   <global>
    </config>

Method.php(Shipping>Onepag>Block>Method.php) code

   <?php
    class Shipping_Onepage_Block_Method extends Mage_Checkout_Block_Onepage_Shipping_Method_Available
{
    public function getShippingRates()
    {
        $rates = parent::getShippingRates();
        if (array_key_exists('freeshipping', $rates)) {
            $rates = array('freeshipping' => $rates['freeshipping']);
        }

        return $rates;
    }
}
1
1

Your rewrite directive is incorrect. Your directive says : rewrite the class located in 'markshust_helloworld', in the module 'checkout' with the class 'MagePsycho_Shipmentfilter_Block_Onepage_Shipping_Method_Available'

the correct rewrite format is:

<blocks>
   <module>
     <rewrite>
      <path_to_class>YOUR MODULE CLASS</path_to_class>
     </rewrite>
   </module>
</blocks>

so, if for instance you want to rewrite the core class 'Mage_Checkout_Block_Onepage_Shipping_Method' you'd rewrite it as such:

<blocks>
   <checkout>
     <rewrite>
       <onepage_shipping_method>YOUR MODULE CLASS</onepage_shipping_method>
     </rewrite>
   </checkout>
</blocks>

additionally, your <blocks> element is inside the <routers> element, which is incorrect. Blocks is not a property of the routers configuration.

Personally, I think creating a module to do this is overkill. You can use a bit of javascript (inserted to the checkout page via layout js directive in the checkout handles) to determine if freeshipping option is available, and if so set all the other shipping methods as hidden.

Off the op of my head something like this will iterate all the shipping methods, using jQuery

jQuery('#co-shipping-method-form input[type=radio]').each(function(){
            // test and do something with the shipping method
});

you can then also set the freeshipping option as checked at the same time.

3
  • Proxi can we continue this discussion in chat as I believe I may almost be there with your awnser – dannyw24 Jan 24 '14 at 15:38
  • it is now 23:45 here, and I am off to bed (just been waiting for my 1 year old to fall asleep - she is teething like mad, so up constantly), sorry. – ProxiBlue Jan 24 '14 at 15:43
  • No Problem - I shall pursue the module solution due to not been able to write JS thanks for pointing out my mistakes and goodluck with the baby :) – dannyw24 Jan 24 '14 at 15:44
1

first please rename your module to "Shipping_Method"

<modules>
    <Shipping_Method>
        <version>1.0.0</version>
    </Shipping_Method>
</modules>

Rename your Block file to: /app/code/local/Shipping/Method/Block/Shipping/Method.php

your rewrite configuration should be like:

<blocks>
    <checkout>
        <rewrite>
            <onepage_shipping_method_available>Shipping_Method_Block_Shipping_Method</onepage_shipping_method_available>
        </rewrite>
    </checkout>
</blocks>

in the Method.php:

<?php
class Shipping_Method_Block_Shipping_Method extends Mage_Checkout_Block_Onepage_Shipping_Method_Available
{
    public function getShippingRates()
    {
        $rates = parent::getShippingRates();
        if (array_key_exists('freeshipping', $rates)) {
            $rates = array('freeshipping' => $rates['freeshipping']);
        }

        return $rates;
    }
}

then it should work...

Not the answer you're looking for? Browse other questions tagged or ask your own question.