0

I need to show different content on the product pages (view.phtml)

Here is the code I have. The first array with the if statement works, it's getting the else if to work that I am having trouble with

<?php if(in_array($category->getId(), array(62,63,64,65,66,67,68))): ?>

for prods in these categories

<?php elseif(in_array($category->getId(), array(3,55,56,57,58,59,60))): ?>

 for prods in other categories

 <?php else: ?>

 everything else

  <?php endif; ?>
  • What's your actual issue? – Abhishek Panchal Aug 10 '17 at 15:17
  • No problem in this code check your $category->getId(). code work fine. – Qaisar Satti Aug 10 '17 at 15:18
  • This doesn't work. It only shows the first content for the first array, and then everything else shows for everything else. The second one isn't working. – Annie Aug 10 '17 at 15:25
  • What does var_dump($category->getId()); output? In your second array, you have 58,59.60, is that intentional? Or did you need 58,59,60? – nick.graziano Aug 10 '17 at 15:34
  • That was a mistake! I corrected it, but still only the first array works. – Annie Aug 10 '17 at 15:45
0

I think the issue is that you have a . instead of a , in your second array. I think that you are testing it with category number 59 or 60, which will not make the in_array($category->getId(), array(3,55,56,57,58,59.60)) === true.

Try this:

<?php if(in_array($category->getId(), array(62,63,64,65,66,67,68))): ?>

    for prods in these categories

<?php elseif(in_array($category->getId(), array(3,55,56,57,58,59,60))): ?>

    for prods in other categories

<?php else: ?>

    everything else

<?php endif; ?>
| improve this answer | |
  • I changed 59.60 to 59,60, as that was a mistake. But still, only the first array works – Annie Aug 10 '17 at 15:44
0

The issue is product is assigned to multiple categories. you need to load product first and then get category ids of product.

$_product = Mage::getModel('catalog/product')->load($prodId);
$ids = $_product->getCategoryIds();
var_dump($ids); //this is category id

you will get multiple category ids in Array, then you can compare 2 arrays by using array_diff() in your if..else condition.

| improve this answer | |
  • Thank you, but it's still not working. <?php $_product = Mage::getModel('catalog/product')->load(prodId); $ids = $_product->getCategoryIds(); var_dump($ids); ?> <?php if(in_array($category->getId(), array(62,63,64,65,66,67,68))): ?> for prods in these categories <?php elseif(in_array($category->getId(), array(3,55,56,57,58,59,60))): ?> for prods in other categories <?php else: ?> everything else <?php endif; ?> – Annie Aug 10 '17 at 16:18
  • what did you get in Category Ids Array? – Abhishek Panchal Aug 10 '17 at 16:19
  • I don't know what you mean. It is showing nothing – Annie Aug 10 '17 at 16:21
  • it shows this for the first one array(0) { } for prods in these categories – Annie Aug 10 '17 at 16:22
  • did you fetch productId ?? $prodId.. inside the load function. lets chat here. chat.stackexchange.com/rooms/info/63618/… – Abhishek Panchal Aug 10 '17 at 16:23
0

I think you almost got it. @AbhishekPanchal already pointed you the right direction, you just failed implementing it.

Try this instead:

<?php 
$_product = Mage::getModel('catalog/product')->load($prodId); 
$id = $_product->getCategoryId();  
?> 
<?php if(in_array($id, array(62,63,64,65,66,67,68))): ?> 
  for prods in these categories 
<?php elseif(in_array($id, array(3,55,56,57,58,59,60))): ?> 
  for prods in other categories 
<?php else: ?> 
  everything else 
<?php endif; ?>
| improve this answer | |

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.