Magento -在前端和后端之间传递数据


Magento - passing data between front and backend

这是我第一次使用Magento我必须准备模块,其中添加选择字段(是/否)到一般信息(类别在管理面板)。这部分我已经做完了。下一步,当用户进入品类侧,检查在一般信息表单中选择的值。如果用户没有登录,并且在一般信息表单中admin选项被选择为yes,系统将显示"您必须登录"之类的信息。

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>

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

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

您可以观察到在类别加载后在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'是否正确,并且客户是否登录",如果是这样,添加验证错误消息,并重定向到登录页面。

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

现在你需要在你的config.xml中定义你的观察者,并将它连接到事件:

<!-- 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事件,仅在前端中。你也可以在全局作用域中定义这个,但是没有意义,因为它只在前端被分派,并且应该只用于前端的客户。