2

We've got a number of attributes which are generated by basic Excel formulas and then bulk-loaded into the catalog via imports. The process results in "extra" attributes which are read-only and shouldn't be altered by individuals.

Ideally these values wouldn't be stored in EAV attributes, but would instead be calculated on-the-fly as needed. I've considered (and not ruled out) extending the catalog/product model to have additional methods for this data, such as:

class My_Extension_Model_Catalog_Product extends Mage_Core_Catalog_Product {

  public function getSomeValue() {

    if (!$x = $this->getX() || !$y = $this->getY()) {
      return null;
    }

    return ($x * $y);
  }
}

The attribute needs to be available to consumers who expect it to behave like any other attribute--they should be able to call Mage::getModel('catalog/product')->load(123)->getSomeValue(); without any extra boilerplate or bootstrapping.

What other options would I have? Would there be events that would let me calculate and set the value when the product is being loaded? Or better could I assign the function when the product is loaded?

1 Answer 1

2

If you have a fixed amount of attributes I would suggest to implement a custom extension that creates them using the install script. The calculation can be done by rewriting the Mage_Core_Catalog_Product like you've written above.

As for as events goes you have 2 events for loading products:

  1. catalog_product_load_before
  2. catalog_product_load_after.

For loading product collections you have

  1. catalog_product_collection_load_before
  2. catalog_product_collection_load_after.
2
  • I'll see if I can build a solution into the collection load events; if possible I'd like to keep the data out of the database
    – STW
    Aug 28, 2015 at 18:19
  • 2
    At event catalog_product_load_after you can inject data into the model $model->setData('custom_attribute', $x * $y), then it will be available both as $model->getData('custom_attribute') and $model->getCustomAttribute(); Aug 28, 2015 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.