1

How can I get additional data in custom shipping method?

enter image description here

public function collectRates(RateRequest $request)
    {
        if (!$this->getConfigFlag('active')) {
            return false;
        }

        $this->configPhpSigep();
        $metodos = $this->calculaFreteCorreios($request);

        /** @var \Magento\Shipping\Model\Rate\Result $result */
        $result = $this->_rateResultFactory->create();
        $free_sheeping = $request->getFreeShipping();

        foreach ($metodos as $metodo) {

            /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
            $method_colection = $this->_rateMethodFactory->create();

            $method_colection->setCarrier('correiosship');
            $method_colection->setCarrierTitle($metodo['nome']);
            $method_colection->setData('prazo', $metodo['prazo']);

            $method_colection->setMethod('correiosship_' . $metodo['codigo']);
            $method_colection->setMethodTitle($metodo['nome']);
//            $method_colection->setMethodTitle($metodo['nome'] . " Em média " . $metodo['prazo'] . " dia(s)");

            $method_colection->setPrice($metodo['valor_cliente']); // valor pago pelo cliente
            $method_colection->setCost($metodo['valor_lojista']); // valor pago pelo lojista
            $result->append($method_colection);
        }

        return $result;
    }

I set additional data with:

$method_colection->setData('prazo', $metodo['prazo']);

Now I need to render in template custom-method-item-template.html

<!-- Initialize collapsible binding -->
<tbody collapsible="as: '$collapsible_' + method.method_code">
<tr class="row">
    <td class="col col-method">
        <input type="radio"
               class="radio"
               click="element.selectShippingMethod"
               ifnot="method.error_message"
               ko-checked="element.isSelected"
               ko-value="method.carrier_code + '_' + method.method_code"
               attr="'aria-labelledby': 'label_method_' + method.method_code + '_' + method.carrier_code + ' ' + 'label_carrier_' + method.method_code + '_' + method.carrier_code,
                        'checked': element.rates().length == 1 || element.isSelected" />
    </td>

    <td class="col col-method"
        attr="'id': 'label_method_' + method.method_code + '_' + method.carrier_code"
        text="method.method_title" />
    <td class="col col-price">
        <each args="element.getRegion('price')" render="" />
    </td>
    <!-- Need to render additional data here -->
    <td data-bind="text: method.prazo"></td>
    <!--<td class="col col-carrier"-->
        <!--attr="'id': 'label_carrier_' + method.method_code + '_' + method.carrier_code"-->
        <!--text="method.carrier_title" />-->
</tr>
<!-- Row for shipping method description -->
<tr class="row" visible="$context['$collapsible_' + method.method_code].opened">
    <td class="col" colspan="5" i18n="'Some description.'"/>
</tr>
<tr class="row row-error"
    if="method.error_message">
    <td class="col col-error" colspan="5">
        <div role="alert" class="message error">
            <div text="method.error_message"></div>
        </div>
        <span class="no-display">
                <input type="radio"
                       attr="'value' : method.method_code, 'id': 's_method_' + method.method_code" />
            </span>
    </td>
</tr>
</tbody>

How can I do this?

Thank's

1 Answer 1

0

Your code is correct on:

public function collectRates(RateRequest $request)
{
   ...
   $method_colection->setData('prazo', $metodo['prazo']);
   ...
}

But in magento at the checkout or cart page, the rates are obtained by querying the api:

rest/default/V1/carts/mine/estimate-shipping-methods*

To load your additional data in the api you must override Magento\Quote\Model\Cart\ShippingMethodConverter in your di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Quote\Model\Cart\ShippingMethodConverter" type="{VENDOR}\{NAMESPACE}\Model\Cart\ShippingMethodConverter" />
</config>

In your file: {VENDOR}\{NAMESPACE}\Model\Cart\ShippingMethodConverter you can add your additional data:

<?php

namespace {VENDOR}\{NAMESPACE}\Model\Cart;

/**
 * Quote shipping method data.
 */
class ShippingMethodConverter extends \Magento\Quote\Model\Cart\ShippingMethodConverter
{

  /**
   * Converts a specified rate model to a shipping method data object.
   *
   * @param string $quoteCurrencyCode The quote currency code.
   * @param \Magento\Quote\Model\Quote\Address\Rate $rateModel The rate model.
   * @return array
   */
  public function modelToDataObject($rateModel, $quoteCurrencyCode)
  {
    $result = parent::modelToDataObject($rateModel, $quoteCurrencyCode);

    /** @var \Magento\Framework\Api\AbstractExtensibleObject $result */
    $result->setData('prazo', $rateModel->getPrazo());

    return $result->__toArray();
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.