简体   繁体   中英

magento remove add to cart with view.phtml

I followed [these directions][1] for removing the "add to cart". I am trying to remove add to cart button for items with attribute of "instore_only" and when the response is yes, I want it to echo a static block I have made for it. When I do the first part, the button never goes away. Here is my code:

//Check if the "Available in store only" variable is set to 'Yes':  
        if(($_product->getAttributeText('instore_only')) == "Yes"){
//If set to Yes, tell PHP what to output:
        echo $this->getLayout()->createBlock('cms/block')->setBlockId('instore_only')->toHtml();
}
//If set to No, then show the 'add to cart box' as normal.
        else {
?>
        <?php if (!$this->hasOptions()):?>
            <div class="add-to-box">
                <?php if($_product->isSaleable()): ?>
                    <?php echo $this->getChildHtml('addtocart') ?>
                    <?php if( $this->helper('wishlist')->isAllow() || $_compareUrl=$this->helper('catalog/product_compare')->getAddUrl($_product)): ?>
                        <span class="or"><?php echo $this->__('OR') ?></span>
                    <?php endif; ?>
                <?php endif; ?>
                <?php echo $this->getChildHtml('addto') ?>
            </div>
            <?php echo $this->getChildHtml('extra_buttons') ?>
        <?php elseif (!$_product->isSaleable()): ?>
            <div class="add-to-box">
                <?php echo $this->getChildHtml('addto') ?>
            </div>
        <?php endif; ?>

        <?php if ($_product->getShortDescription()):?>
            <div class="short-description">
                <h2><?php echo $this->__('Quick Overview') ?></h2>
                <div class="std"><?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?></div>
            </div>
        <?php endif;?>

        <?php echo $this->getChildHtml('other');?>

        <?php if ($_product->isSaleable() && $this->hasOptions()):?>
            <?php echo $this->getChildChildHtml('container1', '', true, true) ?>
        <?php endif;?>

        <?php
        }
        ?>

I have verified the location of the correct view.phtml using template path hints on my frontend.

So, in short, does this code look right, and if not, can I call a cms block in view.phtml? The site supports a small retail store, so some items are only available in the store and not for online purchasing.

I'm about 1 week old in magento and code. I am trying to do a few tweeks to a basic site with a basic template.

Check your attribute settings to make sure it's available on the front end. Also, make sure "Used in Listing" is set to yes so it gets added to the index tables. This makes it quicker to call. I suspect that will allow your current code to work...but not sure without testing.

A less elegant way is to call it from the resource model. I don't recommend this way because you are bypassing the index tables...

Try:

 $_product->getResource()->getAttribute('instore_only')->getFrontend()->getValue($_product);

To hide the qty box and the "Add to cart" button from view.phtml, You can comment all the code in addtocart.phtml located in template/catalog/product/view/addtocart.phtml

Hope this helps

I'm assuming, from your question, that the static block is never displayed and that the add to cart button is always displayed. I'm also going to assume that you set your "Instore Only" attribute to "Yes" on the products that you are testing, you've created and enabled a CMS Static block with the identifier instore_only for the current store, and that you've cleared or disabled the Magento Cache.

Check your product attribute configuration

$_product->getAttributeText('instore_only') will return the text value for attributes that have the type Dropdown or Multiple select .

Yes/No catalog input type

If your product attribute is configured with the Yes/No catalog input type, then getAttributeText() will not return a value for it - so it will never be equal to "Yes" in your test and your static block will never be displayed.

Instead you should ask for the attribute value directly. The Yes/No input type is directly compatible with boolean operations, so you can simply test the value in your if statement. Like so:

if ($_product->getInstoreOnly()) {
  //output your static block
} else {
  //output the add to cart form
}

Text catalog input type

If your attribute configuration as the Text or Text area catalog input type, then you'd compare like this:

if ($_product->getInstoreOnly() == "Yes") {
  //output your static block
} else {
  //ouput the add to cart form
}

In this instance you'd have to manually type Yes into a box in the product editor to make this work.

Dropdown catalog input type

If your attribute is configured as a Dropdown , or Multiple select choice, to which you've manually added a choice named Yes , then your code above should be correct.

Used in product listing should be Yes

You should also check that the catalog attribute Used in product listing option set to Yes , so that the attribute value is loaded on the product page, for you, by Magento.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM