This is probably more than one way to do this, but my method of choice is to create a plugin for the \ Magento \ Shipping \ Model \ Shipping , in particular the collectRates () function. Our requirements were more specific than yours (below).
Basic logical flow ...
collectRates () (unmodified function in \ Magento \ Shipping \ Model \ Shipping, collects bids for all delivery methods)
afterCollectRates () (plugin)
- At this point, all delivery methods were called and bids were saved in our $ request object.
- You can identify products that are in the cart through $ request-> getAllItems ()
- NOTE. Child / Parent products are separate items, and depending on your storeβs configuration, one or the other may not have the custom attribute that you want to view.
- You can view all delivery methods / bids through $ request-> getResult () β getAllRates ()
- I did not find a key function for deleting a bet, my workaround was ...
- Disconnect all data in speed to delete.
- After changing all bets, use the foreach () loop to store them in tempArray (with some logic, so as not to add if the cost == 0, etc.).
- Now clear and reset all existing bets via $ request-> getResult () β reset ()
- Finally, add bids from your tempArray to
Depending on how you calculate bids, you can also expand on various shipping methods so that you can completely bypass them when certain products are in the basket (probably not for your use case, but for those trying to turn off UPS / FedEx / etc. bids)
As already mentioned, our requirements were more extensive, and we also had the beforeCollectRates () function, which actually created an array of products and some other logic (we had to restrict various delivery methods, add processing for certain products, and use dimensional logic to create a shipping list mailboxes to be sent to UPS / FedEx, etc. for the actual part of CollectRates ().)
source share