2016-08-14 71 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事件前端只有。您始終可以在全局範圍內定義它,但沒有意義,因爲它只在前端分派,並且只應用於前端的客戶。

相關問題