What if we want to only show a certain shipping method to certain customers?

For this we need to tap in to the collectCarrierRates() function located in Mage_Shipping_Model_Shipping class.

Unfortunately the only way for us to do this is with a rewrite.

So, in your config.xml set up the rewrite;

...
<global>
    ...
    <models>
        ...
        <shipping>
            <rewrite>
                <shipping>[Vendor]_[ModuleName]_Model_Shipping_Shipping</shipping>
            </rewrite>
        </shipping>
        ...
    </models>
    ...
</global>
...

Then in Model/Shipping/Shipping.php

<?php

class [Vendor]_[ModuleName]_Model_Shipping_Shipping extends Mage_Shipping_Model_Shipping
{
    public function collectCarrierRates($carrierCode, $request)
    {
        if ($carrierCode === 'ourcarriercode') {
            if (1 == 2) {
                // carrier not available
                return $this;
            }
        }
        
        return parent::collectCarrierRates($carrierCOde, $request);
    }
}

Too much Shipping? :p

Because we only want to do the custom logic on a particular shipping carrier, and because this method is called for every available carrier, we need to first check the current carrier code is the one we want to perform our custom logic on.

Obviously replace if (1 == 2) with something sensible, obviously.

Why return $this?

It is a little odd but basically by returning the class in it’s current state it no longer sees this as an available shipping method (because it has not added it to its list of results).

Magic.

  • magento

Like this post? Share it :)


Related Posts

Back