在prestashop模块中管理多个商店

时间:2017-05-24 08:33:16

标签: module prestashop prestashop-1.6 multistore

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

通过“让它工作”我的意思是我希望能够为每个不同的商店分离配置,这样每个商店都有自己的插件配置,而不与网站上的每个商店共享一个配置。目前该插件只有一个配置,如果我在其中一个商店内更改它,则更改会影响所有其他商店。

这是代码:

<?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'));
    }
}

我尝试了什么:

  • 我尝试通过更改

    的每次出现来添加文档中所述的商店ID
    Configuration::get('KEY'); 
    

    Configuration::get('KEY', null, null, (int)$this->context->shop->id);
    

    Configuration::updateValue('KEY', 'VALUE');
    

    Configuration::updateValue('KEY', 'VALUE', false, null, (int)$this->context->shop->id)
    

    它不起作用,商店之间的价值仍然分享

  • 由于第一个选项不起作用,我考虑通过为商店1命名KEY_1,为商店2命名KEY_2来为字段指定不同的名称,等等。我认为这会有解决了这个问题,所以我改变了每次出现的事情

    Configuration::get('KEY'); 
    

    Configuration::get('KEY_' . (int)$this->context->shop->id);
    

    Configuration::updateValue('KEY_' . (int)$this->context->shop->id, 'VALUE');
    

    Configuration::updateValue('KEY', 'VALUE', false, null, (int)$this->context->shop->id)
    

结果是一样的。

我转储$this->context->shop并发现id属性始终为1,无论我在哪家商店。即使我,商店ID似乎总是一样的改变商店,所以财产根本不安全。

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

1 个答案:

答案 0 :(得分:2)

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

员工上下文商店不基于您访问管理员的网址。如果您之前没有选择另一个,它将默认为主要的一个。

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

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

相关问题