Magento 2:如何运送称为On Checkout的rest api函数?


9

当您在“结帐页面”上单击“在此处寄送”时,它将调用

magento / rest / default / V1 / carts / mine / estimate-shipping-methods-by-address-id

然后转到下面的JS文件

magento \ vendor \ magento \ module-checkout \ view \ frontend \ web \ js \ model \ shipping-rate-processor \ customer-address.js

magento \ vendor \ magento \ module-checkout \ view \ frontend \ web \ js \ model \ resource-url-manager.js

getUrlForEstimationShippingMethodsByAddressId: function(quote) {
    var params = (this.getCheckoutMethod() == 'guest') ? {quoteId: quote.getQuoteId()} : {};
    var urls = {
        'default': '/carts/mine/estimate-shipping-methods-by-address-id'
    };
    return this.getUrl(urls, params);
}

magento \ vendor \ magento \ module-quote \ Model \ ShippingMethodManagement.php

 public function estimateByAddressId($cartId, $addressId)
    {
      echo 1;exit;
    }

上面的函数如何estimateByAddressId调用?

Answers:


6

如您所指出的那样,当您单击“在此处发送”时,HTTP POST请求将分派到"/V1/carts/mine/estimate-shipping-methods-by-address-id"REST API(来自模块引用)。如果您看一下,module-quote/etc/webapi.xml您会找到以下网址:

<route url="/V1/carts/mine/estimate-shipping-methods-by-address-id" method="POST">
  <service class="Magento\Quote\Api\ShippingMethodManagementInterface" method="estimateByAddressId"/>
  <resources>
    <resource ref="self" />
  </resources>
  <data>
    <parameter name="cartId" force="true">%cart_id%</parameter>
  </data>
</route>

您会注意到在<route>元素下有<service>一个带有class="Magento\Quote\Api\GuestShipmentEstimationInterface"和的元素method="estimateByExtendedAddress"。现在显然,该estimateByAddressId方法无法从接口实例化。

这里出现了magento 2依赖注入。查看module-quote/etc/di.xml将interface(Magento\Quote\Api\ShippingMethodManagementInterface)依赖项映射到首选实现类(Magento\Quote\Model\ShippingMethodManagement)的文件。

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Quote\Api\ShippingMethodManagementInterface" type="Magento\Quote\Model\ShippingMethodManagement" />
    ...................
</config>

这就是estimateByAddressId方法的调用方式。

有用的链接:

Magento 2 Web API:
http : //devdocs.magento.com/guides/v2.0/get-started/bk-get-started-api.html
http://devdocs.magento.com/guides/v2.0/ extension-dev-guide / service-contracts / service-to-web-service.html

Magento 2依赖注入:
http : //devdocs.magento.com/guides/v2.0/extension-dev-guide/depend-inj.html
http://magento-quickies.alanstorm.com/post/68129858943/magento- 2个注入接口

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.