简体   繁体   English

magento删除添加到购物车用view.phtml

[英]magento remove add to cart with view.phtml

I followed [these directions][1] for removing the "add to cart". 我按照[这些说明] [1]删除了“添加到购物车”。 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. 我正在尝试删除属性为“ instore_only”的商品的“添加到购物车”按钮,并且当响应为“是”时,我希望它回显为之创建的静态块。 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. 我已经在前端使用模板路径提示验证了正确的view.phtml的位置。

So, in short, does this code look right, and if not, can I call a cms block in view.phtml? 简而言之,这段代码看起来正确吗?如果不正确,我可以在view.phtml中调用cms块吗? 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. 我在magento和代码方面大约有1周大。 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 要从view.phtml隐藏数量框和“添加到购物车”按钮,您可以注释位于template/catalog/product/view/addtocart.phtml中的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. 我还要假设您在要测试的产品上将“仅商店”属性设置为“是”,您已经为当前商店创建并启用了标识符为instore_only的CMS静态块,并且已清除或禁用了Magento缓存。

Check your product attribute configuration 检查您的产品属性配置

$_product->getAttributeText('instore_only') will return the text value for attributes that have the type Dropdown or Multiple select . $_product->getAttributeText('instore_only')将返回类型为DropdownMultiple 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. 如果您的产品属性配置为“ Yes/No目录输入类型,则getAttributeText()不会为其返回值-因此在测试中它永远不会等于“是”,并且永远不会显示静态块。

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. Yes/No输入类型与布尔运算直接兼容,因此您可以简单地在if语句中测试该值。 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: 如果您将属性配置为“ Text或“ Text area目录输入类型,那么您将进行如下比较:

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. 在这种情况下,您必须在产品编辑器的框中手动输入“ Yes才能执行此操作。

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. 如果您的属性配置为DropdownMultiple select选择,并且已向其中手动添加了名为Yes的选择,则上面的代码应该正确。

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. 您还应该检查“ Used in product listing目录”属性Yes设置为“ Yes ,以便该属性值由Magento加载到您的商品页面上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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