0

Like in the title, i want to make custom shipping method which will appears only for the logged users.

I had reward points module, and i wanna make a "FreeShipping for Points" you can use the reward points only if you are logged in user so the shipping for points option have to be available only for logged users.

I already made custom shipping method based on http://code.tutsplus.com/tutorials/create-a-custom-shipping-method-in-magento--cms-23747.

<?php
// app/code/local/Envato/Customshippingmethod/Model
class Envato_Customshippingmethod_Model_Demo
extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface
{
  protected $_code = 'envato_customshippingmethod';

public function collectRates($carrierCode, $request)
    {
        if (!$this->_isAvailable($carrierCode, $request)) {
           return $this;
        }
        return parent::collectCarrierRates($carrierCode, $request);
    }

  public function getAllowedMethods()
  {
    return array(
      'envato_customshippingmethod' => $this->getConfigData('name'),
    );
  }

  protected function _isAvailable($carrierCode, $request)
    {
        $regionCode = $request->getDestRegionCode();

        switch ($carrierCode) {
            case 'envato_customshippingmethod':
                if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
                    return false;
                }
                break;
        }
        return true;
    }


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

    $rate->setCarrier($this->_code);
    $rate->setCarrierTitle($this->getConfigData('title'));
    $rate->setMethod($this->_code);
    $rate->setMethodTitle($this->getConfigData('name'));
    $rate->setPrice($this->getConfigData('price'));
    $rate->setCost(0);

    return $rate;
  }
}

?>  

1 Answer 1

0

You need to rewrite the following class Mage_Shipping_Model_Shipping
Step 1:Include following code in your config.xml

 <global> 
    <models>     
            <shipping>
                <rewrite>
                    <shipping>Company_Module_Model_Shipping_Shipping</shipping>
                </rewrite>
            </shipping>
    </models>
  </global>

Step 2 : your model class

class Company_Module_Model_Shipping_Shipping extends Mage_Shipping_Model_Shipping
{
    public function collectCarrierRates($carrierCode, $request)
    {
        if (!$this->_isAvailable($carrierCode, $request)) {
           return $this;
        }
        return parent::collectCarrierRates($carrierCode, $request);
    }

    /**
     * @param string $carrierCode
     * @param Varien_Object $request
     * @return bool
     */
    protected function _isAvailable($carrierCode, $request)
    {
        $regionCode = $request->getDestRegionCode();

        switch ($carrierCode) {
            case 'flatrate':
                if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
                    return false;
                }
                break;
            case 'freeshipping':
                if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
                    return false;
                }
                break;
        }
        return true;
    }
}

As you can see in above example, I have disables free shiping and fedex shipping method if user is not log-in.In the same way you customize you this code.

4
  • Doesn't work. Maybe i made some mistakes, i edited my post and add my model class.
    – kowalptk
    Feb 17, 2016 at 11:10
  • Create a new model app\code\local\Sipping\Disable\Model\Shipping\Shipping.php in this file use this code.
    – Arunendra
    Feb 17, 2016 at 11:18
  • I don't understand why i need to create new model, i already have model for custom shipping, can i disable unlogged users from that model? Really i need to write another model?
    – kowalptk
    Feb 17, 2016 at 11:22
  • As i can see your model class every things is ok make sure your shipping code is right,you can create new model for test or leave it
    – Arunendra
    Feb 17, 2016 at 11:31

Your Answer

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

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