Jump to content

[SOLVED] Get featured products from two categories


yay

Recommended Posts

I have set up a modified version of the "Featured products on homepage" module which instead of the default Home category, gets products from a custom category. The reason for this is I need multiple copies of the module on my homepage so I can show multiple blocks of featured products from different categories.

 

Homefeatured.php, where number 11 on line 121 is the custom category ID:

	public function hookDisplayHome($params)
	{
		if (!$this->isCached('homefeatured.tpl', $this->getCacheId('homefeatured')))
		{
			$category = new Category(11, (int)Context::getContext()->language->id);
			$categoryLink = $category->id; 
			$categoryName = $category->name; 
			$nb = (int)Configuration::get('HOME_FEATURED_NBR');
			$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), "position");

			$this->smarty->assign(array(
				'products' => $products,
				'categoryLink' => $categoryLink,
				'categoryName' => $categoryName,
				'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
				'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
			));
		}
		return $this->display(__FILE__, 'homefeatured.tpl', $this->getCacheId('homefeatured'));
	}

So far so good, but this means I haven't got any real control over which products from category 11 will show up on the homepage. So I'm wondering if it's possible to only get products which also has the Home (ID number 2) category selected as well?

 

To summarize: Product X is associated with categories 11 and 2 and therefore show up on the homepage. Product Y is only associated with category 11 and does not show up on the front page.

 

Ideas very much appreciated!

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

Maybe override Category.php and add a function similar to getProducts:

 

public function getProductsLimited($limitingCategoryID = null, $id_lang, $p, $n, $order_by = null, $order_way = null, $get_total = false, $active = true, $random = false, $random_number_products = 1, $check_access = true, Context $context = null)
 
with same contents as getProducts, only in where clause of SQL statement, add:
 
WHERE product_shop.`id_shop` = '.(int)$context->shop->id.'
AND cp.`id_category` = '.(int)$this->id
.($limitingCategoryID ? ' AND EXISTS(SELECT `id_product` FROM `ps_category_product` cpt WHERE `id_category` = '.$limitingCategoryID.' AND cpt.`id_product` = cp.`id_product` )' : '') 
.($active ? ' AND product_shop.`active` = 1' : '')
.($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '')
.($id_supplier ? ' AND p.id_supplier = '.(int)$id_supplier : '')
 
 
Then Change line 125 in your code to:

 

$products = $category->getProductsLimited(2,(int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), "position");

 

Where 2 is the ID of the 'Home' category (Change accordingly if needed)

 

Hope this does the trick,

pascal.

  • Like 2
Link to comment
Share on other sites

  • 1 month later...

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