Jump to content

essai de module même fabricant


miles

Recommended Posts

Hello

Je cherche un module qui se greffe dans la fiche produit affichant les produits d'un meme fabricant(sur le meme fonctionnement q le module dans la meme categorie).N'ayant pas trouvé j'ai essayé de le dév en me basant sur le module productcategory.

Modif faites j'ai réussi à l'installer sans pb mais par contre il n'apparait pas dans ma fiche produit. Etant pas un pro de dev à votre avis qu'est ce qui coince (à mon avis c dans le php...un pb avec l'id)
voilà mon productsmanufacturer.php, lui est associé un css et un tpl

<?php

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

class productsmanufacturer extends Module
{
    function __construct()
    {
         $this->name = 'productsmanufacturer';
         $this->version = '1.2.1';
         $this->tab = 'Products';

       parent::__construct();

       $this->displayName = $this->l('Products Manufacturer');
       $this->description = $this->l('Display products of the same Manufacturer on the product page');
    }

   function install()
   {
        if (!parent::install())
            return false;
        return $this->registerHook('productfooter');
   }

   private function getCurrentProduct($products, $id_current)
   {
       if ($products)
           foreach ($products as $key => $product)
               if ($product['id_product'] == $id_current)
                   return $key;
       return false;
   }

   public function hookProductFooter($params)
   {
       global $smarty, $cookie;

       $idProduct = intval(Tools::getValue('id_product'));
       $product = new Product(intval($idProduct));

       $Manufacturer = new Manufacturer(1);
       if (isset($params['Manufacturer']->id_manufacturer))
           $Manufacturer = $params['Manufacturer'];
       if ($Manufacturer->id_manufacturer == 1 AND isset($product->id_manufacturer_default) AND $product->id_manufacturer_default > 1)
           $Manufacturer = New Manufacturer(intval($product->id_manufacturer_default));
       if (!Validate::isLoadedObject($Manufacturer))
           Tools::displayError('Bad Manufacturer!');
       if (intval($Manufacturer->id_manufacturer) === 1)
           return;

       if (!$Manufacturer->active) 
           return;

       // Get infos
       $sizeOfManufacturerProducts = $Manufacturer->getProducts(intval($cookie->id_lang), 1, 30, NULL, NULL, true);
       $manufacturerProducts = $Manufacturer->getProducts(intval($cookie->id_lang), 1, $sizeOfManufacturerProducts);

       $middlePosition = 0;

       //remove current product from the list
       $current_product_key = null;
       if (is_array($manufacturerProducts) AND sizeof($manufacturerProducts))
       {
           foreach ($manufacturerProducts as $key => $manufacturerProducts)
           {
               if ($manufacturerProducts['id_product'] == $idProduct)
               {
                   $current_product_key = $key;
                   break;
               }
           }
           if (isset($manufacturerProducts[$current_product_key]))
               unset($manufacturerProducts[$current_product_key]);


           // Get positions
           $middlePosition = round($sizeOfManufacturerProducts / 2, 0);
           $productPosition = $this->getCurrentProduct($manufacturerProducts, $idProduct);

           // Flip middle product with current product
           if ($productPosition)
           {
               $tmp = $manufacturerProducts[$middlePosition-1];
               $manufacturerProducts[$middlePosition-1] = $manufacturerProducts[$productPosition];
               $manufacturerProducts[$productPosition] = $tmp;
           }

           // If products tab higher than 30, slice it
           if ($sizeOfManufacturerProducts > 30)
           {
               $manufacturerProducts = array_slice($manufacturerProducts, $middlePosition - 15, 30, true);
               $middlePosition = 15;
           }
       }

       // Display tpl
       $smarty->assign('manufacturerProducts', $manufacturerProducts);
       $smarty->assign('middlePosition', $middlePosition);
       return $this->display(__FILE__, 'productsmanufacturer.tpl');
   }
}
?>



merci guys!

Link to comment
Share on other sites

bon j'essaie d'avancer dans ma recherche mais je galère

Je sais qu'on peut contourner le champs accessoires pour afficher certains produits en plus dans la page produit, mais je reste persuadé qu'un module indépendant permettant d'afficher dans la page produit les produits du meme fabricant serait drolement utile, le meme module que "dans la meme categorie" mais qui serait "du meme fabricant"


Y a bien quelqu'un qui serait partant pour m'aider, j'offre le café :-)

Link to comment
Share on other sites

  • 3 months later...
  • 2 weeks later...

il faut regarder la première réponse de Rocky, tout est dedans !

1- Tu prends ton dossier "productscategory", tu le copies/colles dans le répertoire modules en appelant maintenant cette copie : "productsmanufacturer".

2- Dans le répertoire, modifies le fichier config.xml en mettant par exemple :

<?xml version="1.0" encoding="UTF-8" ?>

productsmanufacturer
Products Manufacturer
1.0
Affiche les produits du meme fabricant sur la page produits.
ton nom
front_office_features
1
1



3- Dans le fichier fr.php (et toutes les autres langues activées dans ta boutique), modifies les traductions.

4- Renommes le fichier "productscategory.tpl" en "productsmanufacturer.tpl".

5- Renommes le fichier "productscategory.php" en "productsmanufacturer.php".

6- mets le code ci-dessous dans le fichier productsmanufacturer.php :

<?php

class ProductsManufacturer extends Module
{
    function __construct()
    {
         $this->name = 'productsmanufacturer';
         $this->version = '1.2.1';
         $this->tab = 'Products';

       parent::__construct();
       $this->page = basename(__FILE__, '.php');
       $this->displayName = $this->l('Products Manufacturer');
       $this->description = $this->l('Display products of the same manufacturer on the product page');
    }

   function install()
   {
        if (!parent::install())
            return false;
        return $this->registerHook('productfooter');
   }

   private function getCurrentProduct($products, $id_current)
   {
       if ($products)
           foreach ($products as $key => $product)
               if ($product['id_product'] == $id_current)
                   return $key;
       return false;
   }

   public function hookProductFooter($params)
   {
       global $smarty, $cookie;

       $idProduct = intval(Tools::getValue('id_product'));
       $product = new Product(intval($idProduct));

       // Get infos

       $sizeOfManufacturerProducts = Manufacturer::getProducts(intval($product->id_manufacturer), intval($cookie->id_lang), 1, 30, NULL, NULL, true);
       $manufacturerProducts = Manufacturer::getProducts(intval($product->id_manufacturer), intval($cookie->id_lang), 1, $sizeOfManufacturerProducts);        

       // Get positions
       $middlePosition = round($sizeOfManufacturerProducts / 2, 0);
       $productPosition = $this->getCurrentProduct($manufacturerProducts, $idProduct);

       // Flip middle product with current product
       if ($productPosition)
       {
           $tmp = $manufacturerProducts[$middlePosition-1];
           $manufacturerProducts[$middlePosition-1] = $manufacturerProducts[$productPosition];
           $manufacturerProducts[$productPosition] = $tmp;
       }

       // If products tab higher than 30, slice it
       if ($sizeOfManufacturerProducts > 30)
       {
           $manufacturerProducts = array_slice($manufacturerProducts, $middlePosition - 15, 30, true);
           $middlePosition = 15;
       }


       // Display tpl
       $smarty->assign('categoryProducts', $manufacturerProducts);
       $smarty->assign('middlePosition', $middlePosition);
       return $this->display(__FILE__, 'productsmanufacturer.tpl');
   }
}
?>



Normalement comme ça tout devrait fonctionner.

Maintenant comme il le dit ensuite en bas, tu peux aussi modifier la variable ‘categoryProducts’ en ‘manufacturerProducts pour garder une certaine consistance, mais ce n'est pas indispensable, si tu le fais il faut bien modifier toutes ces variables dans tout le module.

Link to comment
Share on other sites

  • 3 months later...

Bonjour,

 

je me permets de rebondir sur cet ancien sujet car je n'arrive pas à faire la même chose avec la version 1.4.5.1 actuelle de Prestashop.

 

Voici mon code modifié, le module apparaît bien dans la liste et il s'installe mais n'apparaît pas dans la fiche Produit, sans message d'erreur donc je ne sais pas ce que je fais de mal...

 

Voici le contenu de mon fichier PHP où j'ai essayé de mettre à jour les instances category en manufacturer mais sans succès :

 

<?php
if (!defined('_PS_VERSION_'))
exit;
class productsManufacturer extends Module
{
 private $_html;
public function __construct()
 {
   $this->name = 'productsmanufacturer';
   $this->version = '1.3';
 $this->author = 'PrestaShop';
   $this->tab = 'front_office_features';
 $this->need_instance = 0;

 parent::__construct();

 $this->displayName = $this->l('Products Manufacturer');
 $this->description = $this->l('Display products of the same collection on the product page.');

 if (!$this->isRegisteredInHook('header'))
  $this->registerHook('header');
 }
public function install()
{
  if (!parent::install() OR !$this->registerHook('productfooter') OR !$this->registerHook('header') OR !Configuration::updateValue('PRODUCTSCATEGORY_DISPLAY_PRICE', 0))
   return false;
  return true;
}

public function uninstall()
{
  if (!parent::uninstall() OR !Configuration::deleteByName('PRODUCTSCATEGORY_DISPLAY_PRICE'))
   return false;
  return true;
}

public function getContent()
{
 $this->_html = '';
 if (Tools::isSubmit('submitCross') AND Tools::getValue('displayPrice') != 0 AND Tools::getValue('displayPrice') != 1)
  $this->_html .= $this->displayError('Invalid displayPrice');
 elseif (Tools::isSubmit('submitCross'))
 {
  Configuration::updateValue('PRODUCTSCATEGORY_DISPLAY_PRICE', Tools::getValue('displayPrice'));
  $this->_html .= $this->displayConfirmation($this->l('Settings updated successfully'));
 }
 $this->_html .= '
 <form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post">
 <fieldset><legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Settings').'</legend>
  <label>'.$this->l('Display price on products').'</label>
  <div class="margin-form">
   <input type="radio" name="displayPrice" id="display_on" value="1" '.(Configuration::get('PRODUCTSCATEGORY_DISPLAY_PRICE') ? 'checked="checked" ' : '').'/>
   <label class="t" for="display_on"> <img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'" title="'.$this->l('Enabled').'" /></label>
   <input type="radio" name="displayPrice" id="display_off" value="0" '.(!Configuration::get('PRODUCTSCATEGORY_DISPLAY_PRICE') ? 'checked="checked" ' : '').'/>
   <label class="t" for="display_off"> <img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'" title="'.$this->l('Disabled').'" /></label>
   <p class="clear">'.$this->l('Show the price on the products in the block.').'</p>
  </div>
  <center><input type="submit" name="submitCross" value="'.$this->l('Save').'" class="button" /></center>
 </fieldset>
 </form>';
 return $this->_html;
}

private function getCurrentProduct($products, $id_current)
{
 if ($products)
  foreach ($products AS $key => $product)
   if ($product['id_product'] == $id_current)
 return $key;
 return false;
}

public function hookProductFooter($params)
{
 global $smarty, $cookie;

 $idProduct = (int)(Tools::getValue('id_product'));
 $product = new Product((int)($idProduct));
 /* If the visitor has came to this product by a category, use this one */
 if (isset($params['manufacturer']->id_manufacturer))
  $manufacturer = $params['manufacturer'];
 /* Else, use the default product category */
 else
 {
  if (isset($product->id_manufacturer_default) AND $product->id_manufacturer_default > 1)
   $manufacturer = New Manufacturer((int)($product->id_manufacturer_default));
 }

 if (!Validate::isLoadedObject($manufacturer) OR !$manufacturer->active)
  return;
 // Get infos
 $manufacturerProducts = $manufacturer->getProducts((int)($cookie->id_lang), 1, 100); /* 100 products max. */
 $sizeOfManufacturerProducts = (int)sizeof($manufacturerProducts);
 $middlePosition = 0;

 // Remove current product from the list
 if (is_array($manufacturerProducts) AND sizeof($manufacturerProducts))
 {
  foreach ($manufacturerProducts AS $key => $manufacturerProduct)
   if ($manufacturerProduct['id_product'] == $idProduct)
   {
 unset($manufacturerProducts[$key]);
 break;
   }
  $taxes = Product::getTaxCalculationMethod();
  if (Configuration::get('PRODUCTSCATEGORY_DISPLAY_PRICE'))
   foreach ($manufacturerProducts AS $key => $manufacturerProduct)
 if ($manufacturerProduct['id_product'] != $idProduct)
 {
  if ($taxes == 0 OR $taxes == 2)
   $manufacturerProducts[$key]['displayed_price'] = Product::getPriceStatic((int)$manufacturerProduct['id_product'], true, NULL, 2);
  elseif ($taxes == 1)
   $manufacturerProducts[$key]['displayed_price'] = Product::getPriceStatic((int)$manufacturerProduct['id_product'], false, NULL, 2);
 }

  // Get positions
  $middlePosition = round($sizeOfManufacturerProducts / 2, 0);
  $productPosition = $this->getCurrentProduct($manufacturerProducts, (int)$idProduct);

  // Flip middle product with current product
  if ($productPosition)
  {
   $tmp = $manufacturerProducts[$middlePosition-1];
   $manufacturerProducts[$middlePosition-1] = $manufacturerProducts[$productPosition];
   $manufacturerProducts[$productPosition] = $tmp;
  }

  // If products tab higher than 30, slice it
  if ($sizeOfManufacturerProducts > 30)
  {
   $manufacturerProducts = array_slice($manufacturerProducts, $middlePosition - 15, 30, true);
   $middlePosition = 15;
  }
 }

 // Display tpl
 $smarty->assign(array(
  'manufacturerProducts' => $manufacturerProducts,
  'middlePosition' => (int)$middlePosition,
  'ProdDisplayPrice' => Configuration::get('PRODUCTSCATEGORY_DISPLAY_PRICE')));
 return $this->display(__FILE__, 'productsmanufacturer.tpl');
}

public function hookHeader($params)
{
 Tools::addCSS($this->_path.'productsmanufacturer.css', 'all');
 Tools::addJS(array($this->_path.'productsmanufacturer.js', _PS_JS_DIR_.'jquery/jquery.serialScroll-1.2.2-min.js'));
}
}

 

 

Merci d'avance pour votre aide !

Link to comment
Share on other sites

  • 3 months later...

Hello a tous,

 

J'ai un petit problème. La manip pour avoir les produits du même fabricant sur la fiche produit fonctionne bien parcontre j'ai un problème de javascript impossible à resoudre. Mes produits ne défilent pas... Quelqu'un a une idee ?

 

Je suis sous prestashop 1.3

Link to comment
Share on other sites

  • 1 month later...

Bonjour,

 

J'ai utilisé avec succès ce code qui fonctionne à merveille sur ma fiche produit sous 1.4.7 ! Merci pour le tuto :)

Dans le cas de mon site, il ne s'agit pas de 'fabricants' mais d''artistes'.

 

J'ai juste un petit souci dans la liste qui s'affiche sous la fiche produit : j'aimerais que cette liste ne remonte pas le produit en cours de consultation.

 

Pourriez-vous m'aider pour créer cette exception dans mon script 'productsmanufacturer.php' ? Je n'ose pas trop toucher au code...

 

Vous remerciant par avance,

Link to comment
Share on other sites

Normalement c'est déjà le cas, regarde cette partie du code :

 

 

 

// Remove current product from the list

if (is_array($manufacturerProducts) AND sizeof($manufacturerProducts))

{

foreach ($manufacturerProducts AS $key => $manufacturerProduct)

if ($manufacturerProduct['id_product'] == $idProduct)

{

unset($manufacturerProducts[$key]);

break;

 

}

Link to comment
Share on other sites

Bonjour,

 

Merci pour le conseil, en fait j'avais une autre version du code je pense sans ces lignes et je n'ai pas comparé avec ce qui était au-dessus. Du coup je les ai rajoutées, et ça fonctionne !

 

Merci encore et bonne journée :)

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...