星期六送货选项UPS扩展Magento

时间:2014-12-23 21:16:13

标签: magento ups

我正在使用 Magento Enterprise

我正在尝试从前端结帐页面上的UPS API中提取 星期六投放选项 。我现在似乎无法随心所欲。我在管理员上启用了该选项,当我尝试调用getAvailableMethods()函数并打印结果时,我在阵列上获得了星期六传递选项,但我无法在前端显示该选项。我修改了UPS model以包含星期六交付的代码(经过大量研究后我发现它不再那样了)但它仍然没有做任何东西。有没有办法在我的前端结账页面上获得交付选项?

这是我正在使用的一些代码

UPS Model

我在originShipment数组

下包含了星期六递送选项
'originShipment'=>array(
                // United States Domestic Shipments
                'United States Domestic Shipments' => array(
                    '01' => Mage::helper('usa')->__('UPS Next Day Air'),
                    '02' => Mage::helper('usa')->__('UPS Second Day Air'),
                    '03' => Mage::helper('usa')->__('UPS Ground'),
                    '07' => Mage::helper('usa')->__('UPS Worldwide Express'),
                    '08' => Mage::helper('usa')->__('UPS Worldwide Expedited'),
                    '11' => Mage::helper('usa')->__('UPS Standard'),
                    '12' => Mage::helper('usa')->__('UPS Three-Day Select'),
                    '13' => Mage::helper('usa')->__('UPS Next Day Air Saver'),
                    '14' => Mage::helper('usa')->__('UPS Next Day Air Early A.M.'),
                    '54' => Mage::helper('usa')->__('UPS Worldwide Express Plus'),
                    '59' => Mage::helper('usa')->__('UPS Second Day Air A.M.'),
                    '65' => Mage::helper('usa')->__('UPS Saver'),
                    '33' => Mage::helper('usa')->__('UPS Saturday Delivery'),
                ),

并在我的方法数组

    'method'=>array(
        '1DM'    => Mage::helper('usa')->__('Next Day Air Early AM'),
        '1DML'   => Mage::helper('usa')->__('Next Day Air Early AM Letter'),
        '1DA'    => Mage::helper('usa')->__('Next Day Air'),
        '1DAL'   => Mage::helper('usa')->__('Next Day Air Letter'),
        '1DAPI'  => Mage::helper('usa')->__('Next Day Air Intra (Puerto Rico)'),
        '1DP'    => Mage::helper('usa')->__('Next Day Air Saver'),
        '1DPL'   => Mage::helper('usa')->__('Next Day Air Saver Letter'),
        '2DM'    => Mage::helper('usa')->__('2nd Day Air AM'),
        '2DML'   => Mage::helper('usa')->__('2nd Day Air AM Letter'),
        '2DA'    => Mage::helper('usa')->__('2nd Day Air'),
        '2DAL'   => Mage::helper('usa')->__('2nd Day Air Letter'),
        '3DS'    => Mage::helper('usa')->__('3 Day Select'),
        'GND'    => Mage::helper('usa')->__('Ground'),
        'GNDCOM' => Mage::helper('usa')->__('Ground Commercial'),
        'GNDRES' => Mage::helper('usa')->__('Ground Residential'),
        'STD'    => Mage::helper('usa')->__('Canada Standard'),
        'XPR'    => Mage::helper('usa')->__('Worldwide Express'),
        'WXS'    => Mage::helper('usa')->__('Worldwide Express Saver'),
        'XPRL'   => Mage::helper('usa')->__('Worldwide Express Letter'),
        'XDM'    => Mage::helper('usa')->__('Worldwide Express Plus'),
        'XDML'   => Mage::helper('usa')->__('Worldwide Express Plus Letter'),
        'XPD'    => Mage::helper('usa')->__('Worldwide Expedited'),
        'SAT'   => Mage::helper('usa')->__('Saturday Delivery'),
    ),
'containers_filter' => array(
                array(
                    'containers' => array('00'), // Customer Packaging
                    'filters'    => array(
                        'within_us' => array(
                            'method' => array(
                                '01', // Next Day Air
                                '13', // Next Day Air Saver
                                '12', // 3 Day Select
                                '59', // 2nd Day Air AM
                                '03', // Ground
                                '14', // Next Day Air Early AM
                                '02', // 2nd Day Air
                                '33', // Saturday Delivery
                            )
                        ),
                        'from_us' => array(
                            'method' => array(
                                '07', // Worldwide Express
                                '54', // Worldwide Express Plus
                                '08', // Worldwide Expedited
                                '65', // Worldwide Saver
                                '11', // Standard
                                '33', // Saturday Delivery
                            )
                        )
                    )
                ),

我已启用管理员的所有选项。从理论上讲,除非我使用错误的代码进行周六交付选项,否则这应该会在我的前端进行星期六交付。我不确定我在这里做错了什么。

由于

1 个答案:

答案 0 :(得分:0)

这是一个老问题,但仍然是一个相关的人,根据我的经验,他的解决方案并不容易获得。

上述代码的问题在于假设星期六交付是UPS的费率。但是,在UPS API文档中,他们声明它是一个运输选项。如果您将此运送选项添加到API请求,它将返回该请求中的所有费率,并应用星期六交付。

因此,在Magento中解决这个问题的方法是向UPS API发出两个请求 - 一次是非星期六费率,一次是星期六费率。

我已经整理了一个模块来添加这个功能,可以在这里找到。

https://github.com/PromInc/Magento-1.x-UPS-Saturday-Delivery

但是为了确保我们理解发生了什么,我将在这里解释代码(使用Core文件作为示例 - 当然不要覆盖你的核心文件!!!):

  

应用程序/代码/核心/法师/运输/型号/ Shipping.php

将运营商添加到运营商列表 - 复制UPS。

public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
    ...

    $limitCarrier = $request->getLimitCarrier();
    if (!$limitCarrier) {
        $carriers = Mage::getStoreConfig('carriers', $storeId);

        /*
        For UPS Saturday Shipping, add an additional carrier that will have the saturday delivery option added in Shipping/Carrier/Ups.php
        Saturday delivery is an option in the API request and applies to ALL shipping rates in that result.
        So we make 2 requests to UPS - once for non-saturday and once for Saturday.
        NOTE: system configuration used for getting Admin configured options
        */
        if( Mage::getStoreConfig('shipping/saturday_delivery_ups/enabled') && in_array( date('w'), explode( ",", Mage::getStoreConfig('shipping/saturday_delivery_ups/days_enabled') ) ) ) {
            $carriers['upssaturday'] = $carriers['ups'];
        }

        foreach ($carriers as $carrierCode => $carrierConfig) {
            $this->collectCarrierRates($carrierCode, $request);
        }
    } else {
        ...
    }

    return $this;
}

将新运营商(上周六)的运营商代码设置为 ups ,以确保我们加载正确的类并向UPS发出请求。

还设置一个变量来标记此运营商已启用星期六交付选项。

public function collectCarrierRates($carrierCode, $request)
{
    /* @var $carrier Mage_Shipping_Model_Carrier_Abstract */
    // $carrier = $this->getCarrierByCode($carrierCode, $request->getStoreId());
    $carrierCodeForLoad = $carrierCode;
    if( $carrierCode == 'upssaturday' ) {
        $carrierCodeForLoad = 'ups';
    }
    $carrier = $this->getCarrierByCode($carrierCodeForLoad, $request->getStoreId());

    if (!$carrier) {
        return $this;
    }

    // Tell the UPS request to set the Saturday delivery option
    if( $carrierCode == 'upssaturday' ) {
        $carrier->setSaturdayDelivery(true);
    }

    $carrier->setActiveFlag($this->_availabilityConfigField);
    ...
    return $this;
}
  

应用程序/代码/核心/法师/ USA /型号/运输/运营商/ Ups.php

解析XML响应时,修改方法代码并显示名称以反映它是星期六送货方式。

protected function _parseXmlResponse($xmlResponse)
{
    ...
    } else {
        if( $this->getSaturdayDelivery() ) {
            $saturdayShippingMethods = explode( ",", Mage::getStoreConfig('shipping/saturday_delivery_ups/allowed_methods') );
        }
        foreach ($priceArr as $method=>$price) {
            if( $this->getSaturdayDelivery() && !in_array( $method, $saturdayShippingMethods ) ) {
                continue;
            }

            $rate = Mage::getModel('shipping/rate_result_method');
            $rate->setCarrier('ups');
            $rate->setCarrierTitle($this->getConfigData('title'));
            // $rate->setMethod($method);
            $rate->setMethod( $method . ( $this->getSaturdayDelivery() ? '_sat' : '' ) );
            // $method_arr = $this->getShipmentByCode($method);  // string
            $method_arr = $this->getShipmentByCode($method) . ( $this->getSaturdayDelivery() ? ' Saturday' : '' );  // string
            $rate->setMethodTitle($method_arr);
            $rate->setCost($costArr[$method]);
            $rate->setPrice($price);

            $result->append($rate);
        }
    }
    return $result;
}

如果为此费率启用,则将星期六递送选项添加到XML请求以获取报价。

protected function _getXmlQuotes()
{
    ....
    if ($serviceCode !== null) {
        $xmlRequest .= "<Service>" .
            "<Code>{$serviceCode}</Code>" .
            "<Description>{$serviceDescription}</Description>" .
            "</Service>";
    }

if( $this->getSaturdayDelivery() ) {
  $xmlRequest .= <<< XMLRequest
<ShipmentServiceOptions>
    <SaturdayDelivery/>
</ShipmentServiceOptions>
XMLRequest;
}
  $xmlRequest .= <<< XMLRequest
  <Shipper>
XMLRequest;
    ...
}