I'm creating a custom shipping method on Magento2. I got the admin working ok. There are 3 different services it choose from, lets say "14hrs", 24hrs and Economy.
The problems comes in the front end, checkout page. I can see my Method, but I can't figure out how to display the select for the services. I've read several shipping method module tutorials, but none talks about this.
Currently this is my CustomShipping.php:
namespace TCK\ASM\Model\Carrier;
use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Rate\Result;
use Magento\Checkout\Model\Cart;
use TCK\ASM\Model\Source\Method;
class ASMShipping extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements
\Magento\Shipping\Model\Carrier\CarrierInterface
{
/**
* @var string
*/
protected $_code = 'ASMShipping';
/**
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory
* @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory
* @param array $data
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
\Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
\Magento\Checkout\Model\Cart $cart,
\TCK\ASM\Model\Source\MethodFactory $metodoFactory,
array $data = []
) {
$this->_rateResultFactory = $rateResultFactory;
$this->_rateMethodFactory = $rateMethodFactory;
$this->_metodoFactory = $metodoFactory;
parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
$this->cart = $cart;
}
/**
* @return array
*/
public function getAllowedMethods()
{
return ['ASMShipping' => $this->getConfigData('name')];
}
/**
* @param RateRequest $request
* @return bool|Result
*/
public function collectRates(RateRequest $request)
{
if (!$this->getConfigFlag('active')) {
return false;
}
$err = null;
$envio = array();
//Mage2
/** @var \Magento\Shipping\Model\Rate\Result $result */
$result = $this->_rateResultFactory->create();
/** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
$method = $this->_rateMethodFactory->create();
$method->setCarrier('ASMShipping');
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethod('ASMShipping');
$method->setMethodTitle($this->getConfigData('name'));
/*you can fetch shipping price from different sources over some APIs, we used price from config.xml - xml node price*/
//$amount = $this->getConfigData('handling_fee');
$amount=0;
$envio["cpOrg"]=$request->_data['postcode'];
$envio["codPaisOrg"] = '34';
$envio["cpDst"] = $request->_data['dest_postcode'];
$envio["peso"] = $request->_data['package_weight'];
$envio["bultos"] = 1;
$envio["precioProducto"] = $request->_data['package_value'];
$amount=$request->_data['package_value'];
$method->setPrice($amount);
$method->setCost($amount);
$result->append($method);
$items=$this->cart->getQuote()->getAllVisibleItems();
return $result;
/** end func **/
}
}