将Magento 1.9的运输扩展迁移到Magento 2.2

时间:2017-12-03 04:45:24

标签: magento magento-1.9 magento2

我正在尝试将自定义magento 1.9扩展迁移到magento 2.2。我做了很多搜索,无法找到有关我尝试迁移的用例的信息。我最初跟随this tutorial进行了1.9扩展。我知道有一个工具可以帮助端口扩展,但我试图手动执行此操作,因为我无法让该工具为我的功能。

每次更新购物车时,自定义送货延期都会运行,以计算自定义送货费率。目标是在magento 2.2中重新创建此扩展,这样每次打开或更新购物车时,它都会运行并计算运费,然后通过结帐流程传播。

以下是magento 1.9扩展的概述。关于如何将其翻译成magento 2.2的任何建议?

/app/etc/modules/Extensions_Shipper.xml

<?xml version="1.0"?>
<config>
<modules>
    <Extensions_Shipper>
        <active>true</active>
        <codePool>local</codePool>
        <depends>
            <Mage_Shipping />
        </depends>
    </Extensions_Shipper>
</modules>

/app/code/local/Extensions/Shipper/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
    <Extensions_Shipper>
        <module>0.0.1</module>
    </Extensions_Shipper>
</modules>
<global>
    <models>
        <extensions_shipper>
            <class>Extensions_Shipper_Model</class>
        </extensions_shipper>
    </models>
</global>
<default>
    <carriers>
        <extensions_shipper>
            <active>1</active>
            <model>extensions_shipper/carrier</model>
            <title>Shipping Options</title>
            <sort_order>10</sort_order>
            <sallowspecific>0</sallowspecific>
        </extensions_shipper>
    </carriers>
</default>

/app/code/local/Extensions/Shipper/Model/Carrier.php

<?php
class Extensions_Shipper_Model_Carrier extends Mage_Shipping_Model_Carrier_Abstract implements Mage_Shipping_Model_Carrier_Interface
{
    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
    {
        //Are there magento 2.2 equivalence for the following?
        $addressInfo = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData();
        $result = Mage::getModel('shipping/rate_result');
        $items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
        $currentItem = Mage::getModel('catalog/product')->load($items[$itemsArray[$i]]->getProduct()->getId());
        Mage::getSingleton('core/session')->addNotice('some text');
        //$result = some calculations for shipping rate
        return $result;
    }

    public function getAllowedMethods()
    {
        return array();
    }
}

1 个答案:

答案 0 :(得分:0)

您可以在checkout_cart_add_product_complete事件上创建一个事件观察者来执行逻辑以更新运费。

namespace MyCompany\MyModule\Observer;

use Magento\Framework\Event\ObserverInterface;

class MyObserver implements ObserverInterface
{
  public function __construct()
  {
    //Observer initialization code...
    //You can use dependency injection to get any class this observer may need.
  }

  public function execute(\Magento\Framework\Event\Observer $observer)
  {
    //Observer execution code...
  }
}

订阅events.xml中的活动:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="my_module_event_before">
        <observer name="myObserverName" instance="MyCompany\MyModule\Observer\MyObserver" />
    </event>
    <event name="my_module_event_after">
        <observer name="myObserverName" instance="MyCompany\MyModule\Observer\AnotherObserver" />
    </event>
</config>

请参阅有关事件和观察员的Magento文档:http://devdocs.magento.com/guides/v2.0/extension-dev-guide/events-and-observers.html

有关所有Magento 2.2事件的列表,请参阅:https://cyrillschumacher.com/magento-2.2-list-of-all-dispatched-events/