在预购物模块中管理多个商店



我有一个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)
    

    它不起作用,价值仍然在商店之间共享

  • 由于第一个选项不起作用,我想通过将它们命名为商店 1KEY_1、商店 2KEY_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似乎也总是相同的,所以财产根本不安全。

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

通过使用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 设置的。

最新更新