Prestashop 1.6 - Avatar client dans les avis

User Profile Advanced Module

Nous allons nous simplifier le travail en achetant un module qui permet l'upload d'avatar pour les utilisateurs du site, nous pouvons le trouver à moins de 30 euros dans la boutique addons de Prestashop (le module est compatible 1.6). Une fois le module installé nous allons devoir y faire quelques modifications, il ne sera donc plus possible de faire les mises à jours sans perdre ces dernières, idem pour le module de commentaires (ce tutoriel ne propose pas de faire des overrides, ce qui n'est vraiment pas bien du tout)...

On ajoute une colonne "customer_avatar" dans notre base de donnée

Tout d'abord, allons faire un petit tour dans notre base de donnée via phpmyadmin. Nous ajoutons dans la table PREFIX_product_comment une colonne de type TEXT que l'on nommera "customer_avatar" (onglet structure). Choississez NULL dans la case défaut.
Ce qui nous donne:


Edition de ProductComment.php situé dans /modules/productcomments

On recherche vers la ligne 44:

/** @var integer Customer name */
public $customer_name;

On ajoute après:

/** @var string Customer avatar */
public $customer_avatar;

On recherche vers la ligne 76:

'customer_name' => array('type' => self::TYPE_STRING),

On ajoute après:

'customer_avatar' => array('type' => self::TYPE_STRING),

On recherche:

public static function getByProduct($id_product, $p = 1, $n = null, $id_customer = null)
{

On ajoute après:

/* On va chercher l'avatar en fonction de l'id customer */

$customer_avatar2 = Db::getInstance()->getValue('
SELECT avatar_thumb
FROM `'._DB_PREFIX_.'avatar2customer` a2c
WHERE `id_customer` = '.$id_customer);


$query2 = 'UPDATE '._DB_PREFIX_.'product_comment SET customer_avatar = "'.$customer_avatar2.'"
WHERE id_customer = '.$id_customer;

Db::getInstance()->Execute($query2);

/* Fin de on va chercher l'avatar en fonction de l'id customer */

On ajoute dans la suite de la fonction:

pc.`customer_avatar`,

Vers:

IF(c.id_customer, CONCAT(c.`firstname`), pc.customer_name) customer_name,

Ce qui donne:

if (!Validate::isUnsignedId($id_product))
return false;
$validate = Configuration::get('PRODUCT_COMMENTS_MODERATE');
$p = (int)$p;
$n = (int)$n;
if ($p <= 1)
$p = 1;
if ($n != null && $n <= 0)
$n = 5;

$cache_id = 'ProductComment::getByProduct_'.(int)$id_product.'-'.(int)$p.'-'.(int)$n.'-'.(int)$id_customer.'-'.(bool)$validate;
if (!Cache::isStored($cache_id))
{
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT pc.`id_product_comment`,
(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment` AND pcu.`usefulness` = 1) as total_useful,
(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment`) as total_advice, '.
((int)$id_customer ? '(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcuc WHERE pcuc.`id_product_comment` = pc.`id_product_comment` AND pcuc.id_customer = '.(int)$id_customer.') as customer_advice, ' : '').
((int)$id_customer ? '(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_report` pcrc WHERE pcrc.`id_product_comment` = pc.`id_product_comment` AND pcrc.id_customer = '.(int)$id_customer.') as customer_report, ' : '').'
IF(c.id_customer, CONCAT(c.`firstname`), pc.customer_name) customer_name, pc.`customer_avatar`, pc.`id_customer`, pc.`content`, pc.`grade`, pc.`date_add`, pc.title
FROM `'._DB_PREFIX_.'product_comment` pc
LEFT JOIN `'._DB_PREFIX_.'customer` c ON c.`id_customer` = pc.`id_customer`
WHERE pc.`id_product` = '.(int)($id_product).($validate == '1' ? ' AND pc.`validate` = 1' : '').'
ORDER BY pc.`date_add` ASC
'.($n ? 'LIMIT '.(int)(($p - 1) * $n).', '.(int)($n) : ''));
Cache::store($cache_id, $result);
}

On édite le fichier profileadvanced.class.php situé dans modules/profileadv

On ajoute à la fonction saveAvatar vers la ligne 543:

$query2 = 'UPDATE '._DB_PREFIX_.'product_comment SET customer_avatar = "'.$avatar_thumb.'"
WHERE id_customer = '.$id_customer;
Db::getInstance()->Execute($query2);

Ce qui nous donne:

public function saveAvatar($data=null){
$id_customer = $data['id_customer'];
$avatar = $data['avatar'];
$avatar_thumb = $data['avatar_thumb'];

$show_my_profile = $this->_is_show(array('show_my_profile' => $data['show_my_profile']));

// if exist record
$query = 'SELECT COUNT(*) as count from '._DB_PREFIX_.'avatar2customer
WHERE id_customer = '.$id_customer;

$result = Db::getInstance()->GetRow($query);
$exist_record = $result['count'];

if($exist_record){
//update
$query = 'UPDATE '._DB_PREFIX_.'avatar2customer SET avatar = "'.$avatar.'",
avatar_thumb = "'.$avatar_thumb.'",
is_show = '.$show_my_profile.'
WHERE id_customer = '.$id_customer;

} else {
// insert
$query = 'INSERT INTO '._DB_PREFIX_.'avatar2customer (id_customer, avatar, avatar_thumb,is_show)
VALUES ('.$id_customer.', "'.$avatar.'", "'.$avatar_thumb.'", '.$show_my_profile.') ';

}

$query2 = 'UPDATE '._DB_PREFIX_.'product_comment SET customer_avatar = "'.$avatar_thumb.'"
WHERE id_customer = '.$id_customer;

Db::getInstance()->Execute($query);
Db::getInstance()->Execute($query2);

}

Nous allons désormais pouvoir appeller les variables dans le tpl du module commentaire.

On édite productcomments.tpl situé dans le dossier modules de votre thème.

On ajoute dans le foreach...

Chercher:

{if $comments}
{foreach from=$comments item=comment}
{if $comment.content}

Ajouter après:

{if $comment.customer_avatar}
//*a href="{$modules_dir}/profileadv/shopper.php?id={$comment.id_customer}">//*img class="img-responsive" src="http://www.tangence-web.com/upload/{$comment.customer_avatar}">//*a>
//*img class="img-responsive" src="{$modules_dir}/profileadv/i/avatar_m.gif">
{/if}

Remplacer les //* par des crochets

Commentaires


Notice: Undefined index: customer_avatar in /home/tangence/public_html/cache/smarty/compile/0c/23/4a/0c234a9ea2459e7dbf9533a4ca298d269dd2f2e8.file.productcomments.tpl.php on line 56
le 14/07/2015 01:24:27

I feel that is among the so much significant information for me. And i'm glad studying your article. However should observation on few general issues, The website taste is ideal, the articles is in point of fact excellent D. Just right activity, cheers dkkdeebdfbeaceed