Page 1 of 1

[SOLVED] Get product id knowing its name?

Posted: Thu Apr 11, 2013 1:13 pm
by noosphere
Hello everybody...
just facing a new challenge...I can't solve :(
I'd like to get the ID of a product (CG Products module) but I know only the name.

Do you know how to do it ?
Thank you in advance !

Have a nice day ;-)

Re: How to: Get product id knowing its name?

Posted: Tue Apr 16, 2013 8:37 pm
by Dr.CSS
Isn't it the first thing on the left next to the products name..?

Re: How to: Get product id knowing its name?

Posted: Wed Apr 17, 2013 7:18 am
by noosphere
Dr.CSS wrote:Isn't it the first thing on the left next to the products name..?
In the admin yes....it's that easy ! ;-)

I meant in my template ?

Re: How to: Get product id knowing its name?

Posted: Thu Apr 25, 2013 10:13 pm
by Dr.CSS
{entry->id} ..?

Re: How to: Get product id knowing its name?

Posted: Fri Apr 26, 2013 9:56 am
by noosphere
I'd like to get the ID of a product (CG Products module) knowing only the name.

Do you know how to do it ?
Thank you in advance !

Have a nice day ;-)

Re: How to: Get product id knowing its name?

Posted: Fri Apr 26, 2013 2:09 pm
by Wishbone
OK.. I'll bite.

Create a UDT called "get_product_id" containing:

Code: Select all

$name = $params['name'];
$assign = $params['assign'];
$product_id = '';

$db = cmsms()->GetDb();
$query = 'SELECT id FROM '.cms_db_prefix().'module_products WHERE product_name= ?';
$row = $db->GetRow($query,array($name));

if(!is_array($product)) {
  $product_id = $row['id'];
}

$smarty->assign($assign, $product_id);
Usage:

Code: Select all

{get_product_id name='Mr. Potato Head' assign='product_id'}

The ID is: {$product_id}

Re: How to: Get product id knowing its name?

Posted: Fri Apr 26, 2013 6:53 pm
by noosphere
Oh Wishbone !
That's it , thank you so much ;-)

And it's simple indeed...I really have to go deeper into UDT and db queries !!!

SOLVED

Re: [SOLVED] Get product id knowing its name?

Posted: Fri Apr 26, 2013 9:12 pm
by Wishbone
Small typo. Change to:

if(is_array($row)) {
$product_id = $row['id'];
}

Note that I removed the ! from isarray

Re: [SOLVED] Get product id knowing its name?

Posted: Mon Apr 29, 2013 9:35 am
by noosphere
yes !
the procedure was correct...now the code is correct and working ;-)
thank you !!!