{"action":"closed","issue":{"url":"https://api.github.com/repos/thiagolima-bm/magento2doc/issues/2","repository_url":"https://api.github.com/repos/thiagolima-bm/magento2doc","labels_url":"https://api.github.com/repos/thiagolima-bm/magento2doc/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/thiagolima-bm/magento2doc/issues/2/comments","events_url":"https://api.github.com/repos/thiagolima-bm/magento2doc/issues/2/events","html_url":"https://github.com/thiagolima-bm/magento2doc/issues/2","id":294375704,"number":2,"title":"Shipping Method - Adicionar novo Courier com requisição API","user":{"login":"kauansoft","id":27354497,"avatar_url":"https://avatars1.githubusercontent.com/u/27354497?v=4","gravatar_id":"","url":"https://api.github.com/users/kauansoft","html_url":"https://github.com/kauansoft","followers_url":"https://api.github.com/users/kauansoft/followers","following_url":"https://api.github.com/users/kauansoft/following{/other_user}","gists_url":"https://api.github.com/users/kauansoft/gists{/gist_id}","starred_url":"https://api.github.com/users/kauansoft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kauansoft/subscriptions","organizations_url":"https://api.github.com/users/kauansoft/orgs","repos_url":"https://api.github.com/users/kauansoft/repos","events_url":"https://api.github.com/users/kauansoft/events{/privacy}","received_events_url":"https://api.github.com/users/kauansoft/received_events","type":"User","site_admin":false},"labels":[{"id":820587441,"url":"https://api.github.com/repos/thiagolima-bm/magento2doc/labels/D%C3%BAvida","name":"Dúvida","color":"d876e3","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2018-02-05T12:27:25Z","updated_at":"2018-02-08T15:42:38Z","closed_at":"2018-02-08T15:42:38Z","author_association":"NONE","body":"Olá, gostaria de uma ajuda se possível, estou tentando criar um módulo para calcular o preço do frete, mas não tem nenhum tutorial na internet que ensine fazer requisições com api, olhei o módulo da ups e dhl, porém não consegui definir a lógica.\r\n\r\nModel.php\r\n\r\n```\r\n<?php\r\nnamespace Kauan\\Shipping\\Model\\Carrier;\r\n\r\nuse Magento\\Quote\\Model\\Quote\\Address\\RateRequest;\r\nuse Magento\\Shipping\\Model\\Rate\\Result;\r\nuse Magento\\Quote\\Model\\Quote\\Address\\RateResult\\Error;\r\nuse Magento\\Shipping\\Model\\Carrier\\AbstractCarrierOnline;\r\nuse Magento\\Shipping\\Model\\Carrier\\CarrierInterface;\r\nuse Magento\\Shipping\\Model\\Simplexml\\Element;\r\nuse Magento\\Ups\\Helper\\Config;\r\nuse Magento\\Framework\\Xml\\Security;\r\n\r\nclass Datafrete extends \\Magento\\Shipping\\Model\\Carrier\\AbstractCarrier implements\r\n    \\Magento\\Shipping\\Model\\Carrier\\CarrierInterface\r\n{\r\n    /**\r\n     * @var string\r\n     */\r\n    protected $_code = 'shipping';\r\n\r\n    /**\r\n     * @param \\Magento\\Framework\\App\\Config\\ScopeConfigInterface $scopeConfig\r\n     * @param \\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\ErrorFactory $rateErrorFactory\r\n     * @param \\Psr\\Log\\LoggerInterface $logger\r\n     * @param \\Magento\\Shipping\\Model\\Rate\\ResultFactory $rateResultFactory\r\n     * @param \\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\MethodFactory $rateMethodFactory\r\n     * @param array $data\r\n     */\r\n    public function __construct(\r\n        \\Magento\\Framework\\App\\Config\\ScopeConfigInterface $scopeConfig,\r\n        \\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\ErrorFactory $rateErrorFactory,\r\n        \\Psr\\Log\\LoggerInterface $logger,\r\n        Security $xmlSecurity,\r\n        \\Magento\\Shipping\\Model\\Simplexml\\ElementFactory $xmlElFactory,\r\n        \\Magento\\Shipping\\Model\\Rate\\ResultFactory $rateFactory,\r\n        \\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\MethodFactory $rateMethodFactory,\r\n        \\Magento\\Shipping\\Model\\Tracking\\ResultFactory $trackFactory,\r\n        \\Magento\\Shipping\\Model\\Tracking\\Result\\ErrorFactory $trackErrorFactory,\r\n        \\Magento\\Shipping\\Model\\Tracking\\Result\\StatusFactory $trackStatusFactory,\r\n        \\Magento\\Directory\\Model\\RegionFactory $regionFactory,\r\n        \\Magento\\Directory\\Model\\CountryFactory $countryFactory,\r\n        \\Magento\\Directory\\Model\\CurrencyFactory $currencyFactory,\r\n        \\Magento\\Directory\\Helper\\Data $directoryData,\r\n        \\Magento\\CatalogInventory\\Api\\StockRegistryInterface $stockRegistry,\r\n        \\Magento\\Framework\\Locale\\FormatInterface $localeFormat,\r\n        Config $configHelper,\r\n        array $data = []\r\n    ) {\r\n        $this->_rateResultFactory = $rateResultFactory;\r\n        $this->_rateMethodFactory = $rateMethodFactory;\r\n        parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);\r\n    }\r\n\r\n    /**\r\n     * @return array\r\n     */\r\n    public function getAllowedMethods()\r\n    {\r\n        return ['datafrete' => $this->getConfigData('name')];\r\n    }\r\n\r\n    /**\r\n     * @param RateRequest $request\r\n     * @return bool|Result\r\n     */\r\n\r\n\r\n\r\n    public function collectRates(RateRequest $request)\r\n    {\r\n        if (!$this->getConfigFlag('active')) {\r\n            return false;\r\n        }\r\n        //API CONNECTION\r\n        $options = array('location' => $this->getConfigData('api'),\r\n                                    'uri'      => $this->getConfigData('api').'?wsdl');\r\n\r\n         $client = new SoapClient($this->url_integracao.\"?wsdl\",$options);\r\n\r\n         //ITS OK TO GET INFOS PRODUCTS LIKE THIS? \r\n        $itemsShipment = $request->getPackageItems();\r\n        foreach ($itemsShipment as $itemShipment) {\r\n            $item = new \\Magento\\Framework\\DataObject();\r\n            $item->setData($itemShipment);\r\n            $nome = $item->getName();\r\n            $preco =  $item->getPrice();\r\n        }\r\n\r\n\r\n //FUNCTION TO GET INFO/VALUES FROM API - RETURN A JSON\r\n             $consulta_frete = $client->getValorFrete(\r\n//NEED KNOW IF THIS LIST CONTAINS RIGHT INFOS TO POST ON API                                                                    $this->getConfigData('usuario'),\r\n                                                                    $this->getConfigData('senha'),\r\n                                                                    $this->getConfigData('codigo'),\r\n                                                                $request->getOrigPostcode(),\r\n                                                                $request->getDestPostcode(),\r\n                                                                $json_produto,\r\n                                                                $json_inf_comp);\r\n\r\n        $consulta_frete = json_decode($consulta_frete);\r\n\r\n\r\n//HOW I USE FOREACH TO DECLARE VALUES AND DESCRIPTIONS ON METHOD->SETPRICE\r\n\r\n        /** @var \\Magento\\Shipping\\Model\\Rate\\Result $result */\r\n        $result = $this->_rateResultFactory->create();\r\n\r\n        /** @var \\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\Method $method */\r\n        $method = $this->_rateMethodFactory->create();\r\n\r\n        $method->setCarrier('datafrete');\r\n        $method->setCarrierTitle($this->getConfigData('title'));\r\n\r\n        $method->setMethod('datafrete');\r\n        $method->setMethodTitle($this->getConfigData('name'));\r\n\r\n        /*you can fetch shipping price from different sources over some APIs, we used price from config.xml - xml node price*/\r\n        //$amount = $this->getConfigData('price');\r\n\r\n        $method->setPrice($amount);\r\n        $method->setCost($amount);\r\n\r\n        $result->append($method);\r\n\r\n        return $result;\r\n    }\r\n}\r\n```\r\n\r\nNão sei como pegar as informações do carrinho, enviar para api e retornar os valores para o carrinho.\r\n\r\nAbraço."}}
{"actor":{"display_login":"kauansoft"}}