0

Need to restrict my custom shipping method to not showing on checkout page using http service to check if shipping address is applicable.

That is my code

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

    /** @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($this->_code);
    $method->setCarrierTitle($this->getConfigData('title'));

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

    $shippingCost = (float)$this->getConfigData('shipping_cost');

    $method->setPrice($shippingCost);
    $method->setCost($shippingCost);

    $result->append($method);

    return $result;
}

I need to check with http service selected shipping address before append method. Can anybody help me how to get current selected shipping method?

1 Answer 1

0

Apologies if I missing the question, below is your method amended to show you the change I would do to implement your need

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

// This is the function to add and within it, you may add an API call to your http service
    if (!$this->myHttpServiceIsNotApplicable()) {
       return false;
    }


    /** @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($this->_code);
    $method->setCarrierTitle($this->getConfigData('title'));

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

    $shippingCost = (float)$this->getConfigData('shipping_cost');

    $method->setPrice($shippingCost);
    $method->setCost($shippingCost);

    $result->append($method);

    return $result;
}
1
  • Thank you, but my really problem is how to get shipping address selected on checkout page. Commented Jun 3, 2020 at 20:32

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.