限制包含特定类别的产品的购物车数量

时间:2020-02-20 20:01:36

标签: php magento magento-1.9

如果属于特定类别ID的产品数量大于3,我想显示一条消息。

到目前为止,这是我目前所能做的,如果产品包含类别ID,也许会设置“ if”语句?

    <?php
     $session= Mage::getSingleton('checkout/session');
     $items = $session->getQuote()->getAllItems();
     $class2 = "hide";
     $count = 0;


   foreach ($items as $item) {

     $categories = $item->getProduct()->getCategoryIds();

     foreach ($categories as $categoryId){

       if($categoryId == 125) $count++;   
     }

     if($count > 3) $class2 = "show";
      }
    ?>

    <div class="<?php echo $class2; ?> warning">
        <p style="margin:0;" class="red">Flash Sale Quantity Is Limited To 3</p>
    </div>

1 个答案:

答案 0 :(得分:1)

大概是可以做到的……第一步是找到类别ID,然后在if语句中开始计数,如果$count>3,那么$class2将是"show";

<?php
 $session= Mage::getSingleton('checkout/session');
 $items = $session->getQuote()->getAllItems();
 $class2 = "hide";
 $count = 0;

 foreach ($items as $item) {

    $categories = $item->getProduct()->getCategoryIds();

    foreach ($categories as $categoryId){

        if($categoryId == 125) { 

            if($count > 3) { $class2 = "show" }

            $count = $count + 1;
        }
    }   
 }

?>

<div class="<?php echo $class2; ?> warning">
    <p style="margin:0;" class="red">Flash Sale Quantity Is Limited To 3</p>
</div>