[英]magento remove add to cart with view.phtml
我按照[这些说明] [1]删除了“添加到购物车”。 我正在尝试删除属性为“ instore_only”的商品的“添加到购物车”按钮,并且当响应为“是”时,我希望它回显为之创建的静态块。 当我做第一部分时,按钮永远不会消失。 这是我的代码:
//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
}
?>
我已经在前端使用模板路径提示验证了正确的view.phtml的位置。
简而言之,这段代码看起来正确吗?如果不正确,我可以在view.phtml中调用cms块吗? 该站点支持一家小型零售商店,因此某些商品仅在商店中可用,而不能在线购买。
我在magento和代码方面大约有1周大。 我正在尝试用基本模板对基本站点进行数周的工作。
检查您的属性设置,以确保该属性在前端可用。 另外,确保将“在清单中使用”设置为“是”,以便将其添加到索引表中。 这样可以更快地打电话。 我怀疑这将允许您当前的代码正常工作...但是不确定是否需要测试。
一种不太优雅的方法是从资源模型中调用它。 我不建议这样做,因为您绕过了索引表...
尝试:
$_product->getResource()->getAttribute('instore_only')->getFrontend()->getValue($_product);
要从view.phtml隐藏数量框和“添加到购物车”按钮,您可以注释位于template/catalog/product/view/addtocart.phtml
中的addtocart.phtml
所有代码。
希望这可以帮助
根据您的问题,我假设静态框永远不会显示,并且添加到购物车按钮始终会显示。 我还要假设您在要测试的产品上将“仅商店”属性设置为“是”,您已经为当前商店创建并启用了标识符为instore_only
的CMS静态块,并且已清除或禁用了Magento缓存。
$_product->getAttributeText('instore_only')
将返回类型为Dropdown
或Multiple select
属性的文本值。
如果您的产品属性配置为“ Yes/No
目录输入类型,则getAttributeText()
不会为其返回值-因此在测试中它永远不会等于“是”,并且永远不会显示静态块。
相反,您应该直接要求属性值。 Yes/No
输入类型与布尔运算直接兼容,因此您可以简单地在if语句中测试该值。 像这样:
if ($_product->getInstoreOnly()) {
//output your static block
} else {
//output the add to cart form
}
如果您将属性配置为“ Text
或“ Text area
目录输入类型,那么您将进行如下比较:
if ($_product->getInstoreOnly() == "Yes") {
//output your static block
} else {
//ouput the add to cart form
}
在这种情况下,您必须在产品编辑器的框中手动输入“ Yes
才能执行此操作。
如果您的属性配置为Dropdown
或Multiple select
选择,并且已向其中手动添加了名为Yes
的选择,则上面的代码应该正确。
您还应该检查“ Used in product listing
目录”属性Yes
设置为“ Yes
,以便该属性值由Magento加载到您的商品页面上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.