2012-11-01 53 views
4

嗨,好吧,我面臨的問題似乎很簡單,但現在變成了一場真正的噩夢。Magento新車屬性

有人問我一個屬性(即點)添加到所有的產品(這是使用的管理控制檯進行非常簡單),並有總作爲規則可以在設定的車屬性!?

我相當肯定,車屬性定義:

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表中都有一個匹配的列!

現在的問題是我應該做的有我的新的屬性來計算和規則處理評估?我應該在此表格中添加一列,並在購物車更改時更新其值?

如何做到這將是很有價值的感謝任何見解。

回答

1

我終於成功地完成任務,只是以供將來參考我在這裏解釋的過程。

在問題中提到的類(即:Mage_SalesRule_Model_Rule_Condition_Address)是關鍵的問題。我不得不重寫它,出於一些奇怪的原因,我無法通過擴展它來獲得所需的內容,所以我的類擴展了其父類(即:Mage_Rule_Model_Condition_Abstract)。

正如我說,我將我的屬性$屬性是這樣的:

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

我也修改getInputType()方法,並宣佈我的屬性作爲數字

現在是什麼做的伎倆是驗證( )方法:

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方法。你可以看到這個對象($地址)默認沒有payment_method,所以這個方法創建一個並分配給它。所以,我也一樣,只需添加以下代碼返回之前:

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

,現在我可以在這個屬性設置規則。

希望這些信息保存人在未來的時間。