在prestashop模块中管理多个商店

问题描述 投票:0回答:1

我有一个 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 [email protected] so we can send you a copy immediately.
 *
 * @author    PrestaShop SA <[email protected]>
 * @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似乎总是相同的,因此该属性不是完全安全。

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

php module prestashop prestashop-1.6 multistore
1个回答
3
投票

通过使用

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

员工上下文商店不是基于您访问管理员的 URL。如果您之前没有选择其他选项,它将默认为主选项。

另一方面,您可以通过在包含所有商店的 foreach 中使用

Configuration::updateValue('KEY', 'VALUE', false, null, (int)$id_shop)
Configuration::get($key, null, null, $id_shop)
来编写模块以拥有所有选项,而不是更改上下文商店。

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

© www.soinside.com 2019 - 2024. All rights reserved.