0

I am trying to add a custom Shipping Method that is only available via API (no storefront checkout). My starting point is the MagePsycho custom shipping module, which works fine, but as expected it shows on both checkout and API. I'm trying to find the way to hide it from the checkout (I have seen some posts and modules that make it work in the admin, but it won't work for me).

This is the module collectRates():

        public function collectRates(RateRequest $request)
        {
                /**
                 * Make sure that Shipping method is enabled
                 */
                if (!$this->isActive()) {
                        return false;
                }

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

                $shippingPrice = $this->getConfigData('price');

                $method = $this->rateMethodFactory->create();

                /**
                 * Set carrier's method data
                 */
                $method->setCarrier($this->getCarrierCode());
                $method->setCarrierTitle($this->getConfigData('title'));

                /**
                 * Displayed as shipping method under Carrier
                 */
                $method->setMethod($this->getCarrierCode());
                $method->setMethodTitle($this->getConfigData('name'));

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

                $result->append($method);

                return $result;
        }

So it is simply checking if it is active in the configuration, adding some more config info. How do I check there if the request is coming from the checkout or API? Or is there any other way?

1 Answer 1

0

Let's manage your requirement in a logical way.

At collectRates() you can check the current area of checkout and disbaled your shipping methjod.

$this->appState->getAreaCode() == 'webapi_rest'.

But this solution has a major disadvantage in that the shipping method is also showing on Magento Own checkout page as Magento checkout page shipping method are showing using rest API.

To resolve the issue let's create a field to quote table at the database called is_webapi_checkout and make this field value default value 0 and make this field value to 1 when Add to Cart happen using rest API.

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.