Magento 2 Filtering Methods Based on Product Attribute

I'm new to Magento 2. Does anyone know how to disable a delivery method based on the attribute with products in the cart. Let me say that we want a pick-up shop only for certain products. On one verification page, they are updated using the messages post / rest / en / V1 / guest-carts / SESSION_ID / methods-shipping-methods, but they have tried unit testing and module quotes and still cannot find where this code is so I can extend it. It would be helpful if someone had been before.

thanks

+5
source share
1 answer

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 ().)

+2
source

All Articles