New attribute for Magento

Hi, the problem that I encountered was at first very simple, but now it has become a real nightmare.

I was asked to add an attribute (namely, a period) to all products (which was done quite simply using the admin panel) and have it in common as an attribute of the basket, the rules of which can be set !?

I am pretty sure that the basket 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; } <...> 

Therefore, if I overwrite this model and add an element to this array, I will get the attribute shown in the admin panel of the rule definition. It seems that all of these attributes have a corresponding column in the sales_flat_quote_address table, with the exception of total_qty and payment_method!

Now the problem is, what should I do so that my new attribute is calculated and evaluated when processing the rules? Should I add a column to this table and update its value when the basket changes?

Any understanding of how to do this will be of great benefit.

+4
source share
1 answer

I finally managed to complete the task, and for future reference, I explain the procedure here.

The class mentioned in the question (i.e.: Mage_SalesRule_Model_Rule_Condition_Address) is the key to the problem. I had to rewrite it and for some odd reason, I could not get what I needed by extending it so that my class extends the parent class (e.g. Mage_Rule_Model_Condition_Abstract).

As I said, I added my attribute to $ attributes as follows:

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

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

now what 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 validation method. you can see that this object ($ address) does not have a payment_method method by default, therefore this method creates it and assigns it. So I did the same, I just added the following code before returning:

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

and now I can set the rules for this attribute.

We hope that this information will save someone time in the future.

+1
source

Source: https://habr.com/ru/post/1443398/


All Articles