use*_*820 4 magento conditional-statements
我想仅在客户购买某些产品时才显示条款和条件(agreements.phtml).例如,我们的一些产品需要处方.客户购买其中一个,我们希望条款出现.
我在后端创建了一个属性:prescription_required
伪代码如下:
Loop through products in the basket
if prescription_required == yes
set flag to yes
end if
end loop
Run Code Online (Sandbox Code Playgroud)
如果flag设置为yes,请通过agreements.phtml中的代码
我认为我遇到的主要问题是在agreement.phmtl中获取产品信息并在整个篮子中循环.
我试过了
<?php
$_product = Mage::getModel('catalog/product')->load($this->getData('product_id'));
echo $_product->getName();
?>
Run Code Online (Sandbox Code Playgroud)
但没有任何回报.
编辑:
我做了一个扩展,正是你正在努力实现并通过Magento Connect发布它.这是链接:http://www.magentocommerce.com/magento-connect/catalog/product/view/id/14603.您可以下载它,或按照以下步骤操作.链接到扩展源代码的链接位于答案的底部.
编辑结束
这些是你必须做的步骤:
假设和目标:
我假设您不想创建比实际需要更复杂的东西.如果购物车中至少有一个产品需要条款/条件,或者没有产品 - 如果购物车中没有产品,我将向您解释如何制作一个扩展,以使所有已启用的条款和条件出现需要这个.
我还假设我们想要把事情弄好并且留下核心文件.当然,修改核心功能可能更容易,但这种方法将来会引起麻烦.这就是我们制作新模块的原因.
接下来我假设所有商店都需要此功能,如果有多个商店.
最后一件事 - 我在Magento Community Edition 1.7上测试了我的方法.它不能保证在其他版本上工作,但我认为它应该适用于1.6和1.5.如果您使用其中一个版本,可以尝试.我不确定它是否适用于旧版本.
创建新属性:
这超出了问题的范围,但最好是:
当然,根据您的需要,您可以通过其他方式创建属性.
在我的例子中,product属性被赋予了代码conditional_agreements.此代码用于下面的代码段.
重写块
我们要重写的块是Mage_Checkout_Block_Agreements.它只有一个功能 - getAgreements().
首先是重写.我的包被调用bartoszgorski,我的模块是conditionalagreements,所以我的代码config.xml是:
<config>
<!-- other code -->
<global>
<!-- other code -->
<blocks>
<checkout>
<rewrite>
<agreements>Bartoszgorski_Conditionalagreements_Block_Agreements</agreements>
</rewrite>
</checkout>
</blocks>
<!-- other code -->
</global>
</config>
Run Code Online (Sandbox Code Playgroud)
在文件本身中,我只是使用帮助器(稍后解释)来检查,如果我们想要显示任何条款和条件.我的代码看起来像这样:
public function getAgreements() {
if(Mage::helper('conditionalagreements')->checkIfQuoteRequiresAgreements() == true) {
return parent::getAgreements();
} else {
return array();
}
}
Run Code Online (Sandbox Code Playgroud)
所以,如果要显示某些内容,我们就可以显示它.否则,function返回一个空array的协议.
重写帮手:
在helper我们要改写的Mage_Checkout_Helper_Data.如果helper没有重写,Magento将不会显示任何条款/条件,但仍然需要用户检查他们的复选框 - 这当然是不可能的.所以,我在config.xml中的代码是:
<config>
<!-- other code -->
<global>
<!-- other code -->
<helpers>
<conditionalagreements>
<class>Bartoszgorski_Conditionalagreements_Helper</class>
</conditionalagreements>
<checkout>
<rewrite>
<data>Bartoszgorski_Conditionalagreements_Helper_Data</data>
</rewrite>
</checkout>
</helpers>
<!-- other code -->
</global>
</config>
Run Code Online (Sandbox Code Playgroud)
在helper,有功能getRequiredAgreementIds()负责获得必须同意的每个条款和条件.我们想要完全相同,但前提是购物车中至少有一种产品需要它.这就是为什么我建议创建另一个帮助函数(checkIfQuoteRequiresAgreements()在我的例子中)来检查这些产品并在其中使用它getRequiredAgreementIds()(block以及如前所示).所以,重写helper应具有以下功能:
public function getRequiredAgreementIds()
{
if($this->checkIfQuoteRequiresAgreements() == true) {
if (is_null($this->_agreements)) {
if (!Mage::getStoreConfigFlag('checkout/options/enable_agreements')) {
$this->_agreements = array();
} else {
$this->_agreements = Mage::getModel('checkout/agreement')->getCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addFieldToFilter('is_active', 1)
->getAllIds();
}
}
return $this->_agreements;
} else {
return array();
}
}
public function checkIfQuoteRequiresAgreements() {
$showAgreements = false;
$quote = Mage::getSingleton('checkout/session')->getQuote();
foreach ($quote->getAllVisibleItems() as $quoteItem) {
$product = Mage::getModel('catalog/product')->load($quoteItem->getProductId());
if($product->getConditionalAgreements() == 1) {
$showAgreements = true;
break;
}
}
return $showAgreements;
}
Run Code Online (Sandbox Code Playgroud)
就是这样.这样做之后,你应该拥有你想要的东西.请记住,如果您在条款和条件中有多个项目,则只能显示所有项目或不显示任何项目.当然,您可以根据我的代码片段开发更多内容 - 这取决于您.
这是我的全功能模块:
https://github.com/bgorski/conditionalagreements
您可以预览我在那里所做的事情,或者只是下载并在您的网站上使用.
希望有所帮助.任何反馈都将非常感激.
| 归档时间: |
|
| 查看次数: |
3174 次 |
| 最近记录: |