将Magento 1.9的运输扩展迁移到Magento 2.2
我正尝试将一个自定义magento 1.9扩展迁移到magento 2.2。我做了很多搜索,找不到我正在尝试迁移的用例的相关信息。我最初遵循this tutorial的1.9扩展。我知道有一种工具可以帮助端口扩展,但我正在尝试手动执行此操作,因为我无法让该工具为我工作。将Magento 1.9的运输扩展迁移到Magento 2.2
每次更新购物车以计算自定义运费时,都会运行自定义送货延期。我们的目标是在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();
}
}
回答:
您可以在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/
以上是 将Magento 1.9的运输扩展迁移到Magento 2.2 的全部内容, 来源链接: utcz.com/qa/259514.html