Search the Community
Showing results for tags 'prestashop 1.7 widget'.
-
Bonjour, tout nouveau sur prestashop j'essais d'utiliser les widget pour mon module. j'ai donc ma structure de module (module generator ). puis j'ai ma classe qui etend modules et implement WidgetInterface le module est installé correctement quand je vais dans position je le vois bien dans le displayHome, mais la methode renderWidget n'est jamais appelée. dans le code que je test j'ai fait une copie des methode du module Ps_FeaturedProducts qui utilise widget mais rien ne s'affiche.. par contre si j'utilise le hook home alors je peux afficher ??? <?php /** * 2007-2017 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. * * DISCLAIMER * * Do not edit or add to this file if you wish to upgrade PrestaShop to newer * versions in the future. If you wish to customize PrestaShop for your * needs please refer to http://www.prestashop.com for more information. * * @author PrestaShop SA <[email protected]> * @copyright 2007-2017 PrestaShop SA * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_PS_VERSION_')) { exit; } use PrestaShop\PrestaShop\Core\Module\WidgetInterface; use PrestaShop\PrestaShop\Adapter\Category\CategoryProductSearchProvider; use PrestaShop\PrestaShop\Adapter\Image\ImageRetriever; use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter; use PrestaShop\PrestaShop\Core\Product\ProductListingPresenter; use PrestaShop\PrestaShop\Adapter\Product\ProductColorsRetriever; use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchContext; use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery; use PrestaShop\PrestaShop\Core\Product\Search\SortOrder; class FidintMarket extends Module implements WidgetInterface { protected $config_form = false; public function __construct() { $this->name = 'fidintmarket'; $this->tab = 'market_place'; $this->version = '1.0.0'; $this->author = 'Fid-Int'; $this->need_instance = 0; /** * Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6) */ $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('FID-INT Market'); $this->description = $this->l('Market place FID-INT pour petits commerçants'); $this->confirmUninstall = $this->l('êtes-vous certain de désactiver FID-INT market place'); $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_); //$this->templateFile = 'module:ps_featuredproducts/views/templates/hook/ps_featuredproducts.tpl'; $this->templateFile = 'module:FidintMarket/views/templates/hook/test.tpl'; } /** * Don't forget to create update methods if needed: * http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update */ public function install() { Configuration::updateValue('FIDINTMARKET_LIVE_MODE', false); include(dirname(__FILE__).'/sql/install.php'); return parent::install() && $this->registerHook('header') && $this->registerHook('backOfficeHeader') && $this->registerHook('displayHome') && $this->registerHook('displayRightColumnProduct'); } public function uninstall() { Configuration::deleteByName('FIDINTMARKET_LIVE_MODE'); include(dirname(__FILE__).'/sql/uninstall.php'); return parent::uninstall(); } /** * Create the structure of your form. */ protected function getConfigForm() { return array( 'form' => array( 'legend' => array( 'title' => $this->l('Settings'), 'icon' => 'icon-cogs', ), 'input' => array( array( 'type' => 'switch', 'label' => $this->l('Live mode'), 'name' => 'FIDINTMARKET_LIVE_MODE', 'is_bool' => true, 'desc' => $this->l('Use this module in live mode'), 'values' => array( array( 'id' => 'active_on', 'value' => true, 'label' => $this->l('Enabled') ), array( 'id' => 'active_off', 'value' => false, 'label' => $this->l('Disabled') ) ), ), array( 'col' => 3, 'type' => 'text', 'prefix' => '<i class="icon icon-envelope"></i>', 'desc' => $this->l('Enter a valid email address'), 'name' => 'FIDINTMARKET_ACCOUNT_EMAIL', 'label' => $this->l('Email'), ), array( 'type' => 'password', 'name' => 'FIDINTMARKET_ACCOUNT_PASSWORD', 'label' => $this->l('Password'), ), ), 'submit' => array( 'title' => $this->l('Save'), ), ), ); } /** * Set values for the inputs. */ protected function getConfigFormValues() { return array( 'FIDINTMARKET_LIVE_MODE' => Configuration::get('FIDINTMARKET_LIVE_MODE', true), 'FIDINTMARKET_ACCOUNT_EMAIL' => Configuration::get('FIDINTMARKET_ACCOUNT_EMAIL', '[email protected]'), 'FIDINTMARKET_ACCOUNT_PASSWORD' => Configuration::get('FIDINTMARKET_ACCOUNT_PASSWORD', null), ); } /** * Save form data. */ protected function postProcess() { $form_values = $this->getConfigFormValues(); foreach (array_keys($form_values) as $key) { Configuration::updateValue($key, Tools::getValue($key)); } } /** * Add the CSS & JavaScript files you want to be loaded in the BO. */ public function hookBackOfficeHeader() { if (Tools::getValue('module_name') == $this->name) { $this->context->controller->addJS($this->_path.'views/js/back.js'); $this->context->controller->addCSS($this->_path.'views/css/back.css'); } } /** * Add the CSS & JavaScript files you want to be added on the FO. */ public function hookHeader() { $this->context->controller->addJS($this->_path.'/views/js/front.js'); $this->context->controller->addCSS($this->_path.'/views/css/front.css'); } public function hookDisplayHome() { } public function hookDisplayRightColumnProduct() { /* Place your code here. */ } /** * Load the configuration form */ public function getContent() { $output = ''; $errors = array(); if (Tools::isSubmit('submitHomeFeatured')) { $nbr = Tools::getValue('HOME_FEATURED_NBR'); if (!Validate::isInt($nbr) || $nbr <= 0) { $errors[] = $this->trans('The number of products is invalid. Please enter a positive number.', array(), 'Modules.FeaturedProducts.Admin'); } $cat = Tools::getValue('HOME_FEATURED_CAT'); if (!Validate::isInt($cat) || $cat <= 0) { $errors[] = $this->trans('The category ID is invalid. Please choose an existing category ID.', array(), 'Modules.FeaturedProducts.Admin'); } $rand = Tools::getValue('HOME_FEATURED_RANDOMIZE'); if (!Validate::isBool($rand)) { $errors[] = $this->trans('Invalid value for the "randomize" flag.', array(), 'Modules.FeaturedProducts.Admin'); } if (isset($errors) && count($errors)) { $output = $this->displayError(implode('<br />', $errors)); } else { Configuration::updateValue('HOME_FEATURED_NBR', (int) $nbr); Configuration::updateValue('HOME_FEATURED_CAT', (int) $cat); Configuration::updateValue('HOME_FEATURED_RANDOMIZE', (bool) $rand); $this->_clearCache('*'); $output = $this->displayConfirmation($this->trans('The settings have been updated.', array(), 'Admin.Notifications.Success')); } } return $output.$this->renderForm(); } /** * Create the form that will be displayed in the configuration of your module. */ public function renderForm() { $fields_form = array( 'form' => array( 'legend' => array( 'title' => $this->trans('Settings', array(), 'Admin.Global'), 'icon' => 'icon-cogs', ), 'description' => $this->trans('To add products to your homepage, simply add them to the corresponding product category (default: "Home").', array(), 'Modules.FeaturedProducts.Admin'), 'input' => array( array( 'type' => 'text', 'label' => $this->trans('Number of products to be displayed', array(), 'Modules.FeaturedProducts.Admin'), 'name' => 'HOME_FEATURED_NBR', 'class' => 'fixed-width-xs', 'desc' => $this->trans('Set the number of products that you would like to display on homepage (default: 8).', array(), 'Modules.FeaturedProducts.Admin'), ), array( 'type' => 'text', 'label' => $this->trans('Category from which to pick products to be displayed', array(), 'Modules.FeaturedProducts.Admin'), 'name' => 'HOME_FEATURED_CAT', 'class' => 'fixed-width-xs', 'desc' => $this->trans('Choose the category ID of the products that you would like to display on homepage (default: 2 for "Home").', array(), 'Modules.FeaturedProducts.Admin'), ), array( 'type' => 'switch', 'label' => $this->trans('Randomly display featured products', array(), 'Modules.FeaturedProducts.Admin'), 'name' => 'HOME_FEATURED_RANDOMIZE', 'class' => 'fixed-width-xs', 'desc' => $this->trans('Enable if you wish the products to be displayed randomly (default: no).', array(), 'Modules.FeaturedProducts.Admin'), 'values' => array( array( 'id' => 'active_on', 'value' => 1, 'label' => $this->trans('Yes', array(), 'Admin.Global'), ), array( 'id' => 'active_off', 'value' => 0, 'label' => $this->trans('No', array(), 'Admin.Global'), ), ), ), ), 'submit' => array( 'title' => $this->trans('Save', array(), 'Admin.Actions'), ), ), ); $lang = new Language((int) Configuration::get('PS_LANG_DEFAULT')); $helper = new HelperForm(); $helper->show_toolbar = false; $helper->table = $this->table; $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; $helper->id = (int) Tools::getValue('id_carrier'); $helper->identifier = $this->identifier; $helper->submit_action = 'submitHomeFeatured'; $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( 'HOME_FEATURED_NBR' => Tools::getValue('HOME_FEATURED_NBR', (int) Configuration::get('HOME_FEATURED_NBR')), 'HOME_FEATURED_CAT' => Tools::getValue('HOME_FEATURED_CAT', (int) Configuration::get('HOME_FEATURED_CAT')), 'HOME_FEATURED_RANDOMIZE' => Tools::getValue('HOME_FEATURED_RANDOMIZE', (bool) Configuration::get('HOME_FEATURED_RANDOMIZE')), ); } public function renderWidget($hookName = null, array $configuration = []) { echo "render widget"; /*if (!$this->isCached($this->templateFile, $this->getCacheId('fidintmarket'))) { $variables = $this->getWidgetVariables($hookName, $configuration); if (empty($variables)) { return false; } $this->smarty->assign($variables); }*/ return $this->fetch($this->templateFile, $this->getCacheId('fidintmarket')); } public function getWidgetVariables($hookName = null, array $configuration = []) { $products = $this->getProducts(); if (!empty($products)) { return array( 'products' => $products, 'allProductsLink' => Context::getContext()->link->getCategoryLink($this->getConfigFieldsValues()['HOME_FEATURED_CAT']), ); } return false; } protected function getProducts() { $category = new Category((int) Configuration::get('HOME_FEATURED_CAT')); $searchProvider = new CategoryProductSearchProvider( $this->context->getTranslator(), $category ); $context = new ProductSearchContext($this->context); $query = new ProductSearchQuery(); $nProducts = Configuration::get('HOME_FEATURED_NBR'); if ($nProducts < 0) { $nProducts = 12; } $query ->setResultsPerPage($nProducts) ->setPage(1) ; if (Configuration::get('HOME_FEATURED_RANDOMIZE')) { $query->setSortOrder(SortOrder::random()); } else { $query->setSortOrder(new SortOrder('product', 'position', 'asc')); } $result = $searchProvider->runQuery( $context, $query ); $assembler = new ProductAssembler($this->context); $presenterFactory = new ProductPresenterFactory($this->context); $presentationSettings = $presenterFactory->getPresentationSettings(); $presenter = new ProductListingPresenter( new ImageRetriever( $this->context->link ), $this->context->link, new PriceFormatter(), new ProductColorsRetriever(), $this->context->getTranslator() ); $products_for_template = []; foreach ($result->getProducts() as $rawProduct) { $products_for_template[] = $presenter->present( $presentationSettings, $assembler->assembleProduct($rawProduct), $this->context->language ); } //print_r($products_for_template); return $products_for_template; } } merci pour votre coup de main.