0

I try (for a special site) to filter the product collection by access group. The attribute is set in the backend by selecting one group or more, hence I have an array of the access group names available for the product.

How can I filter the collection, only leaving me with the products of the access group of the user accessing the site?

I get the current users group:

$roleId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$role = Mage::getSingleton('customer/group')->load($roleId)->getData('customer_group_code');

and then try to filter:

$item_set = Mage::getModel('catalog/product')->getCollection()
        ->addCategoryFilter($category)
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('status', array('eq' => 1));
        ->addAttributeToFilter('access_group',  array('in' => array($role)));

Somehow I realise my approach might be wrong... but I'm a bit stuck :( Any ideas how to achieve this?

[EDIT:] Here's the full code:

<?php

$_category = $this->getDownloadsCategory();
$_helper = Mage::helper('catalog/category');

function getAccessGroupID($name){
    $data = Mage::getResourceModel('catalog/product')
    ->getAttribute('access_group')
    ->getSource()
    ->getOptionId($name);
    return $data;
}

function getItemsForCategory($catID){
    $roleId = Mage::getSingleton('customer/session')->getCustomerGroupId();
    $role = Mage::getSingleton('customer/group')->load($roleId)->getData('customer_group_code');
    $category = Mage::getModel('catalog/category')->load($catID);
    $item_set = Mage::getModel('catalog/product')->getCollection()
        ->addCategoryFilter($category)
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('status', array('eq' => 1))
        ->addAttributeToFilter('access_group',  array('finset' => $role))
        ->load();
    return $item_set;
}

function DrawTree($category) {
    $hlp = Mage::helper('vietload');
    foreach($category as $_category){
        $catId = $_category["entity_id"];
        $catName = $_category["name"];
        $category = Mage::getModel('catalog/category')->load($catId);
        print '<li class="level0 level0 level-top parent">';
        print '<a href="'. $category->getUrl() .'">'. $catName .'</a>';
        $items = getItemsForCategory($catId);
        if($items->count()>0) {
            print '<ul>';
            foreach ($items as $item) {
                //var_dump($item->getAttributeText('access_group'));
                print '<li class="level1"><a href="'. $hlp->getFullUrl($item) .'">'. $item->getName() .'</a></li>';
            }
            print '</ul>';
            print '<span class="head"><a href="javasript:void(0);"></a></span>';
        }
        print '</li>';
    }
}

?>

<div class="block block-verticalmenu">
    <div class="block-title">
        <strong><span><?php echo $this->__("Downloads");?></span></strong>
    </div>
    <div class="block-content"><div></div>
        <ul id="ma-accordion" class="accordion news_accordion">
            <?php DrawTree($_category); ?>
        </ul>
    </div>
</div>
  • why you have thought wrong? – Amit Bera Nov 11 '14 at 9:57
1

when calling addAttributeToFilter('access_group', array('in' => array($role))); magento expects access_group to be one single value.
this translates to WHERE access_group in (2,3,4).
But role is one single value and access group contains the multiple values.
What you need is WHERE FIND_IN_SET(role, access_group).

For that use

addAttributeToFilter('access_group',  array('finset' => $role));
  • thx marius. I tried this, but it gives me zero results... even though I see that the product is assigned to several access groups and $role has the correct current user group. – Awinad Nov 11 '14 at 10:12
0

You can try the below code.

Use conditions like this:

$collection->addFieldToFilter(
    array('field_1'), // columns
    array( // conditions
        array( // conditions for field_1
            array('in' => array('text_1', 'text_2', 'text_3')
        ),
    )
);

This will generate an SQL WHERE condition something like:

... WHERE (
         (field_1 IN ('text_1', 'text_2', 'text_3'))
    )

Hope you can understand the Syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.