magento getCarriers 分析

完整的设置订单追踪信息的时候我们可能会用到它。在后台中他在这里设置:
magento_get_carriers

有的时候我们想要设置自己定义的 carrier 比如 顺丰 申通 圆通 。。等等
我们能够先从 magento api 入手分析
我们调用 magento api —-> order_shipment.getCarriers
能够去查找 api 函数 在 appcodecoreMageSalesModelOrderShipmentApi.php

    public function getCarriers($orderIncrementId)
    {
        $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);

        /**
          * Check order existing
          */
        if (!$order->getId()) {
            $this->_fault('order_not_exists');
        }

        return $this->_getCarriers($order);
    }

追踪 _getCarriers 方法

 protected function _getCarriers($object)
    {
        $carriers = array();
        $carrierInstances = Mage::getSingleton('shipping/config')->getAllCarriers(
            $object->getStoreId()
        );

        $carriers['custom'] = Mage::helper('sales')->__('Custom Value');
        foreach ($carrierInstances as $code => $carrier) {
            if ($carrier->isTrackingAvailable()) {
                $carriers[$code] = $carrier->getConfigData('title');
            }
        }

        return $carriers;
    }

即 都会有个默认的 custom
之后的都是通过 Mage::getSingleton(‘shipping/config’)->getAllCarriers() 获取的
追踪方法 getAllCarriers() 事实上就是循环config中的 carriers 比如这里面就有magento 自带的freeshipping
和 flatrate 等方法。 可是 要想显示到返回的 $carriers 中须要 isTrackingAvailable 为true
我们事实上能够在上面代码中打印你每个 $carrier 的类名 找到调用的 isTrackingAvailable 方法,
经分析 发现默认的比如 flatrate 他的 model中方法 isTrackingAvailable 是继承父类为false 的。
那么假设此时你想在后台 carrier中有 flatrate 能够设置他的 isTrackingAvailable 方法返回true即可了
相同适用于你自己定义的运送方式。

扩展的说下:
另一点,我们在前台结账的时候到 选择运送方式的时候比如有 flatrate 那么此时他的input value 你能够看到是
flatrate_flatrate 我们继续追踪代码,发现他在代码中显示的 是 getCarrier().’_’.getMethod() 通常我们会把这两个值在
设置的时候设置为一样的(在flatrate的model中)所以显示的就是这个样子了

可是这里要说的就是对接 Odoo 的时候默认的magento连接件 是通过这个订单的shipping method 。即运送方式
你能够在数据表 sale_flat_order 找到 比如就是 flatrate_flatrate 。 然而此时你在 Odoo 中设置tracking number的时候
可能报错,是由于提示没有 flatrate_flatrate
这时假设magento 设置 flatrate 的isTrackingAvailable 为true 你把Odoo中这个code改为 flatrate 就能够了
假设没有。能够直接设置code 为 custom 也是能够的,由于你能够在magento api中找到 默认都有个为 custom的code

原文地址:https://www.cnblogs.com/liguangsunls/p/7080402.html