0

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

2 Answers 2

1
    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;
  }
0

I had a look at your code and I noticed a few issues.

1. Custom Free Shipping Method not appended

You have the append for $result->append($this->_getFreeShippingRate()); inside the conditional.

You need to remove this outside of the if statement so a user can select your shipping method.

You also need to update the "setMethodTitle" inside "_getFreeShippingRate" to $rate->setMethodTitle("Free Shipping"); as the title when I setup your method was empty.

2 Changing of Address

Assuming that you want to change the shipping address when a user selects your "Free Shipping" method you to replace "if ($request->getFreeShipping()) {" with the following:

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

    if ($method === 'cue_customshippingmethod_free_shipping') {

This will update the address to the LA address. You might also want to update the user's country aswell to the US.

6
  • correct me if i am wrong
    – Schnecke
    Commented Mar 17, 2016 at 18:35
  • Can you explain what you mean? Do you want to change the address when a user selects the custom "Free Shipping" method? Commented Mar 17, 2016 at 18:37
  • Yes I want to change the address when user selects free shipping (free means ship to store for pick up) n change back when user selects standard (ship to users address)
    – Schnecke
    Commented Mar 18, 2016 at 6:11
  • No problem. I will have an update for you in the next 24 hours. Commented Mar 18, 2016 at 11:33
  • Hi Colin, just wanted to end out if there s anything yet
    – Schnecke
    Commented Mar 19, 2016 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.