嗨,我面临的问题一开始似乎很简单,但现在变成了一场真正的噩梦。
我被要求向所有产品添加一个属性(即点)(使用管理面板非常简单地完成)并将其总计作为可以设置规则的购物车属性!?
我非常肯定购物车属性是在以下位置定义的:
class Mage_SalesRule_Model_Rule_Condition_Address extends Mage_Rule_Model_Condition_Abstract
{
public function loadAttributeOptions()
{
$attributes = array(
'base_subtotal' => Mage::helper('salesrule')->__('Subtotal'),
'total_qty' => Mage::helper('salesrule')->__('Total Items Quantity'),
'weight' => Mage::helper('salesrule')->__('Total Weight'),
'payment_method' => Mage::helper('salesrule')->__('Payment Method'),
'shipping_method' => Mage::helper('salesrule')->__('Shipping Method'),
'postcode' => Mage::helper('salesrule')->__('Shipping Postcode'),
'region' => Mage::helper('salesrule')->__('Shipping Region'),
'region_id' => Mage::helper('salesrule')->__('Shipping State/Province'),
'country_id' => Mage::helper('salesrule')->__('Shipping Country'),
);
$this->setAttributeOption($attributes);
return $this;
}
<...>
因此,如果我覆盖此模型并向该数组添加一个项目,我将获得规则定义管理面板中显示的属性。似乎除了total_qty 和 payment_method 之外,所有这些属性在 sales_flat_quote_address 表中都有一个匹配的列!
现在的问题是我应该怎么做才能在规则处理中计算和评估我的新属性?我应该向该表添加一列并在购物车更改时更新其值吗?
任何有关如何做到这一点的见解都将非常有价值,谢谢。
我终于成功完成了任务,为了将来的参考,我在这里解释了这个过程。
问题中提到的类(即:Mage_SalesRule_Model_Rule_Condition_Address)是问题的关键。我不得不重写它,由于某种奇怪的原因,我无法通过扩展它来获得我需要的东西,所以我的类扩展了它的父类(即:Mage_Rule_Model_Condition_Abstract)。
正如我所说,我将属性添加到 $attributes 中,如下所示:
'net_score' => Mage::helper('mymodule')->__('Net Score')
我还修改了 getInputType() 方法并将我的属性声明为数字
现在的技巧是 validate() 方法:
public function validate(Varien_Object $object)
{
$address = $object;
if (!$address instanceof Mage_Sales_Model_Quote_Address) {
if ($object->getQuote()->isVirtual()) {
$address = $object->getQuote()->getBillingAddress();
}
else {
$address = $object->getQuote()->getShippingAddress();
}
}
if ('payment_method' == $this->getAttribute() && ! $address->hasPaymentMethod()) {
$address->setPaymentMethod($object->getQuote()->getPayment()->getMethod());
}
return parent::validate($address);
}
正如您所看到的,它准备了 Mage_Sales_Model_Quote_Address 的实例并将其发送到其父验证方法。您可以看到该对象 ($address) 默认情况下没有 payment_method,因此此方法创建一个并将其分配给它。所以我也做了同样的事情,只是在返回之前添加了以下代码:
if ('net_score' == $this->getAttribute() && ! $address->hasNetScore()) {
$address->setNetScore( /*the logic for retrieving the value*/);
}
现在我可以对此属性设置规则。
希望这些信息将来可以节省某人的时间。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)