northon Posted October 26, 2014 Share Posted October 26, 2014 Hi, Im working on little modification of pele related products for product page. By default, it shows products which shares the same tag as opened product. I can easily get link, name and image foreach of the product. But i would like to display ID of default category for every product in the list. $category->name; gives me only the category name of opened product as you can see here: http://liftoff.cc/beta/cs/summer-dresses/1-faded-short-sleeve-t-shirts-3.html http://liftoff.cc/beta/cs/blouses/2-blouse.html I tried to get the data by adding this: $id_category = intval(Tools::getValue('id_category')) but without any results. Am I doing something wrong? The Code: public function hookProductTabContent($params) { // final array $pele_related_products = array(); // grab the product id $id_product = intval(Tools::getValue('id_product')); $id_category = intval(Tools::getValue('id_category')); $prod= new Product($id_product); $number_of_products=strlen($prod->pele_related_limit_number_products)==0?-1:$prod->pele_related_limit_number_products; // grab tags from the product $product_tags = Tag::getProductTags($id_product); $the_lang_for_tag=$params['cookie']->id_lang; if(!isset($product_tags[$params['cookie']->id_lang])){//if the tag is not in the selected language let's use the default $the_lang_for_tag=(int)Configuration::get('PS_LANG_DEFAULT'); } // loop through the tags and see what we can find. if(is_array($product_tags[$the_lang_for_tag])&& $number_of_products!=0 ) { $context = Context::getContext(); foreach($product_tags[$the_lang_for_tag] as $tag_key=>$tag_value) { // new tag $tag_generic = new Tag('', $tag_value, intval($the_lang_for_tag)); // what other products match this tag... hand them over! $other_products = $this->pele_getProductsOrderByWeight($tag_generic->id,$params['cookie']->id_lang,$tag_key+1); // grab products associated with this tag oredered by weight desc // what other product match this category... hand them over! // $other_products = $category->getProducts($this->context->language->id, 1, 100); foreach($other_products as $other_key=>$other_value) { // make sure theres no duplicate products if($other_value['id_product'] != $id_product && !array_key_exists($other_value['id_product'], $pele_related_products)) { // get category of each product NOT category of opened product like now!! $category = false; if (isset($params['category']->id_category)) $category = $params['category']; else { if (isset($product->id_category_default) && $product->id_category_default > 1) $category = new Category((int)$product->id_category_default); } if (!Validate::isLoadedObject($category) || !$category->active) return false; // grab product link $other_value['link'] = $context->link->getProductLink($other_value['id_product']); $other_value['displayed_price'] = Product::getPriceStatic($other_value['id_product'],true); $other_value['cat'] = $category->name; // grab product image - request by user. $other_value['image'] = Image::getCover($other_value['id_product']); if(is_array($other_value['image'])) { $other_value['image']['id_image'] = $other_value['id_product'].'-'.$other_value['image']['id_image']; $other_value['image']['link_rewrite'] = $this->pele_LinkRewrite($other_value['id_product'], $params['cookie']->id_lang); } // add the product to the array $pele_related_products[$other_value['id_product']] = $other_value; } } } } // Check input for random order doesent work in PS 1.6. Hide this sentence to set random order as default // if($prod->pele_related_random_order){ shuffle($pele_related_products); // } if($number_of_products>0){ //slice the array $pele_related_products = array_slice($pele_related_products, 0, $number_of_products); } $this->smarty->assign('peleRelatedProducts', $pele_related_products); if($this->psversion()==6){ return $this->display(__FILE__, 'pele_relatedproducts_16.tpl'); }else{ return $this->display(__FILE__, 'pele_relatedproducts.tpl'); } } public function hookProductFooter($params) { return $this->hookProductTabContent($params); } /** * pele_LinkRewrite * Retreive the rewrite for the given product * * @param $id_product integer * @param $id_lang integer * @return $rewrite string */ public function pele_LinkRewrite($id_product, $id_lang) { $result = Db::getInstance()->ExecuteS('SELECT `link_rewrite` FROM `'._DB_PREFIX_.'product_lang` WHERE `id_product`='.intval($id_product).' AND `id_lang` = '.intval($id_lang)); return $result[0]['link_rewrite']; } public function pele_getProductsOrderByWeight( $id_tag, $id_lang, $index_tag) { return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT pl.name, pl.id_product, REPLACE(SUBSTRING(SUBSTRING_INDEX(p.pele_related_tag_weight, ",", '.$index_tag.'), LENGTH(SUBSTRING_INDEX(p.pele_related_tag_weight, ",", '.$index_tag.' -1)) + 1), ",", "") as weight FROM `'._DB_PREFIX_.'product` p LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON p.id_product = pl.id_product'.Shop::addSqlRestrictionOnLang('pl').' '.Shop::addSqlAssociation('product', 'p').' WHERE pl.id_lang = '.(int)$id_lang.' AND product_shop.active = 1 '.('AND p.id_product IN (SELECT pt.id_product FROM `'._DB_PREFIX_.'product_tag` pt WHERE pt.id_tag = '.(int)$id_tag.')') .' ORDER BY weight DESC, pl.name ASC'); } public function psversion() { $version=_PS_VERSION_; $exp=$explode=explode(".",$version); return $exp[1]; } public function hookHeader($params) { if (!isset($this->context->controller->php_self) || $this->context->controller->php_self != 'product') return; $this->context->controller->addCSS($this->_path.'css/pele_relatedproducts16.css', 'all'); $this->context->controller->addJqueryPlugin(array('scrollTo', 'serialScroll', 'bxslider')); } } Thanks!! I appreciate all help.David Link to comment Share on other sites More sharing options...
musicmaster Posted October 27, 2014 Share Posted October 27, 2014 Why don't you use "$product->id_category_default" as is done in the function? Link to comment Share on other sites More sharing options...
northon Posted October 27, 2014 Author Share Posted October 27, 2014 Okay and where shall i put it? I tried to put "$product->id_category_default" here so far and its not working: foreach($other_products as $other_key=>$other_value) {$other_value['cat'] = $product->id_category_default // before: $category->name;} This gives me php notice "trying to get propery of non-object and undefined variable on line 317 (content of line 317: $other_value['cat'] = $id_category; //this is value for .tpl file) // grab the product id $id_product = intval(Tools::getValue('id_product')); $id_category = intval(Tools::getValue('id_category_default')); This gives me no php notice and number 0 instead of correct id. Here you can see the result. Link to comment Share on other sites More sharing options...
musicmaster Posted October 27, 2014 Share Posted October 27, 2014 If you want to make modifications you should be capable of experimenting with the code yourself. Maybe you should use $prod, maybe you should create a product object first. It is for you to find out! Link to comment Share on other sites More sharing options...
Recommended Posts