Skip to content

I created a simple shipping method, I want to set the country rate manually? Example US x2

i try to use $request->getDestRegionCode() but it dosent work

<?php

class Custom extends AbstractCarrier implements CarrierInterface
{

protected $_code = 'custom';

protected $rateResultFactory;

protected $rateMethodFactory;

public function __construct(
    ScopeConfigInterface $scopeConfig,
    ErrorFactory $rateErrorFactory,
    LoggerInterface $logger,
    ResultFactory $rateResultFactory,
    MethodFactory $rateMethodFactory,
    array $data = []
)
{
    $this->rateResultFactory = $rateResultFactory;
    $this->rateMethodFactory = $rateMethodFactory;
    parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
}

public function getAllowedMethods()
{
    return ['custom' => $this->getConfigData('name')];
}

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

    /** @var MagentoShippingModelRateResult $result */
    $result = $this->rateResultFactory->create();

    /** @var MagentoQuoteModelQuoteAddressRateResultMethod $method */
    $method = $this->rateMethodFactory->create();
    $method->setCarrier('custom');
    $method->setCarrierTitle($this->getConfigData('title'));

    $method->setMethod('custom');
    $method->setMethodTitle($this->getConfigData('name'));


    $amount = $this->getConfigData('price');
     
    $shippingPrice = $this->getFinalPriceWithHandlingFee($amount);
     
    $dest = strtolower($request->getDestCountryId());
    
    $method->setPrice($shippingPrice);
    $method->setCost($amount);

    $result->append($method);

    return $result;
}
}