Tags/topics: 
2
Answers
Vote up!
0
Vote down!

Fetch products created by acting user

Hello,

I need to get products created by currently acting user and display them in a drop-down list. The code below basically works, however it displays all products created by all users. Any clues please whats wrong with my code and how to limit the list?

global $user;
$uid = $user->uid;

$products = db_query("SELECT * FROM {commerce_product} WHERE status=1 AND uid=uid");

$products_array = array();

foreach ($products as $product) {
$products_array[$product->product_id] = $product->title;
}

$form['products'] = array(
'#type' => 'select',
'#title' => t('Select Product'),
'#options' => $products_array,
);

Asked by: sketman
on May 30, 2016

2 Answers

Vote up!
0
Vote down!

I think there is a mistake in db_query. Try with this:
$products = db_query("SELECT * FROM {commerce_product} WHERE status=1 AND uid=:id",array(':id' => $uid));

Answer by: Jorge SB
Posted: Jun 2, 2016
Vote up!
0
Vote down!

That made it!
Thank you Jorge, I appreciate your help.

Answer by: sketman
Posted: Jun 2, 2016