【问题标题】:Magento - two if statements within same categoryMagento - 同一类别中的两个 if 语句
【发布时间】:2013-06-27 06:33:57
【问题描述】:

我创建了以下内容,以允许根据类别 ID 在产品页面上显示不同的静态 CMS 块。

<?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;?>

类别部分的一切都很好,尽管我想根据同一类别中的产品 ID 显示不同的块。

例如(这显然是不正确的):

<?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> 

【问题讨论】:

  • 您为什么不使用magento 提供的类别描述字段以任何具体原因使用类别的静态块??
  • 这是一个包含大小图表图像的块,该图表图像因类别而异。尽管裤子属于同一类别,但根据裤子的类型(修身/标准),有两种不同的图表。所以我想根据类别和产品 ID 显示图表。我不想使用产品描述,因为图表显示在选项卡中。

标签: php magento content-management-system block specs


【解决方案1】:
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,

【讨论】:

  • 对于这一行:getId()==23) && in_array($productIDarray , $productId): ?> 我得到一个错误:解析错误:语法错误,意外的 T_BOOLEAN_AND 在
  • 我没有测试过这段代码。这只是你如何继续前进的想法。代码已更新,请再次检查。仍未测试。
  • 试过那个。无论如何,它都会显示其他部分“sizespecpantsstd”。我还尝试将数组更改为: 仍然没有效果。
【解决方案2】:
$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 } ?>

希望对你有帮助

【讨论】:

  • 对于这一行:getId()==23) && in_array($product_id,$id_array){ ?> 我收到以下错误:解析错误:语法错误,11
    行出现意外的“{”
【解决方案3】:

试试这个

<?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 } ?>

【讨论】:

  • 对于这一行code getId()==23) && in_array($product_id,$id_array){ ?> code 我得到以下信息错误:解析错误:语法错误,第 11 行出现意外的“{”
  • 请检查一下,我已经修改了
  • 您的会显示标准图表,这也是您的 else 选项的一部分。我感觉产品 id 数组方面不起作用,它会立即跳转到 elseif。
【解决方案4】:
<?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;?>

谢谢你们的投入,我能够通过将每个人的代码的点点滴滴放在一起来产生结果。

干杯:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 2019-05-05
    相关资源
    最近更新 更多