首页 文章

Magento - 过滤具有特定客户群价格的产品(按组代码,而不是ID)

提问于
浏览
2

我希望按产品级别设置 group_price 并分配给特定客户组的产品过滤产品列表 .

我当时认为这将是:

$products->addFieldToFilter('group_price', array(
        array(
            'cust_group' => 2
        )
    ));

但似乎 group_price 不是属性 . 此外,尽管产品上设置了团体价格, $_product->getGroupPrice() 始终会返回产品的价格 .

理想情况下,我更愿意通过客户组代码(即批发,零售等)而不是组ID来过滤这些代码(仅适用于有人可能会删除客户组并稍后重新创建并且ID更改的情况) .

有什么想法吗?

2 回答

  • 1

    我有类似的要求,但问题是迭代大型产品集合太慢了 .

    catalog_product_index_price 表包含组信息,因此您可以尝试以下方法:

    $productCollection = Mage::getModel('catalog/product')->getCollection();
    ...
    $productCollection->addFinalPrice();
    $productCollection->getSelect()->where(
        'price_index.customer_group_id = ? AND price_index.group_price IS NOT NULL',
        $customer_group_id
    );
    
  • 0

    我最终使用了这个:

    $products = Mage::getModel('catalog/product')->getResourceCollection();
        $products
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('visibility', array('neq' => 1));
    
        foreach ($products as $product => $value) {
    
            //still not sure how to get 'group_price' without running load() =(
            $productModel = Mage::getModel('catalog/product')->load($value->getId());
            $groupPrices = $productModel->getData('group_price');
    
            // A helper of mine
            // store the customer's groupId to compare against available group prices array below.
            $currentGroupId = Mage::helper('user')->getCustomerGroup()->getId();
    
            // remove products w/o configured group prices
            if (!count($groupPrices)) {
                $products->removeItemByKey($product);
            }
            foreach ($groupPrices as $key) {
                foreach ($key as $subkey => $value) {
                    if ($subkey == "cust_group") {
                        $customerGroup = $subkey;
                        if ($value != $currentGroupId) {
                            $products->removeItemByKey($product);
                        }
                    }
                }
            }
        };
    
    
        $this->_productCollection = $products;
        return $this->_productCollection;
    

相关问题