【问题标题】:Magento New Cart AttributeMagento 新购物车属性
【发布时间】:2012-10-21 21:05:47
【问题描述】:

嗨,我面临的问题一开始似乎很简单,但现在变成了一场真正的噩梦。

我被要求为所有产品添加一个属性(即点)(使用管理面板非常简单地完成)并将其总数作为可以设置规则的购物车属性!?

我非常肯定购物车属性定义在:

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!

现在的问题是我应该怎么做才能在规则处理中计算和评估我的新属性?我应该在此表中添加一列并在购物车更改时更新其值吗?

任何关于如何做到这一点的见解都将非常有价值,谢谢。

【问题讨论】:

    标签: magento module attributes shopping-cart


    【解决方案1】:

    我终于设法完成了任务,为了将来参考,我在这里解释了这个过程。

    问题中提到的类(即: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 实例并将其发送到其父 validate 方法。你可以看到这个对象($address)默认没有payment_method,所以这个方法会创建一个并将它分配给它。于是我也照做了,只是在return前加了如下代码:

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

    现在我可以在这个属性上设置规则了。

    希望这些信息将来可以节省一些人的时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      • 2012-09-17
      • 2013-05-10
      相关资源
      最近更新 更多