在 prestashop 模块中管理多个商店

Managing multiple stores in prestashop module

我有一个 Prestashop 1.6 模块。它适用于一家商店,但我需要让它适用于我使用多商店功能创建的其他商店。

"make it work" 我的意思是我希望能够为每个不同的商店分开配置,这样每个商店都有自己的插件配置,而不用与网站上的每个商店共享一个配置。目前插件只有一个配置,如果我在其中一个商店中更改它,更改会影响所有其他商店。

这是代码:

<?php
/**
 * 2007-2015 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * @author    PrestaShop SA <contact@prestashop.com>
 * @copyright 2007-2015 PrestaShop SA
 * @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 * International Registered Trademark & Property of PrestaShop SA
 */

class Homecategories extends Module
{
    public function __construct()
    {
        $this->name = 'homecategories';
        $this->tab = 'front_office_features';
        $this->version = '1.0';
        $this->need_instance = 0;

        parent::__construct();

        $this->page = basename(__FILE__, '.php');
        $this->displayName = $this->l('Homepage Categories Customized');
        $this->description = $this->l('Displays categories on your homepage');
    }

    public function install() {
        Configuration::updateValue('HOMEPAGE_CATEGORIES_IDS', '');
        Configuration::updateValue('HOMEPAGE_CATEGORIES_PROD_COUNT', 4);
        return parent::install() &&
        $this->registerHook('displayHomeTab') &&
        $this->registerHook('displayHomeTabContent') &&
        $this->registerHook('displayHeader');
    }

    public function uninstall() {
        $this->unregisterHook('displayHomeTab') &&
        $this->unregisterHook('displayHomeTabContent') &&
        $this->unregisterHook('displayHeader') &&
        parent::uninstall();
    }

    public function hookDisplayHeader() {
        $this->context->controller->addCSS(_PS_THEME_DIR_.'/css/category.css', 'all');
        $this->context->controller->addCSS(_THEME_CSS_DIR_.'product_list.css');
    }

    public function hookDisplayHome($params) {
        $root_cat = Category::getRootCategory($this->context->cookie->id_lang);
        $prodCount = (int)Configuration::get('HOMEPAGE_CATEGORIES_PROD_COUNT');
        $categoryIds = Configuration::get('HOMEPAGE_CATEGORIES_IDS');
        if(!empty($categoryIds) && trim($categoryIds) != '') {
            $idsArray = explode(',', $categoryIds);
            $allProducts = array();

            foreach($idsArray as $id) {
                $category = new Category($id, (int)$this->context->language->id);

                $products = $category->getProducts((int)$this->context->language->id, 1, 10000);
                shuffle($products);
                for($i = 0; $i<$prodCount; $i++) {
                    $allProducts[] = $products[$i];
                }
            }

            $this->context->smarty->assign(
                array(
                    'products' => $allProducts,
                    'prodCount' => $prodCount,
                    'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
                )
            );
        }
        return $this->display(__FILE__, '/views/templates/hooks/homecategories.tpl');
    }



    public function renderForm() {
        $fields_form = array(
            'form' => array(
                'legend' => array(
                    'title' => $this->l('Settings'),
                    'icon' => 'icon-cogs'
                ),
                'description' => $this->l('Display custom categories on home page.'),
                'input' => array(
                    array(
                        'type' => 'text',
                        'label' => $this->l('Categories ID'),
                        'name' => 'HOMEPAGE_CATEGORIES_IDS',
                        'class' => 'fixed-width-xs',
                        'desc' => ''//$this->l('Insert how many products you\'d like to display for each category.'),
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('Products per category'),
                        'name' => 'HOMEPAGE_CATEGORIES_PROD_COUNT',
                        'class' => 'fixed-width-xs',
                        'desc' => ''//$this->l('Insert how many products you\'d like to display for each category.'),
                    )
                ),
                'submit' => array(
                    'title' => $this->l('Save'),
                )
            ),
        );

        $helper = new HelperForm();
        $helper->show_toolbar = false;
        $helper->table = $this->table;
        $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
        $helper->default_form_language = $lang->id;
        $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
        $this->fields_form = array();
        $helper->id = (int)Tools::getValue('id_carrier');
        $helper->identifier = $this->identifier;
        $helper->submit_action = 'submitHomepageCategories';
        $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        $helper->tpl_vars = array(
            'fields_value' => $this->getConfigFieldsValues(),
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id
        );

        return $helper->generateForm(array($fields_form));
    }

    public function getConfigFieldsValues() {
        return array(
            'HOMEPAGE_CATEGORIES_PROD_COUNT' => Tools::getValue('HOMEPAGE_CATEGORIES_PROD_COUNT', (int)Configuration::get('HOMEPAGE_CATEGORIES_PROD_COUNT')),
            'HOMEPAGE_CATEGORIES_IDS' => Tools::getValue('HOMEPAGE_CATEGORIES_IDS', Configuration::get('HOMEPAGE_CATEGORIES_IDS')),
        );
    }

    public function getContent() {
        $output = '';
        $errors = array();
        if (Tools::isSubmit('submitHomepageCategories')) {
            $ids = str_replace('/\s+/g', '', Tools::getValue('HOMEPAGE_CATEGORIES_IDS'));
            $idsArray = explode(',', $ids);
            foreach($idsArray as $id) {
                if(!Validate::isInt($id) || (int)$id < 0) {
                    $errors[] = $this->l('All IDs must be positive integers. Unrecognized value: ') . $id;
                }
            }
            $prodCount = trim(Tools::getValue('HOMEPAGE_CATEGORIES_PROD_COUNT'));
            if (!Validate::isInt($prodCount) || (int)$prodCount <= 0)
                $errors[] = $this->l('Product count must be a positive non-zero integer.');
            if (isset($errors) && count($errors))
                $output = $this->displayError(implode('<br />', $errors));
            else {
                Configuration::updateValue('HOMEPAGE_CATEGORIES_IDS', $ids);
                Configuration::updateValue('HOMEPAGE_CATEGORIES_PROD_COUNT', (int)$prodCount);
                Tools::clearCache(Context::getContext()->smarty, $this->getTemplatePath('homecategories.tpl'));
                $errors[] = $ids;
                $output = $this->displayConfirmation($this->l('Your settings have been updated.'));
            }
        }

        return $output.$this->renderForm();
    }


    public function hookDisplayHomeTabContent($params) {
        return $this->hookDisplayHome($params);
    }
    public function hookLeftColumn($params) {
        return $this->hookDiplayHome($params);
    }

    public function hookRightColumn($params) {
        return $this->hookDisplayHome($params);
    }

    public function hookDisplayTopColumn($params) {
        return $this->hookDisplayHome($params);
    }

    public function hookDisplayHomeTab($params) {
        return $this->display(__FILE__, 'tab.tpl', $this->getCacheId('homefeatured-tab'));
    }
}

我尝试了什么:

结果是一样的。

我转储了 $this->context->shop,发现 id 属性始终是 1,无论我在哪个商店。商店 ID 似乎总是相同的,即使如果我改变商店,那么 属性 一点也不安全。

我该怎么办?我错过了什么吗?如何为每个商店创建不同的配置,而不是让所有商店共享一个?

通过使用 Configuration::get('KEY');Configuration::updateValue('KEY', 'VALUE');,它将根据您当前的上下文获取和更新密钥。这意味着要更改另一个商店的值,在管理员中,您只需 select 可用 select 框中的存储(通常在通知附近的顶部)。

员工上下文商店不是基于您访问管理员的url。如果您之前没有 select编辑过另一个,它将默认为主要的。

另一方面,您可以通过在包含所有商店的 foreach 中使用 Configuration::updateValue('KEY', 'VALUE', false, null, (int)$id_shop)Configuration::get($key, null, null, $id_shop) 来编写您的模块以包含所有选项,而不是更改上下文商店。

另一方面,前台(前端)的上下文商店是根据您访问它的 url 设置的。