Jump to content

[RESOLU] Récupération de l'id category dans l'url d'une fiche produit


Recommended Posts

Bonjour à tous,


J'ai tenté de tripatouiller le fichier link.php pour obtenir l'id de category dans l'url de mes fiches produits mais sans succès.

Je souhaiterais obtenir une url du type http://xxx/product.php?id_product=1&id_category=4'>http://xxx/product.php?id_product=1&id_category=4 au lieu de http://xxx/product.php?id_product=1

Le but est d'avoir les boutons de catégories de mon menu en current lorsque le visiteur est sur un produit.

Je vous remercie d'avance de votre aide.

Cordialement.

Link to comment
Share on other sites

Hi,

pour récupérer l'id catégorie d'un produit, je fais comme ça :

$product = new Product(intval($_GET['id_product']));
if (isset($product) AND Validate::isLoadedObject($product))
   $ActiveCategory = intval($product->id_category_default);



et voilà....

Link to comment
Share on other sites

Non, l'url ne contiendra jamais l'id de la catégorie, mais si tu regardes le code, l'id du produit te permet de récupérer l'id catégorie par défaut de ce produit sans que cela soit passé en url.

Link to comment
Share on other sites

J'imagine que je dois triturer le code sur product.php au niveau de :

$product = new Product(intval($_GET['id_product']), true, intval($cookie->id_lang));
   if (!Validate::isLoadedObject($product) OR !$product->active)
       $errors[] = Tools::displayError('product is no longer available');
   elseif (!$product->checkAccess(intval($cookie->id_customer)))
       $errors[] = Tools::displayError('you do not have access to this product');
   else
   {



Je vais tester.

Merci encore BVince

Link to comment
Share on other sites

La portion de code que je t'ai fourni te permet d'obtenir dans $ActiveCategory la catégorie du produit en cours, donc c'est ton module menu que tu dois modifier pour affecter la classe current selon ton besoin à l'aide de la variable $ActiveCategory. Et ne baisse pas les bras en 3 secondes et demi en mettant "ça marche pas", "Si vous donnez un poisson à un pauvre, il mangera un jour; mais si vous lui apprenez à pêcher, il mangera tous les jours"

Link to comment
Share on other sites

Re BVince,

Ton proverbe est bien approprié mais je dois fournir ce menu rapidement à un client qui croit que je suis un cador en prestashop, or ce n'est pas le cas. Celà dit, je trouve Prestashop vraiment super.

Le menu que j'utilise est le module categoriesbar. il y a un fichier tpl et un php. Le fichier php semblent envoyer les variables dans le tpl et là, je ne comprend rien.

Ton code doit certainement s'insérer à cet endroit (.tpl)

//construct categories
       $depth = 0;
       $rootCateg = Category::getRootCategory()->recurseLiteCategTree($depth);
       if (isset($_GET['id_category']))
       $ActiveCategory = intval($product->id_category_default);
       $smarty->assign('currentCategoryId', intval($_GET['id_category']));
       $smarty->assign('bar_tpl_path', _PS_MODULE_DIR_.'categoriesbar/category-bar-level.tpl');
       $smarty->assign('categoriesLevel', $rootCateg);

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



et pour envoyer ses variables ici (.php) :

{foreach from=$categoriesLevel.children item=child name=categoriesLevel}
       {if $smarty.foreach.categoriesLevel.last}
           {include file=$bar_tpl_path node=$child last='true'}
       {else}
           {include file=$bar_tpl_path node=$child}
       {/if}
   {/foreach}



Je ne pige absolument pas la logique !!

Si tu pouvais au moins me mettre sur la piste...
Merci encore pour tout ce que tu fais

EDIT : le premier bloc de code est erroné, il comporte le tien :

       $depth = 0;
       $rootCateg = Category::getRootCategory()->recurseLiteCategTree($depth);

       if (isset($_GET['id_category']))
       $smarty->assign('currentCategoryId', intval($_GET['id_category']));
       $smarty->assign('bar_tpl_path', _PS_MODULE_DIR_.'categoriesbar/category-bar-level.tpl');
       $smarty->assign('categoriesLevel', $rootCateg);

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

Link to comment
Share on other sites

Je ferais comme ça :

        $depth = 0;
       $rootCateg = Category::getRootCategory()->recurseLiteCategTree($depth);
       $ActiveCategory = "";
       if (isset($_GET['id_category']))
               $ActiveCategory = intval($_GET['id_category']);
       if (isset($_GET['id_product'])) {
               $product = new Product(intval($_GET['id_product']));
           if (isset($product) AND Validate::isLoadedObject($product))
               $ActiveCategory = intval($product->id_category_default);
       }
       $smarty->assign('currentCategoryId', $ActiveCategory);
       $smarty->assign('bar_tpl_path', _PS_MODULE_DIR_.'categoriesbar/category-bar-level.tpl');
       $smarty->assign('categoriesLevel', $rootCateg);

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

Link to comment
Share on other sites

Ca me retourne une page blanche mais c'est de ma faute, je n'ai pas communiqué la fin du code et du coup il y a deux return.

La fin du code du .php :

       /**
   * Returns module content for header
   *
   * @param array $params Parameters
   * @return string Content
   */
   function hookTop($params)
   {
       global $smarty;

       //construct categories
       $depth = 0;
       $rootCateg = Category::getRootCategory()->recurseLiteCategTree($depth);

       if (isset($_GET['id_category']))
       $smarty->assign('currentCategoryId', intval($_GET['id_category']));
       $smarty->assign('bar_tpl_path', _PS_MODULE_DIR_.'categoriesbar/category-bar-level.tpl');
       $smarty->assign('categoriesLevel', $rootCateg);

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

   function hookNavbar($params)
   {
       return $this->hookTop($params);
   }

}

?>



EDIT : que ton nom soit loué à travers les âges BVince !

Ca marche !!!

Mille mercis pour ton aide !!

Link to comment
Share on other sites

/**
   * Returns module content for header
   *
   * @param array $params Parameters
   * @return string Content
   */
   function hookTop($params)
   {
       global $smarty;

       //construct categories
       $depth = 0;
       $rootCateg = Category::getRootCategory()->recurseLiteCategTree($depth);

       $ActiveCategory = "";
       if (isset($_GET['id_category']))
               $ActiveCategory = intval($_GET['id_category']);
       if (isset($_GET['id_product'])) {
           $product = new Product(intval($_GET['id_product']));
               if (isset($product) AND Validate::isLoadedObject($product))
                   $ActiveCategory = intval($product->id_category_default);
       }
       if($ActiveCategory!="")
           $smarty->assign('currentCategoryId', $ActiveCategory);

       $smarty->assign('bar_tpl_path', _PS_MODULE_DIR_.'categoriesbar/category-bar-level.tpl');
       $smarty->assign('categoriesLevel', $rootCateg);

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

   function hookNavbar($params)
   {
       return $this->hookTop($params);
   }

}

?>

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