Jump to content

Recommended Posts

Bonjour,

 

J'ai un produit qui est dans une catégorie racine et dans une sous-catégorie de cette même catégorie racine.

 

- Lorsque je suis dans la fiche de ce produit à partir de la catégorie racine, le module "PRODUITS DANS LA MEME CATEGORIE" m'affiche une liste de produits appartenant à la catégorie racine. (CAS 1)

 

- Lorsque je suis dans la fiche de ce produit à partir de la sous-catégorie, le module "PRODUITS DANS LA MEME CATEGORIE" m'affiche une liste de produits appartenant à la sous-catégorie. (CAS 2)

 

Ma question est : est-il possible d'afficher une liste de produits appartenant à la sous-catégorie quand on se situe dans le CAS 1 ?

Link to comment
Share on other sites

Bon, après 3h de taff (je vous préviens je suis nul en php), j'ai réussi à faire ce que je voulais mais à mon avis mon code n'est pas du tout optimisé. Je vous le partage quand même

 

Fichier modifié => productscategory.php (version de prestashop 1.6.0.14)

public function hookProductFooter($params)
	{
		$id_product = (int)$params['product']->id;
		$product = $params['product'];
		$productCurrentCat = $params['product']->id_category_default;

		$cache_id = 'productscategory|'.$id_product.'|'.(isset($params['category']->id_category) ? (int)$params['category']->id_category : (int)$product->id_category_default);

		if (!$this->isCached('productscategory.tpl', $this->getCacheId($cache_id)))
		{

			$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;

			// Get infos
			$category_products = $category->getProducts($this->context->language->id, 1, 999); /* 999 products max. */
			$nb_category_products = (int)count($category_products);
			$middle_position = 0;
			
			// Remove current product from the list
			if (is_array($category_products) && count($category_products))
			{
				foreach ($category_products as $key => $category_product)
				{
					//remove current product
					if ($category_product['id_product'] == $id_product)
					{
						unset($category_products[$key]);
					}
					//remove product of others categories
					if($productCurrentCat != $category_product['id_category_default'])
					{
						unset($category_products[$key]);
					}
				}

				$taxes = Product::getTaxCalculationMethod();
				if (Configuration::get('PRODUCTSCATEGORY_DISPLAY_PRICE'))
				{
					foreach ($category_products as $key => $category_product)
					{
						if ($category_product['id_product'] != $id_product)
						{
							if ($taxes == 0 || $taxes == 2)
							{
								$category_products[$key]['displayed_price'] = Product::getPriceStatic(
									(int)$category_product['id_product'],
									true,
									null,
									2
								);
							} elseif ($taxes == 1)
							{
								$category_products[$key]['displayed_price'] = Product::getPriceStatic(
									(int)$category_product['id_product'],
									false,
									null,
									2
								);
							}
						}
					}
				}

				// Get positions
				/*$middle_position = (int)round($nb_category_products / 2, 0);
				$product_position = $this->getCurrentProduct($category_products, (int)$id_product);

				// Flip middle product with current product
				if ($product_position)
				{
					$tmp = $category_products[$middle_position - 1];
					$category_products[$middle_position - 1] = $category_products[$product_position];
					$category_products[$product_position] = $tmp;
				}

				// If products tab higher than 30, slice it
				if ($nb_category_products > 30)
				{
					$category_products = array_slice($category_products, $middle_position - 15, 30, true);
					$middle_position = 15;
				}*/
			}

			// Display tpl
			$this->smarty->assign(
				array(
					'categoryProducts' => $category_products,
					'middlePosition' => (int)$middle_position,
					'ProdDisplayPrice' => Configuration::get('PRODUCTSCATEGORY_DISPLAY_PRICE')
				)
			);
		}

		return $this->display(__FILE__, 'productscategory.tpl', $this->getCacheId($cache_id));
	}
Link to comment
Share on other sites

  • 2 years later...

Je me permets de réagir parce que j'ai eu le même besoin, et j'ai préféré juste changer la catégorie "parente" au lieu de modifier la boucle, je pense que c'est plus logique.

 

Dans le code "original" de productscategory.php ligne 119 j'ai changé cela :

 

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);
}

 

par cela :

 

/*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);
}
*/
         
//Forcer la catégorie par défaut
$category = new Category((int)$product->id_category_default);
            

 

L'ayant moi même cherché, autant partager. Merci Nicowcow pour avoir mis le doigt sur ce qui pêchait !

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