Like the title stated.
We’ve created a custom shipping module, it’s quite basic and works like it should.
However, when I continue to the checkout it seems to forget it and select another shipping method. If I select it again and refresh it also forgets it.
It doesn’t matter if the user is logged in or a guest etc.. There are no (error)logs in the console nor in var/log/...
.
What am I missing here?
Why doesn’t it keept the correct delivery method?
(Renamed vendor/module since it contains the company their name)
Carrier code
Vendor/Module/Model/Carrier/ShippingBezorging.php
<?php
/**
* Copyright © 2021 .... All rights reserved.
*/
namespace VendorModuleModelCarrier;
use MagentoQuoteModelQuoteAddressRateRequest;
use MagentoShippingModelRateResult;
class ShippingBezorging extends MagentoShippingModelCarrierAbstractCarrier implements
MagentoShippingModelCarrierCarrierInterface
{
/**
* @var string
*/
protected $_code = 'bezorging';
/**
* @var MagentoShippingModelRateResultFactory
*/
protected $_rateResultFactory;
/**
* @var MagentoQuoteModelQuoteAddressRateResultMethodFactory
*/
protected $_rateMethodFactory;
/**
* Shipping constructor.
*
* @param MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig
* @param MagentoQuoteModelQuoteAddressRateResultErrorFactory $rateErrorFactory
* @param PsrLogLoggerInterface $logger
* @param MagentoShippingModelRateResultFactory $rateResultFactory
* @param MagentoQuoteModelQuoteAddressRateResultMethodFactory $rateMethodFactory
* @param array $data
*/
public function __construct(
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoQuoteModelQuoteAddressRateResultErrorFactory $rateErrorFactory,
PsrLogLoggerInterface $logger,
MagentoShippingModelRateResultFactory $rateResultFactory,
MagentoQuoteModelQuoteAddressRateResultMethodFactory $rateMethodFactory,
array $data = []
) {
$this->_rateResultFactory = $rateResultFactory;
$this->_rateMethodFactory = $rateMethodFactory;
parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
}
/**
* get allowed methods
* @return array
*/
public function getAllowedMethods()
{
return [$this->_code => $this->getConfigData('name')];
}
/**
* @return float
*/
private function getShippingPrice($configPrice)
{
$shippingPrice = $this->getFinalPriceWithHandlingFee($configPrice);
return $shippingPrice;
}
/**
* @param RateRequest $request
* @return bool|Result
*/
public function collectRates(RateRequest $request)
{
if (!$this->getConfigFlag('active')) {
return false;
}
/** @var MagentoShippingModelRateResult $result */
$result = $this->_rateResultFactory->create();
$configPrice = false;
$country = $request->getDestCountryId();
$areas = json_decode($this->getConfigData('postal_areas'));
if($country!=='NL'){
$error = $this->_rateErrorFactory->create();
$error->setCarrier($this->_code);
$error->setCarrierTitle($this->getConfigData('title'));
$error->setErrorMessage(
empty($country)||$country=='US'?'Vul de gegevens hierboven in om te kijken of we bezorgen op jouw adres.':'Onze eigen bezorging is niet beschikbaar buiten Nederland.'
);
return $error;
}
$postalcode = $request->getDestPostcode();
if(!empty($postalcode)){
$re = '/([0-9]+)/m';
preg_match_all($re, $postalcode, $matches, PREG_SET_ORDER, 0);
if(empty($matches))
return false;
$postalcode = intval($matches[0][0]);
foreach($areas as $area){
$_country = is_array($area->country)? $area->country[0] : $area->country;
if($country != $_country)
continue;
$postals = explode('-', $area->postalcode);
if(count($postals) > 1){
$start = intval(str_replace('*', '0', $postals[0]));
$end = intval(str_replace('*', '9', $postals[1]));
if($postalcode > $start && $postalcode < $end){
$configPrice = $area->price;
break;
}
}
else{
$postal = str_replace('*', '[0-9]', $postals[0]);
$re = '/' . $postal . '/m';
preg_match_all($re, $postalcode, $matches, PREG_SET_ORDER, 0);
if(!empty($matches)){
$configPrice = $area->price;
break;
}
}
}
}else
$configPrice = 20; // Added for testing purpose
if($configPrice===false){
$error = $this->_rateErrorFactory->create();
$error->setCarrier($this->_code);
$error->setCarrierTitle($this->getConfigData('title'));
$error->setErrorMessage(
'Eigen bezorging is niet beschikbaar voor jouw postcodegebied.'
);
return $error;
}
/** @var MagentoQuoteModelQuoteAddressRateResultMethod $method */
$method = $this->_rateMethodFactory->create();
$method->setCarrier($this->_code);
$method->setCarrierTitle($this->getConfigData('title'));
// same carrier
// with different method
$method->setMethod($this->_code);
$method->setMethodTitle($this->getConfigData('name'));
$amount = $this->getShippingPrice($configPrice);
$method->setPrice($amount);
$method->setCost($amount);
$result->append($method);
return $result;
}
}