Magento – How to make a product shipable only to specific cities

cart-ruleshipping

How to make a product available only in specific cities. Example organic products will be available only in certain cities, so during checkout if the user selects city other than specified then the checkout process should give error stating "Products available only in so and so city.". Checkout process should not be completed unless a proper city is selected. I have tried with Shopping cart price rules but no luck. Any ideas or suggestions please?

Thanks,

Best Answer

Create a product attribute called city that has a plaintext City Name - match on this in an observer for the predispatch event of the Mage_Checkout_OnepageController::saveShippingAction.

class MyCompany_MyModule_Model_Observer {

    public function saveShippingActionPredispatch($observer){

        $billing = $observer->getEvent()->getController()->getRequest()->getPost('billing');

        foreach(Mage::getSingleton('checkout/session')->getQuote()->getAllItems() as $item){
            if(!$billing['city']==$item->getProduct()->getCity()){
                Mage::throwException("You can't buy this product because you're shipping to the wrong place!");
            }
        }
    }

}

However, this is the easiest part - the reason Zones Manager is so complex is because this is a very complex problem. Thinking through this - there are many considerations:

  • How to handle misspelled city names? (probably would use Zip/Postcodes, obviously)
  • How to obtain /manage a list of postcodes?
  • How to query the customer for their ultimate shipping address before displaying products to them? Geolocation?
  • How to limit the catalog to only show regional items?
  • If you instead use category structure for products / cities, how do you limit them from purchasing a product that wasn't intended for a city at the end of the day?
  • Are you concerned about this complexity impacting your conversion rate?

(corrected the code by putting the missing curly bracket to close the foreach loop)

Related Topic