2016-08-14 87 views
0

这是我第一次与Magento合作。我必须准备模块,将选择字段(是/否)添加到常规信息(管理面板中的类别)。我已经完成了这部分。下一步是检查用户进入类别侧时在常规信息表格中选择的值。如果用户没有登录并且通用信息表单中的管理员选项选择为“是”,则系统应显示如下信息:“您必须登录”。Magento - 在前端和后端之间传递数据

Below my folder structure: 

- app 
-> code 
-> community 
-> AttributeCategory 
->CustomAttributeCategory-> 
- etc 
    -> config.xml 

<?xml version="1.0"?> 
<config> 
    <modules> 
     <AttributeCategory_CustomAttributeCategory> 
      <version>0.0.3</version> 
     </AttributeCategory_CustomAttributeCategory> 
    </modules> 

    <global> 
     <resources> 
      <add_category_attribute_login> 
       <setup> 
        <module>AttributeCategory_CustomAttributeCategory</module> 
        <class>Mage_Catalog_Model_Resource_Setup</class> 
       </setup> 
       <connection> 
        <use>core_setup</use> 
       </connection> 
      </add_category_attribute_login> 
      <add_category_attribute_login_write> 
       <connection> 
        <use>core_write</use> 
       </connection> 
      </add_category_attribute_login_write> 
      <add_category_attribute_login_read> 
       <connection> 
        <use>core_read</use> 
       </connection> 
      </add_category_attribute_login_read> 
     </resources> 
    </global> 
</config> 

- sql -> add_category_attribute_login -> 
- mysql4-install-0.0.3.php : 


<?php 
$this->startSetup(); 
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'is_category_allowed', [ 
    'group'  => 'General Information', 
    'type'  => 'int', 
    'input'  => 'select', 
    'label'  => 'required logged-in user', 
    'sort_order' => 1000, 
    'visible' => true, 
    'required' => true, 
    'source' => 'eav/entity_attribute_source_boolean', 
    'visible_on_front' => true, 
    'global'  => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, 
    'option'  => [ 
     'values' => [ 
      0 => 'No', 
      1 => 'Yes', 
     ] 
    ], 
]); 
$this->endSetup(); 

AND 

- app->etc->modules: 
AttributeCategory_CustomAttributeCategory.xml: 

    <?xml version="1.0"?> 
<config> 
    <modules> 
     <AttributeCategory_CustomAttributeCategory> 
      <active>true</active> 
      <codePool>community</codePool> 
     </AttributeCategory_CustomAttributeCategory> 
    </modules> 
</config> 

请告诉我如何在用户访问类别页面时检查前面的值?

回答

0

您应创建一个观察器,查看加载的类别的属性值,然后执行检查,并在必要时设置错误消息并重定向到客户的登录页面。

您可以在之后加载该类别,并观察Mage_Catalog_CategoryController::_initCategory中的事件catalog_controller_category_init_after。这意味着该类别的所有属性都可以查看,而不管它们是否在类别统计表中。

创建一个观察者:

// File: app/code/community/AttributeCategory/CustomAttributeCategory/Model/Observer.php 
class AttributeCategory_CustomAttributeCategory_Model_Observer 
{ 
    public function checkLoggedInForCategory(Varien_Event_Observer $event) 
    { 
     // Get the category from the event 
     /** @var Mage_Catalog_Model_Category $category */ 
     $category = $event->getCategory(); 

     // Get the customer's session model 
     /** @var Mage_Customer_Model_Session $customerSession */ 
     $customerSession = Mage::getSingleton('customer/session'); 

     if ($category->getIsCategoryAllowed() && !$customerSession->isLoggedIn()) { 
      // Add a custom message here? 
      $customerSession->addError('You must be logged in to view this category.'); 

      // Redirect to login page 
      Mage::app() 
       ->getResponse() 
       ->setRedirect(Mage::getUrl('customer/account/login')) 
       ->sendResponse(); 
      exit; 
     } 
    } 
} 

这里的逻辑基本上说“得到该事件的类别”,这是可以做到,因为它被分派点并将其作为论据,“得到了客户会议“无论客户是否已登录,始终可用”,“检查'is_category_allowed'是否真实并且客户是而不是登录”,如果是这样,请添加验证错误消息,并重定向到登录页面。

登录页面自动呈现并显示所有消息块条目,因此您不需要手动处理显示。

现在,你需要在你的​​3210定义你的观察,并将其连接到事件:

<!-- File: app/code/community/AttributeCategory/CustomAttributeCategory/etc/config.xml --> 
<?xml version="1.0"?> 
<config> 
    <modules> 
     <AttributeCategory_CustomAttributeCategory> 
      <version>0.0.3</version> 
     </AttributeCategory_CustomAttributeCategory> 
    </modules> 

    <global> 
     ... 
    </global> 
    <frontend> 
     <events> 
      <catalog_controller_category_init_after> 
       <observers> 
        <ensure_customer_can_view_category> 
         <class>AttributeCategory_CustomAttributeCategory_Model_Observer</class> 
         <method>checkLoggedInForCategory</method> 
        </ensure_customer_can_view_category> 
       </observers> 
      </catalog_controller_category_init_after> 
     </events> 
    </frontend> 
</config> 

我希望这有助于。网上有大量关于如何创建观察者等的资源,它们是非常有用的东西。 这注册了一个名为AttributeCategory_CustomAttributeCategory_Model_Observer的类中的观察者,方法名为checkLoggedInForCategory,它连接到catalog_controller_category_init_after事件前端只有。您始终可以在全局范围内定义它,但没有意义,因为它只在前端分派,并且只应用于前端的客户。