Display a custom block on a category or product page only if the user is logged in and joins some specific groups

I want to show the contents of the magento static block on the product page, and possibly also on the product listing page.

The static block will be visible only to LOGGED IN USERS , which connect certain groups (for example, logged in users who are inside reseller , general and clients , and not inside testing or guests )).

PS I need to add it also in the sidebar, where I have multi-level navigation.

+6
source share
2 answers
 $array =[1,2,3]; //your group ids array if(Mage::getSingleton('customer/session')->isLoggedIn()) { $customerData = Mage::getSingleton('customer/session')->getCustomer(); $groupId = $customerData->getGroupId(); if(in_array($groupId,$array)){ echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml(); } } 

place the code above in the template name below

interface / [your-theme] / [your-package] /catalog/product/view.phtml

interface / [your-topic] / [your-package] /catalog/product/list.phtml

interface / [your-theme] / [your-package] /catalog/layer/view.phtml

+1
source

Hi, you can do it easily.

Step1: Create a showstatic.phtml template with pp/design/frondtend/YourPackage/YourPackage/template/cms/

Step 2: on this phtml, you can check the client login to the system and its group ID, and then use the Static block.

code:

 <?php $loggedIn = Mage::getSingleton('customer/session')->isLoggedIn(); // add Handler when customer is loggedin if($loggedIn): $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId(); //Get customer Group name $group = Mage::getModel('customer/group')->load($groupId); $Groupcode=$group->getData('customer_group_code'); /* static customer group code array for which static block will call */ $Enableforgroup=array(13, 2, 8, 7); // call static array if (in_array($Groupcode, $Enableforgroup)){ echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml(); } endif; ?> 

Now you can call this phtml in the product category

Category:

Category page app/design/frondtend/YourPackage/YourPackage/template/catalog/category/view.phtml

 echo $this->getLayout()->createBlock('core/template')->setTemplate('cms/showstatic.phtml')->toHtml(); 

Product:

Product page app/design/frondtend/YourPackage/YourPackage/template/catalog/product/view.phtml

 echo $this->getLayout()->createBlock('core/template')->setTemplate('cms/showstatic.phtml')->toHtml(); 

AS magento process:

Create a local.xml file in app/design/frondtend/YourPackage/YourPackage/layout/

Code:

 <?xml version="1.0" encoding="UTF-8"?> <layout version="0.1.0"> <!-- add this handler for show all category pages --> <catalog_category_view> <reference name="content"> <!-- reference block for page where you want show the static block --> <block type="core/template" name="showstatic" template="cms/showstatic.phtml"> <block type="cms/block" name="showstatichow" > <!-- The content of this block is taken from the database by its block_id. You can manage it in admin CMS -> Static Blocks --> <action method="setBlockId"><block_id>footer_links_company</block_id></action> </block> </block> </reference> </catalog_category_view> <!-- add this handler for show all product pages --> <catalog_product_view> <reference name="content"> <!-- reference block for page where you want show the static block --> <block type="core/template" name="showstatic" template="cms/showstatic.phtml"> <block type="cms/block" name="showstatichow" > <!-- The content of this block is taken from the database by its block_id. You can manage it in admin CMS -> Static Blocks --> <action method="setBlockId"><block_id>footer_links_company</block_id></action> </block> </block> </reference> </catalog_product_view> </layout> 

Create showstatic.phtml template showstatic.phtml in pp/design/frondtend/YourPackage/YourPackage/template/cms/

Code:

 <?php $loggedIn = Mage::getSingleton('customer/session')->isLoggedIn(); // show cms blocks when customer is loggedin if($loggedIn): // get Customer group id from session echo $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId(); /* $Enableforgroup is array for reseller, general and clients * group ids */ $Enableforgroup=array(13, 2, 1, 7); // call static array if (in_array($groupId, $Enableforgroup)){ echo $this->getChildHtml("showstatichow"); } endif; ?> 
+2
source

Source: https://habr.com/ru/post/988394/


All Articles