Magento获得类别ID,但从外部主题文件(扩展名)


Magento get category ID but from outside theme files (extension)

我已经为这个问题困扰了几个小时了,似乎它应该是如此简单。

我正在做我的第一个扩展,并希望如果一个产品是一个类别的一部分,它做的事情。看来工作得很好。但是当我尝试从扩展中加载它时,它不起作用。

原来我一直在用:

<?php $yourCatIds = array(1,3);
$productCats = $_product->getAvailableInCategories();

但我知道$_product不会在扩展内工作,我需要得到模型。我试过了:

<?php $yourCatIds = array(1,3,5,6);
   $category = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
   $productCats = Mage::getModel('catalog/category')->load($cat_id);
	if (count(array_intersect($yourCatIds,$productCats))) {}
						else {
						}
				?>
和:
$product = Mage::getModel('catalog/product')->load($_item['product_id']);
$cats = $product->getCategoryIds();

和其他一些我在stackoverflow上看到的,但似乎没有工作。我没有得到一个错误,只是没有得到相同的,当我做它在php文件。

谁能告诉我我在哪里犯傻?

更新:这是我能得到的,但它不工作它打印"not working":(

<?php
 
class Battery_Function_Helper_Data extends Mage_Core_Helper_Abstract
{
 
	public function test(){
 
	$yourCatIds = array(1,2,3,5,6,24);
	$productCats = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
						if (count(array_intersect($yourCatIds,$productCats))) {
    							echo '<a href="#" style="float:right" title="You can only take 1x 160Wh battery or 2x batteries under 100Wh, in your handluggage ONLY." class="masterTooltip">Traveling on a Plane <img style="display: inline-block" src="http://www.somerset.lib.nj.us/images/help.gif"></a><br>';
						}
						else {
						print("not working ");
						}	
	
	}
 
}
?>

您可以一步一步地测试数据:

$category = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
$productCats = Mage::getModel('catalog/category')->load($cat_id);

可以用dump($productCats),看看有没有数据。如果是NULL,您应该检查代码。如果有数据,那么进入下一步,像$productCats一样显示数据。