Jump to content

[RESOLU] Modification Module "Bloc des derniers produits vus"


Grafyx89

Recommended Posts

Bonjour à tous,

 

J'essaie par tout les moyens de modifier le module " Bloc des derniers produits vus " . En effet, ce dernier affiche le nom du produit et sa description.

Je souhaite y afficher plutôt, le nom du fabricant et son prix.

 

J'ai ajouter dans le fichier blockviewed.php le code suivant :

$obj->manufacturer_name = $productsImagesArray[$productViewed]['manufacturer_name'];
$obj->price = $productsImagesArray[$productViewed]['price'];

et dans le .tpl :

<div class="manufacturer">{$productViewed.manufacturer_name|truncate:35|escape:'htmlall':'UTF-8'}</div>
<div class="price"> {$productViewed->price}</div>

Cela ne me renvoi pas en erreur, mais ça n'affiche rien simplement.. Je ne vois pas ou est l'erreur pour ma part, ou bien je n'ai pas inscris le bon code ?.

 

Est-ce que quelqu'un à une éclaircie pour moi ?

Merci de votre aide.

Edited by Grafyx89 (see edit history)
Link to comment
Share on other sites

Merci de ta réponse :) 

 

C'est pourtant la base du module Bloc des derniers produits vus

 

Coté php c'est la même ligne de code que j'ai cité plus haut, par exemple pour la description ils mettent :

$obj->description_short = $productsImagesArray[$productViewed]['description_short'];

pour le faire afficher en front c'est :

<p class="product-description">{$viewedProduct->description_short|strip_tags:'UTF-8'|truncate:40}</p>-->

Je ne comprend pas, pourquoi ça ne serais pas ça pour le prix et le fabricant ? :/

Link to comment
Share on other sites

le fichier blockviewed.php est présenté comme ça :


<?php

if (!defined('_PS_VERSION_'))
    exit;

class BlockViewed extends Module
{

    public function __construct()
    {
        $this->name = 'blockviewed';
        $this->tab = 'front_office_features';
        $this->version = '1.3.1';
        $this->author = 'PrestaShop';
        $this->need_instance = 0;

        $this->bootstrap = true;
        parent::__construct();

        $this->displayName = $this->l('Viewed products block');
        $this->description = $this->l('Adds a block displaying recently viewed products.');
        $this->ps_versions_compliancy = array('min' => '1.6', 'max' => '1.6.99.99');
    }

    public function install()
    {
        return (parent::install() && $this->registerHook('header') && $this->registerHook('leftColumn') && Configuration::updateValue('PRODUCTS_VIEWED_NBR', 2));

    }

    public function getContent()
    {
        $output = '';
        if (Tools::isSubmit('submitBlockViewed'))
        {
            if (!($productNbr = Tools::getValue('PRODUCTS_VIEWED_NBR')) || empty($productNbr))
                $output .= $this->displayError($this->l('You must fill in the \'Products displayed\' field.'));
            elseif ((int)($productNbr) == 0)
                $output .= $this->displayError($this->l('Invalid number.'));
            else
            {
                Configuration::updateValue('PRODUCTS_VIEWED_NBR', (int)$productNbr);
                $output .= $this->displayConfirmation($this->l('Settings updated.'));
            }
        }
        return $output.$this->renderForm();
    }

    public function hookRightColumn($params)
    {
        $productsViewed = (isset($params['cookie']->viewed) && !empty($params['cookie']->viewed)) ? array_slice(array_reverse(explode(',', $params['cookie']->viewed)), 0, Configuration::get('PRODUCTS_VIEWED_NBR')) : array();

        if (count($productsViewed))
        {
            $defaultCover = Language::getIsoById($params['cookie']->id_lang).'-default';

            $productIds = implode(',', array_map('intval', $productsViewed));
            $productsImages = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
            SELECT MAX(image_shop.id_image) id_image, p.id_product, il.legend, product_shop.active, pl.name, pl.description_short, pl.link_rewrite, cl.link_rewrite AS category_rewrite
            FROM '._DB_PREFIX_.'product p
            '.Shop::addSqlAssociation('product', 'p').'
            LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.id_product = p.id_product'.Shop::addSqlRestrictionOnLang('pl').')
            LEFT JOIN '._DB_PREFIX_.'image i ON (i.id_product = p.id_product)'.
            Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
            LEFT JOIN '._DB_PREFIX_.'image_lang il ON (il.id_image = image_shop.id_image AND il.id_lang = '.(int)($params['cookie']->id_lang).')
            LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = product_shop.id_category_default'.Shop::addSqlRestrictionOnLang('cl').')
            WHERE p.id_product IN ('.$productIds.')
            AND pl.id_lang = '.(int)($params['cookie']->id_lang).'
            AND cl.id_lang = '.(int)($params['cookie']->id_lang).'
            GROUP BY product_shop.id_product'
            );

            $productsImagesArray = array();
            foreach ($productsImages as $pi)
                $productsImagesArray[$pi['id_product']] = $pi;

            $productsViewedObj = array();
            foreach ($productsViewed as $productViewed)
            {
                $obj = (object)'Product';
                if (!isset($productsImagesArray[$productViewed]) || (!$obj->active = $productsImagesArray[$productViewed]['active']))
                    continue;
                else
                {
                    $obj->id = (int)($productsImagesArray[$productViewed]['id_product']);
                    $obj->id_image = (int)$productsImagesArray[$productViewed]['id_image'];
                    $obj->cover = (int)($productsImagesArray[$productViewed]['id_product']).'-'.(int)($productsImagesArray[$productViewed]['id_image']);
                    $obj->legend = $productsImagesArray[$productViewed]['legend'];
                    $obj->name = $productsImagesArray[$productViewed]['name'];
                    $obj->manufacturer_name = $productsImagesArray[$productViewed]['manufacturer_name'];
                    $obj->price = $productsImagesArray[$productViewed]['price'];
                    $obj->description_short = $productsImagesArray[$productViewed]['description_short'];
                    $obj->link_rewrite = $productsImagesArray[$productViewed]['link_rewrite'];
                    $obj->category_rewrite = $productsImagesArray[$productViewed]['category_rewrite'];
                    // $obj is not a real product so it cannot be used as argument for getProductLink()
                    $obj->product_link = $this->context->link->getProductLink($obj->id, $obj->link_rewrite, $obj->category_rewrite);

                    if (!isset($obj->cover) || !$productsImagesArray[$productViewed]['id_image'])
                    {
                        $obj->cover = $defaultCover;
                        $obj->legend = '';
                    }
                    $productsViewedObj[] = $obj;
                }
            }

            if (!count($productsViewedObj))
                return;

            $this->smarty->assign(array(
                'productsViewedObj' => $productsViewedObj,
                'mediumSize' => Image::getSize('medium')));

            return $this->display(__FILE__, 'blockviewed.tpl');
        }
        return;
    }

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

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

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

    public function hookHeader($params)
    {
        $id_product = (int)Tools::getValue('id_product');
        $productsViewed = (isset($params['cookie']->viewed) && !empty($params['cookie']->viewed)) ? array_slice(array_reverse(explode(',', $params['cookie']->viewed)), 0, Configuration::get('PRODUCTS_VIEWED_NBR')) : array();

        if ($id_product && !in_array($id_product, $productsViewed))
        {
            $product = new Product((int)$id_product);
            if ($product->checkAccess((int)$this->context->customer->id))
            {
                if (isset($params['cookie']->viewed) && !empty($params['cookie']->viewed))
                    $params['cookie']->viewed .= ','.(int)$id_product;
                else
                    $params['cookie']->viewed = (int)$id_product;
            }
        }
        $this->context->controller->addCSS(($this->_path).'blockviewed.css', 'all');
    }

    public function renderForm()
    {
        $fields_form = array(
            'form' => array(
                'legend' => array(
                    'title' => $this->l('Settings'),
                    'icon' => 'icon-cogs'
                ),
                'input' => array(
                    array(
                        'type' => 'text',
                        'label' => $this->l('Products to display'),
                        'name' => 'PRODUCTS_VIEWED_NBR',
                        'class' => 'fixed-width-xs',
                        'desc' => $this->l('Define the number of products displayed in this block.')
                    ),
                ),
                '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;
        $helper->identifier = $this->identifier;
        $helper->submit_action = 'submitBlockViewed';
        $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(
            'PRODUCTS_VIEWED_NBR' => Tools::getValue('PRODUCTS_VIEWED_NBR', Configuration::get('PRODUCTS_VIEWED_NBR')),
        );
    }
}

c'est moi qui ai rajouté ces deux lignes :

$obj->manufacturer_name = $productsImagesArray[$productViewed]['manufacturer_name'];
$obj->price = $productsImagesArray[$productViewed]['price'];
Edited by Grafyx89 (see edit history)
Link to comment
Share on other sites

Merci de ta réponse,

En effet, avant de voir ta réponse, je me suis rendu compte qu'il manquais un appel dans la requête, alors j'ai ajouté la requête pour la table manufacturer, celà marche, mais pour le prix, c'est juste ajouter "p.price" dans le SELECT, mais ça affiche un prix avec 3 chiffres après la virgule, donc j'imagine le prix HT, et sans le signe € (ça, pas très grave, je le rajouterais à la main)

 

Est-ce que tu as la ligne de code a appelé pour le prix TTC ?

 Merci

 

Ma requête actuelle :

$productsImages = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
            SELECT MAX(image_shop.id_image) id_image, p.id_product, il.legend, product_shop.active, pl.name, p.price, m.`name` as manufacturer_name, pl.description_short, pl.link_rewrite, cl.link_rewrite AS category_rewrite
            FROM '._DB_PREFIX_.'product p
            '.Shop::addSqlAssociation('product', 'p').'
            LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`)
            LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.id_product = p.id_product'.Shop::addSqlRestrictionOnLang('pl').')
            LEFT JOIN '._DB_PREFIX_.'image i ON (i.id_product = p.id_product)'.
            Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
            LEFT JOIN '._DB_PREFIX_.'image_lang il ON (il.id_image = image_shop.id_image AND il.id_lang = '.(int)($params['cookie']->id_lang).')
            LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = product_shop.id_category_default'.Shop::addSqlRestrictionOnLang('cl').')
            WHERE p.id_product IN ('.$productIds.')
            AND pl.id_lang = '.(int)($params['cookie']->id_lang).'
            AND cl.id_lang = '.(int)($params['cookie']->id_lang).'
            GROUP BY product_shop.id_product'
            );
Link to comment
Share on other sites

Yeah !

 

 Je suis parvenu (en récupérant dans le module CrossSelling)

 

Je donne les lignes de la requêtes et les lignes en front pour affichage :

$productIds = implode(',', array_map('intval', $productsViewed));
            $productsImages = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
            SELECT MAX(image_shop.id_image) id_image, p.id_product, il.legend, product_shop.active, pl.name, p.price, product_shop.show_price, m.`name` as manufacturer_name, pl.description_short, pl.link_rewrite, cl.link_rewrite AS category_rewrite
            FROM '._DB_PREFIX_.'product p
            '.Shop::addSqlAssociation('product', 'p').'  

            LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`)
            LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.id_product = p.id_product'.Shop::addSqlRestrictionOnLang('pl').')
            LEFT JOIN '._DB_PREFIX_.'image i ON (i.id_product = p.id_product)'.
            Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
            LEFT JOIN '._DB_PREFIX_.'image_lang il ON (il.id_image = image_shop.id_image AND il.id_lang = '.(int)($params['cookie']->id_lang).')
            LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = product_shop.id_category_default'.Shop::addSqlRestrictionOnLang('cl').')
            WHERE p.id_product IN ('.$productIds.')
            AND pl.id_lang = '.(int)($params['cookie']->id_lang).'
            AND cl.id_lang = '.(int)($params['cookie']->id_lang).'
            GROUP BY product_shop.id_product'
            );

et en dessous :

$obj->manufacturer_name = $productsImagesArray[$productViewed]['manufacturer_name'];
                    $obj->price = $productsImagesArray[$productViewed]['price'];

En FRONT :

<div class="manufacturer">{$viewedProduct->manufacturer_name|truncate:35|escape:'htmlall':'UTF-8'}</div>
                        <span class="price_display">
                            <span class="price"><b>{convertPrice price=$viewedProduct->price}</b></span>
                        </span>

Voilà

 

Merci beaucoup à okom3pom de m'avoir guidé ! c'est vraiment cool. A charge de revanche :)

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...