1

I want to echo an attribute only if the specific product is in an category.
I use this code on the productpage.
So I need a line that checks if the product is inside a category and if so, that do this.
I tried this, but that did not work:

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

if (count(array_intersect($yourCatIds,$productCats))) {
    <?php echo($_product->getData('attributecode')) : ?>
}
?>

EDIT:

<?php $yourCatIds = array(235);
$productCats = $_product->getAvailableInCategories();

if (count(array_intersect($yourCatIds,$productCats))) {
    echo '<li class="test even">' . $_product->getData('i_processor_family_2196_1') . '</li>';
echo '<li class="test even">' . $_product->getAttributeText('i_processor_family_2196_1') . '</li>';
}
?>

How can I change this code by adding <tr><td> and <th> tags?

What code do I need for this?

  • what part does not work. I have tested this on my setup if the product intersects the categories then it displays the attribute. Though you do seem to have a typo and have a : after the get data – David Manners Jan 25 '15 at 12:51
  • Well I get an error. I changed the type : to a ; but that still does not work. And the last ?> does not close correctly, but I can't find the problem. – Henk Z Jan 25 '15 at 13:32
  • is that 100% your code cause the the cat array can not have the ... at the end but I assume you are just shortening it for stackexchange. – David Manners Jan 25 '15 at 13:34
  • actually also you have the <?php echo inside but you do not need that. – David Manners Jan 25 '15 at 13:35
1

From your code it appears you have extra php tags you do not need inside the if statement.

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

if (count(array_intersect($yourCatIds,$productCats))) {
    echo '<li>' . $_product->getData('attributecode') . '</li>';
}
?>
  • Works great! :) But how can I add an <li> class to the attribute code? – Henk Z Jan 25 '15 at 18:40
  • @Jelle nice and simple one that is. I have updated my answer to include the wrapping. As long as the data is a string that will work as you want it. – David Manners Jan 25 '15 at 18:45
  • Many thanks! :) But when I add my attribute code it displays something else than the data for that product. The real data should be Intel Core i5, but it says 337. Do you why it get this data? – Henk Z Jan 25 '15 at 18:50
  • @Jelle is it a select item? – David Manners Jan 25 '15 at 18:50
  • @Jelle try getAttributeText instead of getdata – David Manners Jan 25 '15 at 18:55
0

Only issue with function getAvailableInCategories is wrong.It should be getCategoryIds

  • It seems that getAvailableInCategories is useless for child products. @Amit Bera what do you think about using getCategoryIds instead of getAvailableInCategories in terms of performances ? – eInyzant Jan 23 '18 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.