通过商店或网站限制模块功能的最佳方法是什么 [英] What is the best way to limit a modules functionality by store or website

查看:42
本文介绍了通过商店或网站限制模块功能的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我们有很多拥有多个网站的客户,我们必须为他们编写很多模块.有时,扩展程序可能会覆盖Magento核心默认情况下所执行的某些功能,因此他们想为一家商店而不是另一家商店这样做.显然,我们可以在代码中添加逻辑以查看其存储位置,但是我认为有一些更优雅的方法可以做到这一点.

Basically we have a lot of clients that have multiple websites and we have to write a lot of modules for them. Sometimes an extension may override some functionality that Magento's core does by default and they want to do this for one store but not another. Obviously we can put logic in the code to see what store it is but I am thinking there is something more elegant way to do this.

推荐答案

好问题.

我将以另一种方式解决此问题. 模块结构:

I would have solved this problem on another way. Module structure:

Custom
| - Module
| - - Model
| - - - Product.php
| - - - Customer.php

我认为需要创建一个依赖于商店的类. 如果要为英国商店创建某些功能,则需要为英国商店声明此类,将其写入配置文件中,并使用工厂类进行调用. 例如在config.xml

For my opinion it is need to be created class that depends from store. If you want to create some functionality for Store UK, you need to declare this class for UK store, write it in configuration file and call it with factory class. For example in the config.xml

    <config>
        <stores>
            <store_uk>
                 <catalog_product>Custom_Module_Model_Store_Uk_Product</product_attribute>
                 <customer>Custom_Module_Model_Store_Uk_Customer</customer>
            </store_uk>
            <store_en>
                 <catalog_product>Custom_Module_Model_Store_En_Product</catalog_product>
            </store_en>
        </stores>
    </config>

创建类存储路由器:

Create class store router:

class Custom_Module_Model_Store_Router
{
    public function callMethod($method, $args)
    {
        if (strpos($method, '/') !== false) {
            $method = explode('/', $method);
        }

        if (count($method) != 2) {
            return false;
        }

        $handler = $method[0];
        $method  = $method[1];

        $object = $this->_getObject($handler);
        if ($object) {
            //already checked if method exists 
            retun $object->$method($args);
        }

        return false;
    }

    public function hasStoreMethod($method)
    {
        if (strpos($method, '/') !== false) {
            $method = explode('/', $method);
        }

        if (count($method) != 2) {
            return false;
        }

        $handler = $method[0];
        $method  = $method[1];

        $object = $this->_getObject($handler);
        if (method_exists($object, $method)) { 
            //Bingo
            return true;
        }

        return false;
    }

    protected function _getObject($handler)
    {
        $storeCode = Mage::app()->getStore(true)->getCode();

        $handlerClassName = Mage::getStoreConfig($storeCode . '/' . $handler);

        if (empty($handlerClassName)) {
            return false;
        }

        $handlerInstance = Mage::getModel($handlerClassName);
        //here we can save instance into the _handlers etc.

        return $handlerInstance;
    }
}

此类将作为默认自定义内容

This class will be as default customization

//in your custom module product class
Custom_Module_Model_Product extends Mage_Catalog_Model_Product_Attribute
{
    public function getAttributes($groupId = null, $skipSuper = false)
    {
        $routerStore = Mage::getSingleton('custom_module/store_router');
        if ($routerStore->hasStoreMethod('catalog_product/getAttributes')) {
            $attributes = $routerStore->callMethod('catalog_product/getAttributes', array('groupId' => $groupId, 'skipSuper' => $skipSuper));
            return $attributes;
        }

        return parent::getAttributes($groupId, $skipSuper);
    }
}

该类仅用于存储英国类

//custom module product class for uk store
Custom_Module_Model_Store_Uk_Product extends Mage_Catalog_Model_Product_Attribute
{
    public function getAttributes($groupId = null, $skipSuper = false)
    {
        $attributes = parent::getAttributes($groupId, $skipSuper);

        // do some specific stuff

        return $attributes;
    }
}

完成此步骤后,您将获得具有以下模块结构的明确的自定义类:

After this steps you will have clear customization classes with module structure listed below:

Custom
| - Module
| - - Model
| - - - Store
| - - - - Uk
| - - - - - Product.php
| - - - - - Customer.php
| - - - - En
| - - - - - Product.php
| - - - - Router.php
| - - - Product.php
| - - - Customer.php

我希望这对您的多店开发有帮助

I hope this will help for your multistore development

这篇关于通过商店或网站限制模块功能的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆