0

I have created the following to allow a different static CMS block to displayed on the product page depending on the category ID.

<?php 
    $_category_detail=Mage::registry('current_category'); //Get the current category id
    $product = Mage::getModel('catalog/product')->load($product_id);   //Get the current category id
    $category = Mage::getModel('catalog/layer')->getCurrentCategory(); //Get the current category id
    ?>
<?php if($category->getId()==23): ?>
    <div id="sizingmap">
        <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('sizespecpantsslim')->toHtml(); ?>
    </div>
<?php endif;?>  

<?php if($category->getId()==10): ?>
    <div id="sizingmap">
        <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('sizespecjacket')->toHtml(); ?>
    </div>
<?php endif;?>

Everything is fine for the category part, though i would like to display a different block based on the product id in the same category.

For example (this is obviously not correct):

<?php if($category->getId()==23) "AND the product id are "372,363,354,349,344": ?>
<div id="sizingmap">
    <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('sizespecpantsslim')->toHtml(); ?>
</div>
else // if they are not the mentioned product id's 
<div id="sizingmap">
    <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('sizespecpantsstd')->toHtml(); ?>
</div> 
Java D
  • 436
  • 1
  • 8
  • 18
mini
  • 1
  • 2
  • 1
    Why are you not utilizing the category description field provided by `magento` any specific reason to use static blocks for category ?? – M Khalid Junaid Jun 27 '13 at 06:38
  • This is a block that contains a sizing chart image which is different depending on the category. Though for pants eventhough they are in the same category there are two different charts depending on the type of pants (slim/standard). So I want to display the chart dependent on the category AND the product ID. I dont want to use the product description because the chart is displayed in a tab. – mini Jun 27 '13 at 06:58

4 Answers4

2
Try is this not tested.

<?php $productIDarray = array("372","363","354","349","344")?>
<?php if($category->getId()==23 && in_array($productIDarray , $productId)): ?>

<div id="sizingmap">
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('sizespecpantsslim')->toHtml(); ?>
</div>
<?php else:?>
<div id="sizingmap">
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('sizespecpantsstd')->toHtml(); ?>
</div> 
<?php endif;?>


Thanks,
  • for this line: getId()==23) && in_array($productIDarray , $productId): ?> i get an error: Parse error: syntax error, unexpected T_BOOLEAN_AND in – mini Jun 27 '13 at 14:32
  • I have not tested this code. This is only idea how you can go ahead. Code is updated please check again. Still not tested. –  Jun 27 '13 at 15:07
  • tried that one. It displays the else part "sizespecpantsstd" regardless. I also tried changing the array to: still no effect. – mini Jun 28 '13 at 01:17
1

try this

<?php 
$id_array = array(372,363,354,349,344); //product ids
if(($category->getId()==23) && in_array($product_id,$id_array)){ ?>
    <div id="sizingmap">
        <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('sizespecpantsslim')->toHtml(); ?>
    </div>

<?php }else{ ?>

<?php if($category->getId()==10): ?>
    <div id="sizingmap">
        <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('sizespecjacket')->toHtml(); ?>
    </div>
<?php endif;?>
<?php } ?>
Deepak Mallah
  • 3,926
  • 4
  • 21
  • 26
  • for this line `code` getId()==23) && in_array($product_id,$id_array){ ?> `code` i get the following error: Parse error: syntax error, unexpected '{' on line 11
    – mini Jun 27 '13 at 14:26
  • please check it now i have modified it – Deepak Mallah Jun 27 '13 at 16:15
  • your one displays the std chart which is part of your else options as well. I have a feeling the product id array aspect is not working and its jumping to the elseif straight away. – mini Jun 28 '13 at 01:25
1
$id_array = array(372,363,354,349,344); 
<?php if(($category->getId()==23) && in_array($product_id,$id_array){ ?>
    <div id="sizingmap">
        <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('sizespecpantsslim')->toHtml(); 
}else{

?>
    </div>    
<?php if($category->getId()==10): ?>
    <div id="sizingmap">
        <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('sizespecjacket')->toHtml(); ?>
    </div>
<?php endif;?>
<?php } ?>

hope this helps

  • for this line : getId()==23) && in_array($product_id,$id_array){ ?> I receive the following error: Parse error: syntax error, unexpected '{' on line 11
    – mini Jun 27 '13 at 14:29
0
<?php 
    $_category_detail=Mage::registry('current_category'); //Get the current category
    $product = Mage::registry('current_product'); // Get the current product
    $productId = $this->getProduct()->getId(); // Get the current product ID
    $category = Mage::getModel('catalog/layer')->getCurrentCategory(); //Get the current category id
    ?>
<!-- standard pants - sizing chart -->
<?php if(in_array($this->getProduct()->getId(), array(372,363,354,349,344))): ?>
<div id="sizingmap">
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('sizespecpantsstd')->toHtml(); ?>
</div>
<?php endif;?>
<!-- slim pants - sizing chart -->
<?php if(in_array($this->getProduct()->getId(), array(339,334,329,324,319,314))): ?>
<div id="sizingmap">
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('sizespecpantsslim')->toHtml(); ?>
</div>
<?php endif;?>
<!-- jackets - sizing chart -->
<?php if($category->getId()==10): ?>
    <div id="sizingmap">
        <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('sizespecjacket')->toHtml(); ?>
    </div>
<?php endif;?>

thank you guys for your input, I was able to produce the result by putting bits and pieces of everyones codes together.

Cheers :)

mini
  • 1
  • 2