如何使用php显示和隐藏“添加到购物车”按钮?

如何使用php显示和隐藏“添加到购物车”按钮?,php,magento,Php,Magento,我正在修改我的magento网站上的addtocart.phtml,我想做的是使用一点PHP代码,如果某个产品属于某个类别,它可以简化隐藏并显示“添加到购物车”按钮。我对该守则的看法是: <?php if($_product_category == "beds"){ <div class="add-to-cart"> <div class="qty"> <?php if(!$_product->isGrouped()):

我正在修改我的magento网站上的addtocart.phtml,我想做的是使用一点PHP代码,如果某个产品属于某个类别,它可以简化隐藏并显示“添加到购物车”按钮。我对该守则的看法是:

<?php if($_product_category == "beds"){
    <div class="add-to-cart">
    <div class="qty">
        <?php if(!$_product->isGrouped()): ?>
        <label for="qty"><?php echo $this->__('Quantity:') ?></label>
        <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Quantity') ?>" class="input-text qty" />
        <?php endif; ?>
    </div>
    <div class="add-cart">
        <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><i class="fa fa-shopping-cart "> </i><?php echo $buttonTitle ?></span></span></button>
        <?php echo $this->getChildHtml('', true, true) ?>
    </div>
</div>
} else {
      //hide add to cart button
}
?>


我觉得首先你应该得到一个产品的所有类别,然后检查一下,如果该类别中存在指定的类别,那么显示按钮,否则隐藏按钮,你可以通过下面的代码得到类别

<?php
    $cats = $_product->getCategoryIds();

    foreach ($cats as $category_id) {
        $_cat = Mage::getModel('catalog/category')->load($category_id) ;
        echo $_cat->getName();
    } 
    if(in_array($cats,'your_category_id')){ ?>
        <div class="add-to-cart">
        <div class="qty">
            <?php if(!$_product->isGrouped()): ?>
            <label for="qty"><?php echo $this->__('Quantity:') ?></label>
            <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Quantity') ?>" class="input-text qty" />
            <?php endif; ?> 
        </div>
        <div class="add-cart">
            <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><i class="fa fa-shopping-cart "> </i><?php echo $buttonTitle ?></span></span></button>
            <?php echo $this->getChildHtml('', true, true) ?>
        </div>
    </div>
   <?php  } else {
          //hide add to cart button
    }

    ?>


答案是,您使用一个属性和一些php代码来激活和取消激活“立即购买”按钮

我认为您正在这样做。如果类别为“床”,则您的代码将显示addtocart按钮,否则它将不会做任何事,因此它不会显示您在寻找什么?您应该在上面问一个问题。现在,您只描述到目前为止您所做的工作,而不是描述是否存在问题。类似的工作会删除“添加到购物车”按钮,并将其替换为产品名称,但不只是类别id 11[IMG]的每种产品都会这样做