0

I created an extension to extend Magento with another shipping method. Actually I used this tutorial to do this. Only difference is that the shipping method only shows up if an item in cart is eligible for this method. So far it works. But now I want to pre select this shipping method. How can I do that? Unfortunately I couldn't find any information about it. The only option I'm seeing right now is to add the logic in the template. But I was wondering if it can't be set in the extenion (app/code/core/Mage/Shipping/Model/Carrier/Flatrate.php) itself.

<?php

class Creativ_Shipping_Model_Carrier_Oneitem_Extended_Flatrate1
extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface
{
protected $_code = 'extendedflatrate1';
protected $_isFixed = true;

public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
    if (!$this->getConfigFlag('active')) {
        return false;
    }

    $result = Mage::getModel('shipping/rate_result');

    $items = $request->getAllItems();

    $eligible = false;

    foreach ($items as $item) {
        if ($item->getProduct()->getInsuranceEligible(false)) {
            $eligible = true;
            break;
        }
    }
    if ($eligible) {

        $method = Mage::getModel('shipping/rate_result_method');

        $method->setCarrier('extendedflatrate1');
        $method->setCarrierTitle($this->getConfigData('title'));
        $method->setMethod('oneitemflatrate1');
        $method->setMethodTitle($this->getConfigData('name'));

        $shippingPrice = $this->getConfigData('price');
        $method->setPrice($shippingPrice);
        $method->setCost($shippingPrice);

        $result->append($method);
    }

    return $result;
}

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

}

Thanks for any help!

1

If I clearly understand you, we can reduce your question to following:

How to preselect any shipping method?

If yes - look here: change default shipping method on button click (ajax call) on checkout page

  • 1
    This is almost correct. I was looking for a solution without editing the template so that all my code lives in my extension. It looks like its not possible with PHP (in app/code) but with an extra Js file. Thanks for your help! – Stefan Apr 15 '15 at 13:17
  • You are welcome – zhartaunik Apr 15 '15 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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