Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

magento

I want to show the content of a magento static block on product page and maybe also on product listing page.

Static block will be visible only for LOGGED IN USERS that are joining specific groups (eg. logged in users that are inside reseller, general and clients, not inside testing or guests).

P.S. I need to add it also in sidebar, where I have layered navigation.

like image 1000
Luca Frank Guarini Avatar asked Jun 02 '15 14:06

Luca Frank Guarini


2 Answers

Hi you can do this easyly.

Step1: Create template showstatic.phtml at app/design/frondtend/YourPackage/YourPackage/template/cms/

Step2: on this phtml,you can check customer loggin in and it group id then using show Static block.

code is :

<?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 to call this phtml at category,product by

Category:

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

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

Product:

Product page at 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 local.xml file at app/design/frondtend/YourPackage/YourPackage/layout/

Code is:

<?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 template file showstatic.phtml at app/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;

?> 
like image 146
Amit Bera Avatar answered Oct 21 '22 20:10

Amit Bera


$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 above code in template name below

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

frontend/[your-theme]/[your-package]/catalog/product/list.phtml

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

like image 31
Prashant Valanda Avatar answered Oct 21 '22 21:10

Prashant Valanda