【问题标题】:How to get the Customer Group binded to a Sale rule in Magento?如何让客户组绑定到 Magento 中的销售规则?
【发布时间】:2013-05-24 11:02:00
【问题描述】:

我为一个客户组设置了购物车价格规则。

如何以编程方式获取此客户组 ID?

【问题讨论】:

    标签: magento magento-1.7


    【解决方案1】:

    尝试下面的代码来检索客户组允许的客户 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 数组。

    【讨论】:

      【解决方案2】:

      我建议使用 $rule->getCustomerGroupIds() 而不是 $rule->getData('customer_group_ids')。

      在 CE 1.8 中,“salesrule_customer_group”是一个单独的表,Mage_SalesRule_Model_Rule 类有自己的函数 getCustomerGroupIds() 来检索客户组 ID。

      【讨论】:

        【解决方案3】:

        您可以使用依赖注入获取客户组 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;
        
        
        }
        

        【讨论】:

          猜你喜欢
          • 2011-12-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多