Magento的新的车属性 [英] Magento New Cart Attribute

查看:207
本文介绍了Magento的新的车属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜好吧,我面临的问题似乎是在第一次很简单的,但变成了真正的噩梦了。

Hi well the problem I am facing seemed to be very simple at first but turned into a real nightmare now.

我被要求添加属性(即点)的所有产品(这是使用管理面板完成pretty简单),并有总的哪些规则可以根据设定的车属性!?

I was asked to add an attribute (namely point) to all the products (which was done pretty simple using the admin panel) and have its total as a cart attribute which rules can be set upon!?

我相当积极的,车属性定义:

I am quite positive that cart attributes are defined in:

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

所以,如果我改写这一模式,将项目添加到该数组我会在规则定义管理面板中显示的属性。似乎所有这些属性在sales_flat_quote_address表中的匹配列,除了total_qty和PAYMENT_METHOD!

So if I overwrite this model and add an item to that array I will get the attribute shown in rule definition admin panel. It seems that all these attributes has a matching column in sales_flat_quote_address table except for total_qty and payment_method!

现在的问题是,我该怎么办有我新的属性进行计算和处理规则进行评估?我应该在车更改列添加到该表并更新它的价值呢?

Now the problem is what should I do to have my new attribute be calculated and evaluated in rules processing? should I add a column to this table and update its value upon cart changes?

这是如何做到这一点任何有识之士将是巨大的价值的感谢。

Any insight on how to do this would be of great value thanks.

推荐答案

我终于成功地完成任务,只是以供将来参考我在这里解释的过程。

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

在问题中提到的类(即:Mage_SalesRule_Model_Rule_Condition_Address)是问题的关键。我不得不重写它和一些奇怪的原因,我无法得到我所需要的通过扩展它,所以我的类扩展它的父类(即:Mage_Rule_Model_Condition_Abstract)

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')

我也修改getInputType()方法,并宣布我的属性作为数字

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

现在是什么做的伎俩是validate()方法:

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

,你可以看到它prepares Mage_Sales_Model_Quote_Address的一个实例,并将其发送到其父validate方法。你可以看到,这个对象($地址)没有默认PAYMENT_METHOD所以这种方法创建一个,并将其分配给它。所以,我也一样,只需添加以下code返回前:

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.

这篇关于Magento的新的车属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆