Accessing productID for when viewing a product's web page

Permalink
Hi,

When viewing a product's web page, is there any way to access the associated productID?

I am developing functionality that occurs when viewing a product.
I need to access the associated productID for the currently viewed web page.

I managed to get it working very dodgily by searching a productlist for the current web page name- but as the product title has since changed, and the associated page name has not..obviously this is now broken and was never going to be a proper solution...

$page = Page::getCurrentPage();      
$pageName = $page->vObj->cvName;
$pl = new CoreCommerceProductList();//'filterBy', 
$pl->filterByKeywords($pageName);
$matches = $pl->get();
foreach($matches as $match){
   $productName = strtolower(str_replace('-',' ',$match->prName));
   $pageName = strtolower($pageName);
   if($productName==$pageName){
      $productID = $match->productID;
   }
}

 
katiam replied on at Permalink Reply
Ok so I figured it out afterall, if anyone has the same problem, to find the productID of the page being currently viewed try:

Loader::model('product/model', 'core_commerce');
$page = Page::getCurrentPage();
//get the current pages' cID
$cID = $page->getCollectionID();
//load the product with the cID of the current page
$productObj = new CoreCommerceProduct();
$ar = new ADODB_Active_Record('CoreCommerceProducts');
$ar->Load('cID=?', array($cID));
$productID = $ar->productID;