Php 仅当用户登录并加入某些特定组时,才在类别或产品页面上显示自定义块

Php 仅当用户登录并加入某些特定组时,才在类别或产品页面上显示自定义块,php,magento,Php,Magento,我想在产品页面和产品列表页面上显示magento静态块的内容 静态块仅对加入特定组的登录用户可见(例如登录用户位于经销商、常规和客户,而非测试或客户) 另外,我还需要将其添加到侧栏中,在侧栏中我有分层导航。您好,您可以轻松完成此操作 步骤1:在pp/design/frondend/YourPackage/YourPackage/template/cms/ 步骤2:在此phtml上,您可以检查客户登录和it组id,然后使用show Static block 代码为: <?php

我想在产品页面和产品列表页面上显示magento静态块的内容

静态块仅对加入特定组的
登录用户
可见(例如
登录
用户位于
经销商
常规
客户
,而非
测试
客户


另外,我还需要将其添加到侧栏中,在侧栏中我有分层导航。

您好,您可以轻松完成此操作

步骤1:
pp/design/frondend/YourPackage/YourPackage/template/cms/

步骤2:在此phtml上,您可以检查客户登录和it组id,然后使用show Static block

代码为:

<?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;

?>
<?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>
<?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;

?> 
产品:

<?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;

?>
<?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>
<?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;

?> 
产品页面位于
app/design/frondtend/YourPackage/YourPackage/template/catalog/Product/view.phtml

echo $this->getLayout()->createBlock('core/template')->setTemplate('cms/showstatic.phtml')->toHtml();
echo $this->getLayout()->createBlock('core/template')->setTemplate('cms/showstatic.phtml')->toHtml();
作为magento流程: 在
app/design/frondtend/YourPackage/YourPackage/layout/

代码为:

<?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;

?>
<?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>
<?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;

?> 
将上面的代码放在下面的模板名称中

前端/[您的主题]/[您的软件包]/catalog/product/view.phtml

前端/[您的主题]/[您的软件包]/catalog/product/list.phtml


frontend/[您的主题]/[您的软件包]/catalog/layer/view.phtml

请注意,即使这个答案实际上可以解决OP问题,这也会在视图中引入很多逻辑,这在Magento这样的MVC框架中是非常糟糕的做法。我知道这一点,但仅供快速参考。。我已经将代码放在这个过程中了,好吧,那么看起来您没有正确理解Magento MVC。查看=
.pthml
文件。下面是视图中的构建逻辑(属于块或控制器)。嘿,卢卡,请检查我的答案