0

we are restricting the zip codes for custom shipping method....

for now we are facing problem as here :

Display the shipping charges without clicking on "check" button

so is there any other way we can allow only some zip codes and restrict other zip codes.

can we do restricting zip codes independent of shipping method

  • You can try extending collectRates method in class Mage_Shipping_Model_Shipping. I believe this is the class responsible for collecting all the shipping rates. This way, you could achieve restricting methods on the basis of zip-codes globally. – Prateek Mar 18 '16 at 8:55
  • @Prateek can you please post the answer with code for extending the collectrates method. – Baby in Magento Mar 18 '16 at 9:20
  • @Prateek before, you only helped me to restrict and allow only some zip codes for custom shipping method. now as we hide the shipping method, its giving problem in above link. – Baby in Magento Mar 18 '16 at 10:07
4

You can go with re-write of the core Shipping model. Inside your extension config, create a re-write of that model, and in your extended model file, the content will go something like this :

<?php
class Webkul_Mpperproductshipping_Model_Shipping extends Mage_Shipping_Model_Shipping {

    public function collectRates(Mage_Shipping_Model_Rate_Request $request) {
        $storeId = $request->getStoreId();
        if (!$request->getOrig()) {
            $request
                ->setCountryId(Mage::getStoreConfig(self::XML_PATH_STORE_COUNTRY_ID, $request->getStore()))
                ->setRegionId(Mage::getStoreConfig(self::XML_PATH_STORE_REGION_ID, $request->getStore()))
                ->setCity(Mage::getStoreConfig(self::XML_PATH_STORE_CITY, $request->getStore()))
                ->setPostcode(Mage::getStoreConfig(self::XML_PATH_STORE_ZIP, $request->getStore()));
        }

        $postCode = $request->getDestPostcode();
        $restrictedCodes = array(
            110001,
            110002
            );
        //restricted values. they can come from anywhere

        if (!in_array($postCode, $restrictedCodes)) {
            return $this;
        }

        $limitCarrier = $request->getLimitCarrier();
        if (!$limitCarrier) {
            $carriers = Mage::getStoreConfig('carriers', $storeId);

            foreach ($carriers as $carrierCode => $carrierConfig) {
                $this->collectCarrierRates($carrierCode, $request);
            }
        } else {
            if (!is_array($limitCarrier)) {
                $limitCarrier = array($limitCarrier);
            }
            foreach ($limitCarrier as $carrierCode) {
                $carrierConfig = Mage::getStoreConfig('carriers/' . $carrierCode, $storeId);
                if (!$carrierConfig) {
                    continue;
                }
                $this->collectCarrierRates($carrierCode, $request);
            }
        }

        return $this;
    }

}

Basically, you are just re-writing the collectRates method of the parent class as per your requirements.

I couldn't get a chance to test the code, but it should give you some start for the point. My thoughts say that if this code is in place, you should be able to switch off globally all shipping methods in the site if destination code is not present in the array of allowed codes.

Based upon your config file, this code should replace the original models block :

<models>
    <mpperproductshipping>
        <class>Webkul_Mpperproductshipping_Model</class>
    </mpperproductshipping>
    <shipping>
        <rewrite>
            <shipping>Webkul_Mpperproductshipping_Model_Shipping</shipping>
        </rewrite>
    </shipping>
</models>

And in class Webkul_Mpperproductshipping_Model_Shipping will go the above method. Don't forget to make that class a child of original Mage_Shipping_Model_Shipping class.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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