michael-marinetti

ProductLink with color attribute in url-friendly

Aug 31st, 2011
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.56 KB | None | 0 0
  1. <?php
  2. // Description :  With this modifications, the friendly url link will contains
  3. // the color name if available
  4. // using Prestashop > 1.4.4
  5. class Link extends LinkCore{
  6.    
  7.  
  8.     /**
  9.       * Return the correct link for product, with the color name if there
  10.       * is a color attribute available
  11.       *
  12.       * @param mixed $id_product Can be either the object or the ID only
  13.       * @param string $alias Friendly URL (only if $id_OBJ is the object)
  14.       * @return string link
  15.       *
  16.       */
  17.     public function getProductLink($id_product, $alias = NULL, $category = NULL, $ean13 = NULL, $id_lang = NULL)
  18.     {
  19.         $id_lang = (int)$id_lang;
  20.         if (!is_object($id_product))
  21.             $id_product = new Product($id_product);
  22.        
  23.         $attributs = $id_product->getAttributeCombinaisons($id_lang);
  24.         // adding color attribute, first part
  25.         foreach($attributs AS $k => $attribut)
  26.             if($attributs[$k]['is_color_group'])
  27.             {
  28.                 $color = $attribut['attribute_name'];
  29.                 break;
  30.             }
  31.         // if link_rewrite
  32.         if ($this->allow == 1)
  33.         {
  34.             $url = _PS_BASE_URL_.__PS_BASE_URI__.$this->getLangLink($id_lang) ;
  35.             if (isset($id_product->category) AND !empty($id_product->category) AND $id_product->category != 'home')
  36.                 $url .= $id_product->category.'/';
  37.             $url .= $id_product->id.'-'.$id_product->link_rewrite;
  38.             // adding color attribute, part II
  39.             if (isset($color))
  40.                 $url .= '-'.$color;
  41.             $url .= $id_product->ean13 ? '-'.$id_product->ean13 : '';
  42.             $url .= '.html';
  43.         }
  44.         else
  45.             $url = _PS_BASE_URL_.__PS_BASE_URI__.'product.php?id_product='.(int)$id_product->id;
  46.  
  47.         return $url;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment