1

I have programmatically created some custom product attributes as part of an import routine. These have been assigned to an attribute set and they appear as expected in the admin alongside the appropriate products. However, the values will not save either in the admin OR programmatically via a script. That is, until I set "visible on product view page on front-end" to true.

Why should that make a difference? Does setting that trigger some additional setting up of the attributes that I may have missed in the creation process? Once a value has been saved - I can switch off that option and it still works. Oddness.

Here is the function that is used to create the attributes

public function  newMagentoAttribute($name=null,$set=null,$sortOrder=null,$details=null,$productType="simple,grouped",$atttributeType="text"){
    /*with reference to code found here: http://pradeepkumarrcs.blogspot.co.uk/2011/12/creating-new-attribute-and-adding-to.html [jh]*/
    $attr_data = array(
                'attribute_code' => $name,
                'is_global' =>1,
                'frontend_input' => $atttributeType, //'boolean',
                'default_value_text' => '',
                'default_value_yesno' => '0',
                'default_value_date' => '',
                'default_value_textarea' => '',
                'is_unique' => '0',
                'is_required' => '0',
                'apply_to' => array($productType), //array('grouped')
                'is_configurable' => '0',
                'is_searchable' => '1',
                'is_visible_in_advanced_search' => '0',
                'is_comparable' => '0',
                'is_used_for_price_rules' => '0',
                'is_wysiwyg_enabled' => '0',
                'is_html_allowed_on_front' => '0',
                'is_visible_on_front' => '0',
                'used_in_product_listing' => '0',
                'used_for_sort_by' => '0',
                'frontend_label' => array($name)
            );
    $model = Mage::getModel('catalog/resource_eav_attribute');
    $model->addData($attr_data);
    $model->setEntityTypeId(Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId());
    $model->setIsUserDefined(1);
    if(is_numeric($sortOrder)){
        $model->setPosition($sortOrder);
    }
    try {
        $model->save();
        $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
        //-------------- add attribute to set
        $attribute_set_id=$setup->getAttributeSetId('catalog_product', $set);
       // $attribute_group_id=$setup->getAttributeGroupId('catalog_product', $attribute_set_id, $group_name);
        $attribute_id=$setup->getAttributeId('catalog_product', $name);
        $groupId = $this->createGroupInSet($attribute_set_id,"AE");
        $setup->addAttributeToSet('catalog_product',$attribute_set_id,$groupId, $attribute_id);

        $this->existingAttrs[$name] = "";
    } catch (Exception $e) {
        $this->echoOut( 'ERROR: Sorry, error occured while trying to save the attribute. Error: '.$e->getMessage() );
    }
}

Ideally I am looking for a "what might be going on is..." or "it doesn't work because..." type answer here - I want to understand the process. Maybe it is the simple act of editing the attribute in the admin that triggers something to be set somewhere that makes it "complete"?

* EDIT * Turns out I do not even have to change any of the attribute settings - I just need to load it then save it the admin. Ideas?

Thanks

3
  • Can you please share your codes here ?
    – Arunendra
    Sep 14, 2015 at 9:22
  • oops for the extra t in atttributeType Sep 14, 2015 at 9:27
  • Note: This is a dev environment - all the caches are disabled. Sep 14, 2015 at 9:31

1 Answer 1

1

Try This code :-

private function createAttribute($code, $label, $attribute_type, $product_type)
{       
    $_attribute_data = array(
        'attribute_code' => 'old_site_attribute_'.(($product_type) ? $product_type : 'joint').'_'.$code,
        'is_global' => '1',
        'frontend_input' => $attribute_type, //'boolean',
        'default_value_text' => '',
        'default_value_yesno' => '0',
        'default_value_date' => '',
        'default_value_textarea' => '',
        'is_unique' => '0',
        'is_required' => '0',
        'apply_to' => array($product_type), //array('grouped')
        'is_configurable' => '0',
        'is_searchable' => '0',
        'is_visible_in_advanced_search' => '0',
        'is_comparable' => '0',
        'is_used_for_price_rules' => '0',
        'is_wysiwyg_enabled' => '0',
        'is_html_allowed_on_front' => '1',
        'is_visible_on_front' => '0',
        'used_in_product_listing' => '0',
        'used_for_sort_by' => '0',
        'frontend_label' => array('Old Site Attribute '.(($product_type) ? $product_type : 'joint').' '.$label)
    );


    $model = Mage::getModel('catalog/resource_eav_attribute');

    if (!isset($_attribute_data['is_configurable'])) {
        $_attribute_data['is_configurable'] = 0;
    }
    if (!isset($_attribute_data['is_filterable'])) {
        $_attribute_data['is_filterable'] = 0;
    }
    if (!isset($_attribute_data['is_filterable_in_search'])) {
        $_attribute_data['is_filterable_in_search'] = 0;
    }

    if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
        $_attribute_data['backend_type'] = $model->getBackendTypeByInput($_attribute_data['frontend_input']);
    }

    $defaultValueField = $model->getDefaultValueByInput($_attribute_data['frontend_input']);
    if ($defaultValueField) {
        $_attribute_data['default_value'] = $this->getRequest()->getParam($defaultValueField);
    }


    $model->addData($_attribute_data);

    $model->setEntityTypeId(Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId());
    $model->setIsUserDefined(1);


    try {
        $model->save();
    } catch (Exception $e) { echo '<p>Sorry, error occured while trying to save the attribute. Error: '.$e->getMessage().'</p>'; }
}

See More here :-http://inchoo.net/magento/programatically-create-attribute-in-magento-useful-for-the-on-the-fly-import-system/

11
  • Thanks for the quick response Arunendra. The code is very similar to the one I am using. Exceptions include adding in "is_filterable" and "is_filterable_in_search" values. Should they really make the difference? Sep 14, 2015 at 9:43
  • The attribute codes (field names) are calculated outside the function and need to be in certain formats. That is one other difference in the code there. Sep 14, 2015 at 9:44
  • I am looking for a "what might be going on" or "it doesn't work because" type answer here. Sep 14, 2015 at 10:03
  • are you getting any error when trying to save it through admin ??
    – Arunendra
    Sep 14, 2015 at 10:08
  • nope nothing, everything else saves fine - just ignores those custom attributes ...until I tweak a parameter in the attribute management - then they work Sep 14, 2015 at 10:09

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.