magentozipcodeups

Multiple Origin Zip Codes in Magento Checkout


I'm working on a Magento storefront for a client. They use dropshippers, so a single zip code doesn't do us much help. We do have it set for the most common zip code from which the client ships, so, in many cases, it's ok.

However, in some cases, there is a different origin zip code that needs to be used. In more rare cases, we will have multiple origin zip codes. When there is a zip that differs from the main one, we have stored this in an attribute called 'origin zip' (creative, huh?)

Where should I be looking to make the modifications? We're only using the UPS shipping method, and what I'm looking to do is, before calculating shipping, to grab whatever origin zips may be in the cart (I think we've got this part), but then, depending on the results, I may need to iterate through the shipping calculation and add the values together - i.e. in the case they order one product with an origin zip code, and another product without an origin zip code, it would have to calculate the first, then the second, and then add them together.

If someone could point us in the correct direction of which php files or classes we'll need to modify, I would greatly appreciate it.


Solution

  • First of all you need to add your custom attributed to list of attributes that will be used in shopping cart. Follow these answer on stackoverflow: How to add custom uploaded images to cart in magento 1.4.1.1?

    Then you need to create your custom shipping method, maybe extended from yours one. It should walk through items it receives from shipping request and check for different zip origin, then calculate rate for them separately.

    I hope for you it will not be a problem to create a module that will extend existing shipping method functionality.

    Cheers!

    UPDATE For adding your attribute to product that is loaded in the cart item use such configuration:

    <config>
         <global>
              <sales>
                   <quote>
                        <item>
                            <product_attributes>
                                 <origin_zip />
                            </product_attributes>
                        </item>
                   </quote>
              </sales>
         </global>
    </config>
    

    Then in shipping method model use something like this (used USPS as example):

    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
    {
        if (!$this->getConfigFlag('active')) {
            return false;
        }
    
        $defaultOriginZip = Mage::getStoreConfig('shipping/origin/postcode', $this->getStore());
    
        $requestDataByOriginZip = array();
        // Walking through quote items
        foreach ($request->getAllItems() as $quoteItem) {
            // If virtual or not shippable separately, it should be skipped
            if ($quoteItem->isVirtual() || $quoteItem->isDummy(true)) {
                continue;
            }
            // Retrieving origin zip code
            if ($quoteItem->getProduct()->getOriginZip()) {
                $zipCodeForCalculation = $quoteItem->getProduct()->getOriginZip();
            } else {
                $zipCodeForCalculation = $defaultOriginZip;
            }
    
            if (!isset($requestDataByOriginZip[$zipCodeForCalculation])) {
                // Default values initialization for this zip code
                $requestDataByOriginZip[$zipCodeForCalculation] = array(
                    'orig_postcode' => $zipCodeForCalculation,
                    'package_weight' => 0,
                    'package_value' => 0,
                    // etc...
                );
            }
    
            $requestDataByOriginZip[$zipCodeForCalculation]['package_weight'] += $quoteItem->getRowWeight();
            $requestDataByOriginZip[$zipCodeForCalculation]['package_value'] += $quoteItem->getBaseRowTotal();
            // Etc...
        }
    
        $results = array();
        foreach ($requestDataByOriginZip as $requestData) {
           $requestByZip = clone $request; // Cloning to prevent changing logic in other shipment methods.
           $requestByZip->addData($requestData);
           $this->setRequest($requestByZip);
           // Returns rate result for current request
           $results[] = $this->_getQuotes();
        }
    
        $yourMergedResult = Mage::getModel('shipping/rate_result');
    
        foreach ($results as $result) {
           // Logic for merging the rate prices....
        }
    
        return $yourMergedResult;
    }