0

I am working on a custom module and this is one of my form:

enter image description here

At the moment, I am using a text input for "Magento SKU". The form is constructed like this:

Company_Integration_Block_Adminhtml_Manageskumappings_Edit_Tab_Form

$fieldset->addField('magento_sku', 'text', array(
    'label' => Mage::helper('integration')->__('Magento SKU'),
    'class' => 'required-entry',
    'required' => true,
    'name' => 'magento_sku'
));

$fieldset->addField('sage_sku', 'text', array(
    'label' => Mage::helper('integration')->__('Sage SKU'),
    'class' => 'required-entry',
    'required' => true,
    'name' => 'sage_sku'
));

$fieldset->addField('status', 'select', array(
    'label' => Mage::helper('integration')->__('Status'),
    'name' => 'status',
    'values' => array(
        array(
            'value' => 1,
            'label' => Mage::helper('integration')->__('Enabled')
        ),
        array(
            'value' => 2,
            'label' => Mage::helper('integration')->__('Disabled')
        )
    )
));

I want to turn this field magento_sku into a drop down list (from existing sku in magento).

I know I can do this manually by doing this:

$fieldset->addField('magento_sku', 'select', array(
    'label'     => Mage::helper('form')->__('Magento SKU'),
    'class'     => 'required-entry',
    'required'  => true,
    'name'      => 'title',
    'value'     => '1',
    'values'    => array(
        '-1'    => 'Please Select..',
        'SKU1'  => 'SKU1',
        'SKU2'  => 'SKU3',
        'SKU3'  => 'SKU3'
    )
));

How can I automate this, so that this field is auto-populated from an external source (model? helper?) within my module?


This is my module config: http://pastebin.com/raw.php?i=ATy2NnEL

This is what I tried so far:

$fieldset->addField('magento_sku', 'select', array(
    'label' => Mage::helper('integration')->__('Magento SKU'),
    'class' => 'required-entry',
    'required' => true,
    'name' => 'magento_sku',
    'values' => Mage::getSingleton('manageskumappings/skus')->toOptionArray()
));

My model was here: app\code\local\Company\Integration\Model\Skus.php with the following:

class Company_Integration_Model_Skus extends Varien_Object
{
    static public function getOptionArray()
    {
        return array(
            '-1'    => 'Please Select..',
            'SKU1'  => 'SKU1',
            'SKU2'  => 'SKU3',
            'SKU3'  => 'SKU3'
        );
    }
}

When I did this, I get the error: Fatal error: Call to a member function toOptionArray() on a non-object similar to my other question from yesterday: https://stackoverflow.com/questions/23222513/magento-call-to-a-member-function-getoptionarray-on-a-non-object

2
  • 1
    Why the down vote, is this not a valid question?
    – Latheesan
    Commented Apr 23, 2014 at 14:10
  • It should be a requirement that people explain why they downvote. That's SO for ya. I found your question helpful, thank you.
    – sparecycle
    Commented Dec 9, 2014 at 19:46

2 Answers 2

2

this piece of code will help you.

$products = array();
$productCollection = Mage::getResourceModel('catalog/product_collection')->addAttributeToSort('sku', 'asc');
$i = 1;
$items = $productCollection->getData();
foreach($items as $item) {
    $products[$i]['value'] = $item['sku'];
    $products[$i]['label'] = $item['sku'];
    $i++;
}

$fieldset->addField('magento_sku', 'select', array(
    'label'     => Mage::helper('adminhtml')->__('Product SKU'),
    'name'      => 'magento_sku',
    'values'    => $products,
    'required'  => true
));
1
  • Worked perfectly for me!
    – sparecycle
    Commented Dec 11, 2014 at 20:05
1

Replace

Mage::getSingleton('manageskumappings/skus')->toOptionArray()

with:

Mage::getSingleton('skumappings/skus')->toOptionArray()

as according to your config file the correct alias of your module is skumappings.

Also I don't see any reason in declaring getOptionArray method as static.

4
  • I tried that and now I am getting a different error: Invalid method Company_Integration_Model_Skus::toOptionArray(Array())
    – Latheesan
    Commented Apr 23, 2014 at 14:20
  • Why don't you pull SKUs inside of your Company_Integration_Block_Adminhtml_Manageskumappings_Edit_Tab_Form class? Commented Apr 23, 2014 at 14:35
  • That's what I am having to do now. I thought it would be neater/easier to manage if it was a model file on it's own.
    – Latheesan
    Commented Apr 23, 2014 at 14:37
  • have you removed static from toOptionArray method? Commented Apr 23, 2014 at 14:45

Your Answer

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

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