Magento – Shipping method that changes shipping address magento

free-shippingmagento-1.9PHPshipping-addressshipping-methods

I am trying to make a shipping method for free shipping that changes the shipping address when selected. this is what i have done so far but doesn't work where am i going wrong.

I have written this custom shipping method, its has both free and default shipping so which ever the user picks will excuse but the free shipping doesn't change the address what do i do?

<?php

class Cue_Customshippingmethod_Model_Demo
extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface
{



 protected $_code = 'cue_customshippingmethod';

  public function collectRates(Mage_Shipping_Model_Rate_Request $request)
  {
        $result = Mage::getModel('shipping/rate_result');
        /* @var $result Mage_Shipping_Model_Rate_Result */

        $result->append($this->_getDefaultRate());


        if ($request->getFreeShipping()) {
            /**
             *  If the request has the free shipping flag,
             *  append a free shipping rate to the result.
             */

       $quote = Mage::getSingleton('checkout/session')->getQuote();
        $shippingAddress = $quote->getShippingAddress();
       $shippingAddress->setStreet(array('123 Main Street'))
               ->setCity('Los Angeles')
               ->setRegionId(12)
               ->setPostcode('90034')
               ->save();

            $result->append($this->_getFreeShippingRate());
        }

    return $result;
  }

  public function getAllowedMethods()
  {
    return array(
        'free_shipping' => 'Free Shipping',
        'cue_customshippingmethod' => $this->getConfigData('name'),
    );
  }

  protected function _getFreeShippingRate()
  {
    $rate = Mage::getModel('shipping/rate_result_method');

    $rate->setCarrier($this->_code);
    $rate->setCarrierTitle($this->getConfigData('title'));
    $rate->setMethod('free_shipping');
    //$rate->setMethodTitle('Free Shipping (3 - 5 days)');
    $rate->setMethodTitle($this->getConfigData('name'));
    $rate->setPrice(0);
    $rate->setCost(0);

    return $rate;
  }
   protected function _getDefaultRate() {
        $rate = Mage::getModel('shipping/rate_result_method');
        /* @var $rate Mage_Shipping_Model_Rate_Result_Method */

        $rate->setCarrier($this->_code);
        /**
         * getConfigData(config_key) returns the configuration value for the
         * carriers/[carrier_code]/[config_key]
         */
        $rate->setCarrierTitle($this->getConfigData('title'));

        $rate->setMethod('standand');
        $rate->setMethodTitle('Standard');

        $rate->setPrice($this->getConfigData('price'));
        $rate->setCost(0);

        return $rate;
    }
}

Best Answer

    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
  {
        $result = Mage::getModel('shipping/rate_result');
        /* @var $result Mage_Shipping_Model_Rate_Result */
        $result->append($this->_getDefaultRate());
        $result->append($this->_getFreeShippingRate());

    $quote = Mage::getSingleton('checkout/session')->getQuote();
        $method = $quote->getShippingAddress()->getShippingMethod();

         $shippingAddress = $quote->getShippingAddress();
        $ad1 = $shippingAddress->getStreet();
        $ad2 = $shippingAddress->setCity();
        $ad3  = $shippingAddress->setRegionId(12);
        $ad4  = $shippingAddress->setPostcode('90034');



        if ($method === 'cue_customshippingmethod_free_shipping') {
            /**
             *  If the request has the free shipping flag,
             *  append a free shipping rate to the result.
             */
        $shippingAddress = $quote->getShippingAddress();
       $shippingAddress->setStreet(array('123 Main Street'))
               ->setCity('Los Angeles')
               ->setRegionId(12)
               ->setPostcode('90034')
               ->save();

        }/*else{

         $shippingAddress = $quote->getShippingAddress();
        $shippingAddress->setStreet(array($ad1))
               ->setCity($ad2)
               ->setRegionId($ad3)
               ->setPostcode($ad4)
               ->save();

        }*/

    return $result;
  }
Related Topic