Jump to content

Afficher/récupérer déclinaisons dans category.tpl


Recommended Posts

Bonjour à toutes et à tous,

 

 

j'ai un petit soucis sur la récupération des déclinaisons d'un produit mais à partir du template category car je vous explique...

Dans category.tpl, je fais appel à produt_list.tpl et tous les produits des sous-catégories sont affichés dans le shop et lors du survol de la souris sur un produit, j'affiche le titre et description du produit et j'aimerais affiché un léger bandeau sur le bas de l'image du produit avec les couleurs (déclinaisons) possible...

Link to comment
Share on other sites

Si ça peut aider quelqu'un,

 

Voici le code à rajouter pour récupérer les données de déclinaisons d'un produit à partir de la page category

 

1. J'ai overridé le contrôleur de le classe CategoryController.php

 

Voici le code: 

<?php
    /*
    * 2007-2013 PrestaShop
    *
    * NOTICE OF LICENSE
    *
    * This source file is subject to the Open Software License (OSL 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/osl-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-2013 PrestaShop SA
    *  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
    *  International Registered Trademark & Property of PrestaShop SA
    */

    class CategoryController extends CategoryControllerCore
    {
        public function initContent()
        {
            parent::initContent();

            $this->setTemplate(_PS_THEME_DIR_.'category.tpl');

            if (!$this->customer_access)
                return;

            if (isset($this->context->cookie->id_compare))
                $this->context->smarty->assign('compareProducts', CompareProduct::getCompareProducts((int)$this->context->cookie->id_compare));

            if ($subCategories = $this->category->getSubCategories($this->context->language->id))
            {
                $subProducts = array();
                foreach($subCategories as $subcategory)
                {
                    $tmp_cat_obj = new Category($subcategory['id_category'], self::$cookie->id_lang);

                    $prods = $tmp_cat_obj->getProducts(intval(self::$cookie->id_lang), (int)($this->p), (int)($this->n), $this->orderBy, $this->orderWay);

                    $subProducts[$subcategory['id_category']] = $this->getAttributesCombinationsForProducts($prods);
                }
                self::$smarty->assign('subProducts', $subProducts);
            }

           

            $this->productSort(); // Product sort must be called before assignProductList()

            $this->assignScenes();
            $this->assignSubcategories();
            if (!in_array($this->category->id, array(Configuration::get('PS_HOME_CATEGORY'), Configuration::get('PS_ROOT_CATEGORY'))))
                $this->assignProductList();

            $this->context->smarty->assign(array(
                'category' => $this->category,
                'description_short' => Tools::truncateString($this->category->description),
                'products' => (isset($this->cat_products) && $this->cat_products) ? $this->cat_products : null,
                'id_category' => (int)$this->category->id,
                'id_category_parent' => (int)$this->category->id_parent,
                'return_category_name' => Tools::safeOutput($this->category->name),
                'path' => Tools::getPath($this->category->id),
                'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
                'categorySize' => Image::getSize(ImageType::getFormatedName('category')),
                'mediumSize' => Image::getSize(ImageType::getFormatedName('medium')),
                'thumbSceneSize' => Image::getSize(ImageType::getFormatedName('m_scene')),
                'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
                'allow_oosp' => (int)Configuration::get('PS_ORDER_OUT_OF_STOCK'),
                'comparator_max_item' => (int)Configuration::get('PS_COMPARATOR_MAX_ITEM'),
                'suppliers' => Supplier::getSuppliers()
            ));
        }

        public function getAttributesCombinationsForProducts  ($prods = null)
        {
            if(!empty($prods)){
                foreach($prods as &$prod){
                    $oProd = new Product($prod['id_product']);
                    $attrs = $oProd->getAttributeCombinations(Context::getContext()->language->id);

                    $new_attrs=array();



                    if(!empty($attrs)){
                        foreach($attrs as $attr){

                                                    
                            if($attr['id_attribute_group']==2){ //Si attribut couleur , on prend

                                if(!isset($new_attrs[$attr['id_attribute']])){   
                                    
                                    //Charge la couleur de l'attribut
                                    $oAttr = new Attribute($attr['id_attribute']);
                                    $attr['color']  = $oAttr->color;

                                    $new_attrs[$attr['id_attribute']] = $attr;
                                }

                            }       
                        }
                    }

                    $prod['attrs'] = $new_attrs;
                } 
            }


            return ($prods);
        }
    }

getAttributesCombinationsForProducts , la fonction rajoutée avec le code pour récupérer les déclinaisons sur l'id de la déclinaison couleur.

 

Voilà voilà  :) Y a peut être mieux à faire mais voilà une solution qui marche.
ps: si vous avez besoin d'info pour l'utilisation des données des déclinaisons avec Smarty, n'hésitez pas  ;)

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

  • 1 year later...

Bonjour,

 

J'aimerais pouvoir afficher chaque déclinaison de couleur d'un produit comme un produit à part entière dans product-list.tpl, cependant je n'arrive pas à récupérer les attributs couleur dans le fichier product-list (j'aimerais faire une boucle sur chaque attribut couleur plutôt que sur chaque produit dans product-list) , peut-être que votre code si dessus pourrait m'aider mais je ne saurais comment l'utiliser. Pourriez-vous m'aider ?

 

Bien à vous,

 

Théophane Diaz

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