2

I've just started writing Magento 1.9 plugins and I started with a basic example of writing a basic Shipping Method plugin that will return static rates.

Here is what the structure of the model file looks like :

class X_Y_Model_Carrier
extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface {

protected $_code = 'x_y';

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

    [...]
    $address = $request->getDestStreet();
    $city = $request->getDestCity();
    [...]

    $rate = Mage::getModel('shipping/rate_result_method');
    $rate->setCarrier($this->_code);
    $rate->setCarrierTitle('Carrier');
    $rate->setMethod('x_y_1');
    $rate->setMethodTitle('Method 1');
    $rate->setPrice(12.34);
    $rate->setCost(1.23);

            [...]
    return $result;
}

[...]
}

I noticed that i don't have the detailed address in the $request so getDestCity returns null for example.

I've done some research and according to this answer in order to have such details, I would need to override EstimateAddressInterface.

Could anyone guide me on how to achieve that? How exactly does one override an interface (the proper way) in Magento 1.9

EDIT 1 removed typo duplicate $request in getDestStreet call

0

You try to pick getDestStreet wrong way and should get an fatal error. Correct code as

class X_Y_Model_Carrier
extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface {

protected $_code = 'x_y';

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

    [...]
    $address = $request->getDestStreet();
    $city = $request->getDestCity();
    [...]

    $rate = Mage::getModel('shipping/rate_result_method');
    $rate->setCarrier($this->_code);
    $rate->setCarrierTitle('Carrier');
    $rate->setMethod('x_y_1');
    $rate->setMethodTitle('Method 1');
    $rate->setPrice(12.34);
    $rate->setCost(1.23);

            [...]
    return $result;
}

[...]
}

Check request class which is app/code/core/Mage/Shipping/Model/Rate/Request.php

Following fields you should get:

/**
 * Fields:
 * - orig:
 *   - country_id: UK
 *   - region_id: 1
 *   - postcode: 90034
 * - dest:
 *   - country_id: UK
 *   - region_id: 2
 *   - postcode: 01005
 * - package:
 *   - value: $100
 *   - weight: 1.5 lb
 *   - height: 10"
 *   - width: 10"
 *   - depth: 10"
 * - order:
 *   - total_qty: 10
 *   - subtotal: $100
 * - option
 *   - insurance: true
 *   - handling: $1
 * - table (shiptable)
 *   - condition_name: package_weight
 * - limit
 *   - carrier: ups
 *   - method: 3dp
 * - ups
 *   - pickup: CC
 *   - container: CP
 *   - address: RES
 */

Following getter methods are available:

/* @method int getStoreId()
 * @method Mage_Shipping_Model_Rate_Request setStoreId(int $value)
 * @method int getWebsiteId()
 * @method Mage_Shipping_Model_Rate_Request setWebsiteId(int $value)
 * @method string getBaseCurrency()
 * @method Mage_Shipping_Model_Rate_Request setBaseCurrency(string $value)
 *
 * @method Mage_Shipping_Model_Rate_Request setAllItems(array $items)
 * @method array getAllItems()
 *
 * @method Mage_Shipping_Model_Rate_Request setOrigCountryId(string $value)
 * @method string getOrigCountryId()
 * @method Mage_Shipping_Model_Rate_Request setOrigRegionId(int $value)
 * @method int getOrigRegionId()
 * @method Mage_Shipping_Model_Rate_Request setOrigPostcode(string $value)
 * @method string getOrigPostcode()
 * @method Mage_Shipping_Model_Rate_Request setOrigCity(string $value)
 * @method string getOrigCity()
 *
 * @method Mage_Shipping_Model_Rate_Request setDestCountryId(string $value)
 * @method string getDestCountryId()
 * @method Mage_Shipping_Model_Rate_Request setDestRegionId(int $value)
 * @method int getDestRegionId()
 * @method Mage_Shipping_Model_Rate_Request setDestRegionCode(string $value)
 * @method string getDestRegionCode()
 * @method Mage_Shipping_Model_Rate_Request setDestPostcode(string $value)
 * @method string getDestPostcode()
 * @method Mage_Shipping_Model_Rate_Request setDestCity(string $value)
 * @method string getDestCity()
 * @method Mage_Shipping_Model_Rate_Request setDestStreet(string $value)
 * @method string getDestStreet()
 *
 * @method Mage_Shipping_Model_Rate_Request setPackageValue(float $value)
 * @method float getPackageValue()
 * @method Mage_Shipping_Model_Rate_Request setPackageValueWithDiscount(float $value)
 * @method float getPackageValueWithDiscount()
 * @method Mage_Shipping_Model_Rate_Request setPackagePhysicalValue(float $value)
 * @method float getPackagePhysicalValue()
 * @method Mage_Shipping_Model_Rate_Request setPackageQty(float $value)
 * @method float getPackageQty()
 * @method Mage_Shipping_Model_Rate_Request setPackageWeight(float $value)
 * @method float getPackageWeight()
 * @method Mage_Shipping_Model_Rate_Request setPackageHeight(int $value)
 * @method int getPackageHeight()
 * @method Mage_Shipping_Model_Rate_Request setPackageWidth(int $value)
 * @method int getPackageWidth()
 * @method Mage_Shipping_Model_Rate_Request setPackageDepth(int $value)
 * @method int getPackageDepth()
 * @method Mage_Shipping_Model_Rate_Request setPackageCurrency(string $value)
 * @method string getPackageCurrency()
 *
 * @method Mage_Shipping_Model_Rate_Request setOrderTotalQty(float $value)
 * @method float getOrderTotalQty()
 * @method Mage_Shipping_Model_Rate_Request setOrderSubtotal(float $value)
 * @method float getOrderSubtotal()
 *
 * @method boolean getFreeShipping()
 * @method Mage_Shipping_Model_Rate_Request setFreeShipping(boolean $flag)
 * @method float getFreeMethodWeight()
 * @method Mage_Shipping_Model_Rate_Request setFreeMethodWeight(float $value)
 *
 * @method Mage_Shipping_Model_Rate_Request setOptionInsurance(boolean $value)
 * @method boolean getOptionInsurance()
 * @method Mage_Shipping_Model_Rate_Request setOptionHandling(float $flag)
 * @method float getOptionHandling()
 *
 * @method Mage_Shipping_Model_Rate_Request setConditionName(array $value)
 * @method Mage_Shipping_Model_Rate_Request setConditionName(string $value)
 * @method string getConditionName()
 * @method array getConditionName()
 *
 * @method Mage_Shipping_Model_Rate_Request setLimitCarrier(string $value)
 * @method string getLimitCarrier()
 * @method Mage_Shipping_Model_Rate_Request setLimitMethod(string $value)
 * @method string getLimitMethod()
 */
  • oh that second $request' was a copy paste error.. I am already aware of the methods you showed in that file but i am saying that both getDestCity` and getDestStreet are giving me a null / empty answer even though they are already provided during the checkout... – Dany Khalife Aug 21 '16 at 14:13
  • It should return correct value. It tested M1.9.2.4 – Sohel Rana Aug 21 '16 at 17:26

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.