Question

I have data from the server: $this->getConfigData('groups') = "1,5,9,10"

I need to make a condition, if one of these id matches the id from the custom group of the current user, then continue, if not, then deny.

I tried this approach, but it doesn't work, tell me how you can implement this logic

$customerGroup = $this->_customerSession->getCustomer()->getGroupId(); -> 1
$array_key = array_keys((array)$this->getConfigData('groups'));        -> {0}[1]
if(!in_array($customerGroup, $array_key)){
    return false;
}
Was it helpful?

Solution

explode can split a string by a string into an array

Here is a reference sample.

$customerGroup = $this->_customerSession->getCustomer()->getGroupId();//1

$groupsInString = $this->getConfigData('groups'));//(string) "1,5,9,10"
$groupsInArray = explode(",",$this->getConfigData('groups'));//(array) [1,5,9,10]
if(!in_array($customerGroup, $groupsInArray)){
    return false;
}

OTHER TIPS

You receive a comma separated string from the db. use explode (php native function) and then in_array()

docs for explode https://www.php.net/manual/en/function.explode.php

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top