Magento New Cart Attribute

≯℡__Kan透↙ 提交于 2019-12-05 12:42:25

I finally managed to accomplish the task and just for future reference I explain the procedure here.

The class mentioned in the question (ie: Mage_SalesRule_Model_Rule_Condition_Address) is the key to the problem. I had to rewrite it and for some odd reason I couldn't get what I needed by extending it so my class extended its parent class (ie: Mage_Rule_Model_Condition_Abstract).

As I said I added my attribute to $attributes like this:

'net_score' => Mage::helper('mymodule')->__('Net Score')

I also modified getInputType() method and declared my attribute as numeric

now what does the trick is the validate() method:

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);
}

as you can see it prepares an instance of Mage_Sales_Model_Quote_Address and sends it to its parent validate method. you can see that this object ($address) does not have payment_method by default so this method creates one and assigns it to it. So I did the same, simply I added the following code before the return:

if ('net_score' == $this->getAttribute() && ! $address->hasNetScore()) {
    $address->setNetScore( /*the logic for retrieving the value*/);
}

and now I can set rules upon this attribute.

Hope that these information saves somebody's time in the future.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!