Question

I'm using ultimate module creator. And I created one entity.
And one of the attribute is product attribute (SKU).

Its created SKU as an attribute in back end but its not binding product SKU collection.

following function returns null array.

public function getAllOptions($withEmpty = true, $defaultValues = false){
    $source  = Mage::getModel('eav/config')->getAttribute('catalog_product', 'sku');
    return $source->getSource()->getAllOptions($withEmpty, $defaultValues);
} 
Was it helpful?

Solution

The dropdown attributes from your custom entities that come from product attributes, work only with dropdown or multiselect product attributes.
SKU is a text attribute.
It will work with product attributes like color or country of manufacture, but not with sku or name or weight, etc.
There is no verification done when creating the module to see if the product attribute is a dropdown or multiselect because you can create a module using UMC on one instance that does not contain the attribute you want, but use it on an other instance that has it. Or you can add the product attribute later.

[EDIT]
If you want to use the SKU in a dropdown you need to modify the method in your question.
Something like this:

protected $_options = null;
public function getAllOptions($withEmpty = true, $defaultValues = false){
    if (is_null($this->_options)) {
        $collection = Mage::getModel('catalog/product')->getCollection();
        $this->_options = array();
        foreach ($collection as $product) {
            $this->_options[] = array('value' => $product->getSku(), 'label' => $product->getSku());
        }
    }
    $skus = $this->_options;
    if ($withEmpty) {
        array_unshift($skus, array('label' => '', 'value'=>''));
    }
} 

Since SKU is a text attribute and the options for your entity are created by default with type int you need to change the type of your entity attribute to text

But I advice you do do this only if you have a few products in your website. If you have over 300-400 products this might turn into a performance killer because you have to go through the product collection each time you use this attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top