Restricting actions by a customer group in Magento

Restricting actions by a customer group in Magento

In this article I’d like to explain how to create multiselect product attribute which will allow to restrict actions depending on a customer group. This feature can be used to hide price of the product, disable product addition to cart or set other restrictions basing on a customer group.

First of all, you need to create a new module with a setup script sql/modulenamespace_setup/mysql4-install-0.0.1.php:

<?php

/* @var $installer Mage_Catalog_Model_Resource_Setup */
$installer = $this;

$installer->startSetup();

$installer->addAttribute('catalog_product', 'restrict_groups', array(
    'label'                    => 'Available to groups',
    'group'                    => 'General',
    'input'                    => 'multiselect',
    'required'                 => false,
    'source'                   => 'modulenamespace/source_group',
    'backend'                  => 'modulenamespace/backend_group',
    'global'                   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'used_in_product_listing'  => true,
));

$installer->endSetup();

Don’t forget that your module is required to use Mage_Catalog_Model_Resource_Setup model, this is defined in config.xml of the module:

<config>
    <global>
        <resources>
            <modulenamespace_setup>
                <setup>
                    <module>Namespace_Module</module>
                    <class>Mage_Catalog_Model_Resource_Setup</class>
                </setup>
            </modulenamespace_setup>
        </resources>
    </global>
</config>

Next thing is to define source and backend models. Backend model is used for processing saving and loading attribute data. This is needed because multiselect attribute values are stored as comma-separated numbers and on load they should be extracted to an array. Source model defines data which is used for filling values of the multiselect element. Create two files Model/Backend/Group.php and Model/Source/Group.php with a following content:

<?php

class Atwix_Customerprice_Model_Backend_Group extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
{
    const ATTRIBUTE_CODE = 'restrict_groups';

    public function beforeSave($object)
    {
        $attributeCode = $this->getAttribute()->getName();
        if ($attributeCode == self::ATTRIBUTE_CODE) {
            $data = $object->getData($attributeCode);
            $postData = Mage::app()->getRequest()->getPost('product');
            if (!empty($postData) && empty($postData[$attributeCode])) {
                $data = array();
            }
            if (is_array($data)) {
                $object->setData($attributeCode, implode(',', $data));
            }
        }

        parent::beforeSave($object);
    }

    public function afterLoad($object)
    {
        $attributeCode = $this->getAttribute()->getName();
        if ($attributeCode == self::ATTRIBUTE_CODE) {
            $data = $object->getData($attributeCode);
            if (!is_array($data)) {
                $object->setData($attributeCode, explode(',', $data));
            }
        }

        parent::afterLoad($object);
    }
}
<?php

class Namespace_Module_Model_Source_Group extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{

    public function getAllOptions()
    {
        $groups = Mage::getResourceModel('customer/group_collection')
            ->addFieldToFilter('customer_group_id', array('gt'=> 0))
            ->load()
            ->toOptionArray();

        array_unshift($groups, array(
            'value' => 0,
            'label' => Mage::helper('core')->__('All Customer Groups')
        ));

        return $groups;
    }
}

After this you should be able to see a list of available groups on product edit and save attribute’s value. After this, the following code can be used in any place to check permissions:

<?php
    $restrictGroups = $product->getRestrictGroups();
    $allowAction = (!$restrictGroups || in_array(Mage::helper('customer')->getCustomer()->getGroupId(), $restrictGroups));

I hope this article was useful insight on how to create custom multiselect attributes and process theirs data. Thanks for reading and stay tuned for new articles on our blog.

Clients

Smart Brands Choose Us.

From startups backed by Mark Cuban and Sir Richard Branson, to some of the biggest eCommerce operations in the world, Atwix helps our clients deliver unparalleled eСommerce experiences. We’re proud to work with the following companies: