Displaying Custom Product Attributes Based on Customer Group in Magento
In Magento, you can easily display custom product attributes based on the customer’s group. This is particularly useful for wholesale pricing or special attributes that should only be visible to certain customers.
Understanding Magento Customer Groups
Magento comes with several pre-defined customer groups like “Guest”, “General”, and “Wholesale”. You can create new groups via Admin > Customers > Customer Groups. In our case, let’s assume you have a group called “Wholesale”.
Creating Wholesale Product Attributes
First, you need to create the wholesale product attributes that should only appear for your wholesale customers.
- Go to
Admin > Catalog > Attributes > Manage Attributes. - Click on “Add New Attribute”.
- Set the attribute code (e.g.,
wholesale_price), scope (Store view), and input type (Text or Price). - Click “Save Attribute”.
Assigning Wholesale Attributes to Products
Now, assign these attributes to your wholesale products.
- Go to
Admin > Catalog > Manage Products. - Edit the product where you want to add the wholesale attribute.
- In the “Product Details” tab, scroll down to find the “Customizable Options” section.
- Click on ”+ Add Attributes”.
- Select the wholesale attributes you created and click “Add Selected Attribute(s)”.
- Fill in the value for this product (e.g., $100.00) and click “Save”.
Hiding Wholesale Attributes from Non-Wholesale Customers
To hide these wholesale attributes from non-wholesale customers, you need to modify your catalog_product_view.xml file:
- Go to
app/code/core/Mage/Catalog/etc/catalog_product_view.xml. - Locate the
<attributes>node where all product attributes are defined. - Add a new
<attribute>node for each wholesale attribute you created.
Your code should look something like this:
<reference name="product.info">
<action method="setTemplate">
<template>catalog/product/view.phtml</template>
</action>
<block type="core/text_list" name="product.extra_attributes" as="extra_attributes">
<action method="addAttribute"><attribute_code>wholesale_price</attribute_code></action>
</block>
</reference>
In the code above, replace wholesale_price with your actual wholesale attribute codes.
Displaying Wholesale Attributes Conditionally
To display these attributes only for wholesale customers, you need to add a condition in your product view template (catalog/product/view.phtml):
<?php if ($this->getCustomerGroupId() == '5'): ?>
<dt><label><?php echo $this->__('Wholesale Price') ?>:</label></dt>
<dd class="price"><?php echo $this->getProduct()->getAttributeText('wholesale_price'); ?></dd>
<?php endif; ?>
In this code, 5 refers to the Wholesale customer group ID. You can find your specific ID in Admin > Customers > Customer Groups.
Conclusion
Displaying custom product attributes based on customer groups is a powerful feature in Magento that allows you to provide tailored experiences for different types of customers.
Remember, it’s always important to test these changes thoroughly before implementing them in a live environment. If you’re unsure about any part of the process, feel free to reach out to our team at Laravel Company.
By following this guide, you should now be able to display wholesale product attributes only for your Wholesale customers, creating a more efficient and controlled shopping experience.