2015-09-20 16 views
0

我一直有这个麻烦几个小时,它似乎应该是如此简单。Magento获得类别ID,但从外部主题文件(扩展名)

我正在做我的第一个扩展,并希望如果一个产品是它的某个类别的一部分。它在view.phtml中工作正常,但是当我尝试从我的扩展中加载它时,它不起作用。

本来我一直在使用:

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

但我知道$ _产品扩展内不会工作,而我需要得到的模型。我曾尝试:

<?php $yourCatIds = array(1,3,5,6); 
 
    $category = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); 
 
    $productCats = Mage::getModel('catalog/category')->load($cat_id); 
 

 

 

 
\t if (count(array_intersect($yourCatIds,$productCats))) {} 
 
\t \t \t \t \t \t else { 
 
\t \t \t \t \t \t } 
 
\t \t \t \t ?>
人和:
$product = Mage::getModel('catalog/product')->load($_item['product_id']); 
 
$cats = $product->getCategoryIds();

和其他一些我看到的计算器,但似乎没有奏效。我没有得到一个错误,就像我在phtml文件中做的一样。

有谁能告诉我我在哪儿傻吗?

UPDATE:这是作为尽我所能,但还是它不工作 它打印“不工作” :(

<?php 
 
    
 
class Battery_Function_Helper_Data extends Mage_Core_Helper_Abstract 
 
{ 
 
    
 
\t public function test(){ 
 
    
 
\t $yourCatIds = array(1,2,3,5,6,24); 
 
\t $productCats = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); 
 
\t \t \t \t \t \t if (count(array_intersect($yourCatIds,$productCats))) { 
 
    \t \t \t \t \t \t \t 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>'; 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t \t else { 
 
\t \t \t \t \t \t print("not working "); 
 
\t \t \t \t \t \t } \t 
 
\t 
 

 
\t } 
 
    
 
} 
 

 
?>

回答

0

您可以通过一步测试数据的步骤:

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

您可以使用dump($productCats),看有没有一些数据,如果是NULL,你应该检查的代码。如果有是一些数据,然后进行下一步,显示数据就像$productCats

+0

我改变了$ cat_id,因为我注意到这个错误。我已经使用了dump($ productCats),并且我获得了很多信息,但没有指向类别ID。我也甩了$ category给我一个字符串(1) - 2,但我已经检查,类别是3而不是2,所以我输了。 var dump太大而无法粘贴,因此不知道该怎么做:/ –

+0

任何人都可以帮忙吗? –