【问题标题】:Magento - passing data between front and backendMagento - 在前端和后端之间传递数据
【发布时间】:2016-08-14 14:51:38
【问题描述】:

这是我第一次使用 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>

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

【问题讨论】:

    标签: php xml magento magento-1.9


    【解决方案1】:

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

    您可以观察到事件catalog_controller_category_init_afterMage_Catalog_CategoryController::_initCategory 中调度 类别被加载。这意味着该类别的所有属性都可以查看,无论它们是否在类别平面表中。

    创建观察者:

    // 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 事件。您也可以始终在全局范围内定义它,但没有意义,因为它只在前端调度,并且应该只用于前端的客户。

    【讨论】:

      猜你喜欢
      • 2014-10-07
      • 1970-01-01
      • 2014-08-16
      • 2020-10-04
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      • 1970-01-01
      相关资源
      最近更新 更多