0

I want to add a new shipping method available only for some customer groups. I use the documentation to create a new shipping method, but there is no filtering method for customer groups in the tutorial. Tell me how to add select to filter by customer group or how to add a filter by customer group by hardcode in the module?

https://devdocs.magento.com/guides/v2.3/howdoi/checkout/checkout-add-custom-carrier.html

0

You can get the current customer group inside the collectRates to get the current customer id.

$quote = null;
foreach ($request->getAllItems() as $item) {
    $quote = $item->getQuote();
    break;
}
$customerGroupId = $quote ? $quote->getCustomerGroupId() ? 0;

Let's say you have a list of customer groups for which this is allowed. You can get it from config, or have some rules that determine them

$allowedGroups = [1, 2, 3];
if (!in_array($customerGroupId, $allowedGroups)) {
    return false; //this means the method is not allowed.
}
// regular code goes here
1
  • What does the first example code do? Do you have an example of how to make a config with a selector for the shipping method? – Alice Sep 2 '20 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.