amedeos Posted April 16, 2012 Share Posted April 16, 2012 Hi everyone, I'm building up a custom shipping module based on my particular needs. As a base, I'm using the one from the user MunchyMonster on magento's forum several years ago (=http://www.magentocommerce.com/boards/viewthread/7247/P0/). Basically, MunchyMonster's module was set up in such a way to add 3 different flatrates based on postcodes without using any matrix rate table. It was simple but it served the purpose. Now, my needs are slightly different. I'm trying to use the 3 Zip Codes Tiers from MunchyMonster's module as 3 different Free Delivery options. I'm trying to set up free delivery to specified postcodes on a certain day, if the order is placed within a specific time range of the week. I offer free delivery on Tuesdays and Thursdays, if orders are placed within a time range in the week. For instance, if the customer misses the cutoff time of Tuesday, Tuesday delivery is automatically disable. So far I succesfully set up the first tier with php, in order to follow this behaviour (time range, activation etc). So the module works and shows up in the front and back-end. However, I'd need the module to display Thursday Free Delivery as an option, too. Ideally that could be done by using the 2nd tier of the module as a shipping options. Unfortunately MunchyMonster's approach was such that just one of the Tiers could be used at once, and at the end a single shipping option would be fed to magento, as opposed to an array of shipping options. If look at the code below (I'm using the original code because mine would be more complicated to check, due to all the conditional statements) you'll see that 3 tiers from the backend (see backend) cannot be used (and thus displayed in the front end at the same time). Does anyone know how could I have this thing sorted (or at least point me in the right direction?). Although I have some php knowledge, I'm by no means confident in working with magento classes. The fully operational module (without my conditional statments, for the sake of simplicity, can be found here: Custom Module (3 files . Compressed RAR) Screenshot of backend Custom Module - Backend Separated non compressed files: config.xml system.xml php module Thanks in advance for any hints! <?php class MomsMunchies_Shipping_Model_Carrier_Localdelivery extends Mage_Shipping_Model_Carrier_Abstract implements Mage_Shipping_Model_Carrier_Interface { protected $_code = 'localdelivery'; /** * Collects the shipping rates for local delivery from our setting in the admin. * @param Mage_Shipping_Model_Rate_Request $request * @return Mage_Shipping_Model_Rate_Result */ public function collectRates(Mage_Shipping_Model_Rate_Request $request) { // Check if this method is active if (!$this->getConfigFlag('active')) { return false; } //The the destination Zip Code $toZipCode = $request->getDestPostcode(); //Retrieve each of the valid strings of zip codes $validZipString1 = $this->getConfigData('tier1zips'); $validZipString2 = $this->getConfigData('tier2zips'); $validZipString3 = $this->getConfigData('tier3zips'); //Turn them into arrays $validZips1 = explode(",", $validZipString1); $validZips2 = explode(",", $validZipString2); $validZips3 = explode(",", $validZipString3); //Set up our found flags to see if we have a match $foundValid1 = false; $foundValid2 = false; $foundValid3 = false; //Search tier 1 for a match foreach ($validZips1 as $validZip1) { if($validZip1 == $toZipCode) { $foundValid1 = true; break; } } if(!$foundValid1) { //if it wasn't in tier 1 search tier 2 foreach ($validZips2 as $validZip2) { if($validZip2 == $toZipCode) { $foundValid2 = true; break; } } if(!$foundValid2) { //if it wasn't in tier 2 search tier 3 foreach ($validZips3 as $validZip3) { if($validZip3 == $toZipCode) { $foundValid3 = true; break; } } if(!$foundValid3) { //if it wasn't in tier 3 then we can't provide Local Delivery return false; } else { //Found it so charge tier 3 delivery rate $shippingPrice = $this->getConfigData('tier3rate'); } } else { //Found it so charge tier 2 delivery rate $shippingPrice = $this->getConfigData('tier2rate'); } } else { //Found it so charge tier 1 delivery rate $shippingPrice = $this->getConfigData('tier1rate'); } //if we get to here we should have a valid $shippingPrice $result = Mage::getModel('shipping/rate_result'); $method = Mage::getModel('shipping/rate_result_method'); //add the handling fee if needed $shippingPrice += $this->getConfigData('handling_fee'); $method->setCarrier('localdelivery'); $method->setCarrierTitle($this->getConfigData('title')); $shipping_method = "Delivery Fee(" . $toZipCode . ")"; $method->setMethod($shipping_method); $method->setMethodTitle("$shipping_method"); $method->setPrice($shippingPrice); $method->setCost($shippingPrice); $result->append($method); return $result; } /* * Get allowed shipping methods * @return array */ public function getAllowedMethods() { //we only have one method so just return the name from the admin panel return array('localdelivery' => $this->getConfigData('name')); } } if(!$foundValid3) { //if it wasn't in tier 3 then we can't provide Local Delivery return false; } else { //Found it so charge tier 3 delivery rate $shippingPrice = $this->getConfigData('tier3rate'); } } else { //Found it so charge tier 2 delivery rate $shippingPrice = $this->getConfigData('tier2rate'); } } else { //Found it so charge tier 1 delivery rate $shippingPrice = $this->getConfigData('tier1rate'); } //if we get to here we should have a valid $shippingPrice $result = Mage::getModel('shipping/rate_result'); $method = Mage::getModel('shipping/rate_result_method'); //add the handling fee if needed $shippingPrice += $this->getConfigData('handling_fee'); $method->setCarrier('localdelivery'); $method->setCarrierTitle($this->getConfigData('title')); $shipping_method = "Delivery Fee(" . $toZipCode . ")"; $method->setMethod($shipping_method); $method->setMethodTitle("$shipping_method"); $method->setPrice($shippingPrice); $method->setCost($shippingPrice); $result->append($method); return $result; } /* * Get allowed shipping methods * @return array */ public function getAllowedMethods() { //we only have one method so just return the name from the admin panel return array('localdelivery' => $this->getConfigData('name')); } } Quote Link to comment https://forums.phpfreaks.com/topic/261012-magento-custom-shipping-module-how-to-create-multiple-delivery-options/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.