Jump to content

Get product by shop_id


tovirio

Recommended Posts

Yes, i want to use the function get the product , in my rss.php file

Product::getProducts()

The problem is that this function doesn't take in input the parameter id_shop. I have the option multistore on, so i have two shop with different ID. Is possible to use this function or another one to get the product of a determinate shop?

Link to comment
Share on other sites

Maybe I explained myself wrong.

I have two shop, and with my rss file inside my module i want a function that returns all the products of one shop.

For example i have to path:
http://localhost/prestashop/       for shop one
http://localhost/prestashop2/     for shop two

 

I want that the rss file called in the path:
http://localhost/prestashop/modules/mymodule/rss.php      (the path of first shop)

return me the products of the second shop and not of the first shop.
I think that i have to force the variable $context to get the product of the second shop in the path of the first.

Can you explain me a way to do it?
Thanks

 

 

Link to comment
Share on other sites

I assume these shops are not part of a multi-store configuration, and therefore these shops do not share a database and they do not know anything about each other.

 

If that is the case, then you need to write code so that rss feed would connect to the other stores database and obtain the products and combine them with products from its own store

Link to comment
Share on other sites

Maybe I explained myself wrong.

I have two shop, and with my rss file inside my module i want a function that returns all the products of one shop.

For example i have to path:

http://localhost/prestashop/       for shop one

http://localhost/prestashop2/     for shop two

 

I want that the rss file called in the path:

http://localhost/prestashop/modules/mymodule/rss.php      (the path of first shop)

return me the products of the second shop and not of the first shop.

I think that i have to force the variable $context to get the product of the second shop in the path of the first.

Can you explain me a way to do it?

Thanks

 

Yes I know what you want to achieve, and these method get all the products including the filter by id_shop, or do you want have the possibility to manage the id_shop?

 

For example, now works this way...

shop.com/one => Product::getProducts() => get all the products from the shop one

shop.com/two => Product::getProducts() => get all the products from the shop two

 

Do you want to achieve something as this example?

shop.com/one => Product::getProducts($id_shop = 2) => get all the products from the shop two

Link to comment
Share on other sites

Ok, you could add a new method in the class, something like this....

/**
* Get all available products by id shop
*
* @param integer $id_shop Shop id
* @param integer $id_lang Language id
* @param integer $start Start number
* @param integer $limit Number of products to return
* @param string $order_by Field for ordering
* @param string $order_way Way for ordering (ASC or DESC)
* @return array Products details
*/
public static function getProductsByIdShop($id_shop, $id_lang, $start, $limit, $order_by, $order_way, $id_category = false,
	$only_active = false, Context $context = null)
{
	if (!$context)
		$context = Context::getContext();

	$front = true;
	if (!in_array($context->controller->controller_type, array('front', 'modulefront')))
		$front = false;

	if (!Validate::isOrderBy($order_by) || !Validate::isOrderWay($order_way))
		die (Tools::displayError());
	if ($order_by == 'id_product' || $order_by == 'price' || $order_by == 'date_add' || $order_by == 'date_upd')
		$order_by_prefix = 'p';
	else if ($order_by == 'name')
		$order_by_prefix = 'pl';
	else if ($order_by == 'position')
		$order_by_prefix = 'c';

	if (strpos($order_by, '.') > 0)
	{
		$order_by = explode('.', $order_by);
		$order_by_prefix = $order_by[0];
		$order_by = $order_by[1];
	}
	$sql = 'SELECT p.*, product_shop.*, pl.* , m.`name` AS manufacturer_name, s.`name` AS supplier_name
			FROM `'._DB_PREFIX_.'product` p
			INNER JOIN `'._DB_PREFIX_.'product_shop` product_shop ON (product_shop.`id_product` = p.`id_product` AND product_shop.`id_shop` = '.(int)$id_shop.')
			LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_shop` = '.(int)$id_shop.')
			LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`)
			LEFT JOIN `'._DB_PREFIX_.'supplier` s ON (s.`id_supplier` = p.`id_supplier`)'.
			($id_category ? 'LEFT JOIN `'._DB_PREFIX_.'category_product` c ON (c.`id_product` = p.`id_product`)' : '').'
			WHERE pl.`id_lang` = '.(int)$id_lang.
				($id_category ? ' AND c.`id_category` = '.(int)$id_category : '').
				($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '').
				($only_active ? ' AND product_shop.`active` = 1' : '').'
			ORDER BY '.(isset($order_by_prefix) ? pSQL($order_by_prefix).'.' : '').'`'.pSQL($order_by).'` '.pSQL($order_way).
			($limit > 0 ? ' LIMIT '.(int)$start.','.(int)$limit : '');
	$rq = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
	if ($order_by == 'price')
		Tools::orderbyPrice($rq, $order_way);

	foreach ($rq as &$row)
		$row = Product::getTaxesInformations($row);

	return ($rq);
}

Call the method with: 

Product::getProductsByIdShop()

Obviously now you must add the ID shop in the first parameter.

  • Like 3
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...