我有一个允许一个客户组的购物车价格规则。
如何以编程方式获取此客户组 ID?
尝试下面的代码来检索客户组允许的客户 ID。
$rules = Mage::getResourceModel('salesrule/rule_collection')->load();
foreach ($rules as $rule) {
if ($rule->getIsActive()) {
$rule = Mage::getModel('salesrule/rule')->load($rule->getId());
$customer_ids = $rule->getData('customer_group_ids');
}
}
您将拥有客户组 ID 数组。
我建议使用 $rule->getCustomerGroupIds() 而不是 $rule->getData('customer_group_ids')。
在 CE 1.8 中,“salesrule_customer_group”是一个单独的表,并且 Mage_SalesRule_Model_Rule 类具有自己的函数 getCustomerGroupIds() 来检索客户组 ID。
您可以使用依赖注入获取客户组 ID:
protected $_customerGroupIds;
public function __construct(
..........
........
\Magento\Customer\Model\ResourceModel\Group\Collection $customerGroupIds,
.....
) {
$this->_customerGroupIds = $customerGroupIds;
}
public function getCustomerGroupIds()
{
$groupIds = $this->_customerGroupIds->addFieldToSelect('customer_group_id')-
>getData(); //Currently you are getting data as a multidimensional array.
//Now I am converting this into single array
$customerGroupIdsSingleArray = [];
foreach($customerGroupIdsSingleArray as $childArray)
{
foreach ($childArray as $value)
{
$customerGroupIdsSingleArray[] = $value;
}
}
$groupIds = array_values($customerGroupIdsSingleArray);
return $groupIds;
}