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?


1 Answer 1


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();
$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
  • 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
    Commented Sep 2, 2020 at 10:20

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.