I would like to add a list of categories to my conditional statement, how should I add multiple categories id ?
Using like 123,124,125 or 123 || 124
I have a big list of categories so I am looking for the cleanest way to achieve that
thanks
<?php
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product = $objectManager->get('MagentoFrameworkRegistry')->registry('current_product');
$categories = $product->getCategoryIds(); /*will return category ids array*/
foreach($categories as $category){
$cat = $objectManager->create('MagentoCatalogModelCategory')->load($category);
echo $cat->getId();
}
?>
<?php if($cat->getId()==123): ?>
<?php echo $block->getLayout()->createBlock('MagentoCmsBlockBlock')->setBlockId('myblockid')->toHtml();?>
<?php endif; ?>
3
Answers
Use
in_array
PHP function:The best solution is to use in_array function
Use
in_array()
: This Checks if a value exists in an array or not.Return true if exist.