Jump to content

Magento Custom Shipping Module - How to create multiple delivery options?


amedeos

Recommended Posts

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'));
    }
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.