Reputation: 101
I am new to Magento 2. Anybody knows of a way to disable a shipping method based on an attribute withing the products in the cart. Let's say we wanna enable store pickup only for certain products. On the onepage checkout these are updated with a post /rest/en/V1/guest-carts/SESSION_ID/estimate-shipping-methods but tried the module-checkout and module-quote and still can't find where this code is so i can extend it. Would be helpful if somebody has been thru this before.
Thanks
Upvotes: 8
Views: 3899
Reputation: 133
Probably more than one way to accomplish this, but my method of choice was to create a plugin for \Magento\Shipping\Model\Shipping, specifically the collectRates() function. Granted our requirements were more specific than yours (at bottom).
Basic logic flow is...
collectRates() (Unmodified function in \Magento\Shipping\Model\Shipping, Collects rates for all shipping methods)
afterCollectRates() (plugin)
Depending on how you calculate rates, you may also want to extend the various shipping methods so you can bypass them entirely when certain products are in the cart (probably not for your use case, but for anyone trying to disable UPS/FedEx/etc. rates)
As mentioned, our requirements were more extensive and we also had a beforeCollectRates() function which actually created the product array and some other logic (we had to restrict various shipping methods, add handling for specific products, and use dimensional logic to create a list of shipping boxes to send to UPS/FedEx, etc. for the actual CollectRates() part.)
Upvotes: 7