I'm writing an import module to import configurable products into magento, which works quite fine. I've tweaked the import so that it can create all necessary attribute sets, attributes and attribute options needed for creating the configurable products. So far everything works ... quite everything.
When the import creates a new attribute, it can not create the configurable product. When I edit the new attribute in the backend and save it without changes, a message appears which tells me to update some indexes. After I have updated the product flat data index, I can run the import again and everything works fine.
I've tried to ways to create a new attribute:
$setup = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
$setup->addAttribute(
$this->getEntityTypeId(),
$code,
array(
'attribute_code' => $code,
'label' => ucfirst($code),
//'group' => $attributeSet->getId(),
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'input' => 'select',
'unique' => 0,
'required' => 0,
'configurable' => 1,
'filterable' => 1,
'visible_on_front' => 1,
'used_in_product_listing' => 1,
'frontend_label' => array(
$code
)
)
);
The other way is:
$attribute = Mage::getModel("catalog/resource_eav_attribute");
$attribute->addData(
array(
'entity_type_id' => $this->getEntityTypeId(),
'attribute_code' => $code,
'label' => ucfirst($code),
//'group' => $attributeSet->getId(),
'is_user_defined' => 1,
'is_global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'frontend_input' => 'select',
'is_unique' => 0,
'is_required' => 0,
'is_configurable' => 1,
'is_filterable' => 1,
'is_visible_on_front' => 1,
'is_used_in_product_listing'=> 1,
'frontend_label' => array(
$code
)
)
);
$attribute->save();
Both codes create the attribute well but I can't use it to create configurable Attributes. I've tried to manually run the index scripts but this does not help me.
What am I doing wrong? Is creating new attributes somehow the black magic of magento? :-D
I forgot to set the backend type for the new attribute to "int" so it was automatically set to static (which means magento looks for it in the entity table).
The other thing is that i couldn't set the attribute value like this:
$model->setData("newattributecode", 123);
Magento just didn't save the attribute. Instead I had to use the undocumented function Mage_Catalog_Model_Product::addAttributeUpdate() to save the attribute value of this model:
$model->addAttributeUpdate("newattributecode", 123, 0);
Related
I am using Magmi datapump to import some products into Magento.
So far i have this:
require_once("../../magmi/inc/magmi_defs.php");
require_once("../../magmi/integration/inc/magmi_datapump.php");
$dp = Magmi_DataPumpFactory::getDataPumpInstance("productimport");
$dp->beginImportSession("Default","create");
$newProductData = array(
'type' => 'simple',
'sku' => "test-simple",
'qty' => 1000,
'color' => 'Brown',
'price' => 10,
'name' => 'test simple',
'tax_class_id' => 1,
'is_in_stock' => 1,
'store' => 'admin',
'economic_productgroup' => 700,
'image' => '+http://blogs.smh.com.au/entertainment/getflickd/44655_native.jpeg.jpg',
'small_image' => 'http://blogs.smh.com.au/entertainment/getflickd/44655_native.jpeg.jpg',
'thumbnail' => 'http://blogs.smh.com.au/entertainment/getflickd/44655_native.jpeg.jpg'
);
$dp->ingest($newProductData);
$dp->endImportSession();
All attributes are getting imported correctly, except image. This does not work.
This is screenshot from "Default" profile.
Magmi is installed here: http://www.example.com/import/magmi/web/magmi.php
Why are import of my images not working?
Problem solved by creating new profile in webinterface with exact same settings as i had in "default" profile. Not sure why though.
After I set the 'foreign_sortby' => 'datum'; this field is missing now at my edit mask. Does anybody know why?
TCA of tx_veranstaltungen_domain_model_terminblock (1 terminblock can have multiple termine)
'termine' => array(
'exclude' => 1,
'label' => 'LLL:EXT:veranstaltungen/Resources/Private/Language/locallang_db.xlf:tx_veranstaltungen_domain_model_terminblock.termine',
'config' => array(
'type' => 'inline',
'foreign_table' => 'tx_veranstaltungen_domain_model_termin',
'foreign_field' => 'terminblock',
'foreign_sortby' => 'datum',
'maxitems' => 9999,
'appearance' => array(
'collapseAll' => 0,
'levelLinksPosition' => 'top',
'showSynchronizationLink' => 1,
'showPossibleLocalizationRecords' => 1,
'useSortable' => 1,
'showAllLocalizationLink' => 1
),
),
),
Okay I found it out whats the problem, after reading Bug report
You should use foreign_default_sortby instead of foreign_sortby, because IRRE hides the field you use for sorting. The dangerous thing is, that the field could be overwritten by "sorting values". So I wondered why all my fields (of type date (timestamp)) has the value 01.01.1970... because the field values changed to 1,2,....
I hope this can help others, before they destroy their data. :)
(I'm using Typo3 7.6.14)
I've written a module that adds an attribute to the customer model, and it's worked fine.
I wanted to extend the sales/order_shipment model and add another attribute, so I just used the same reference that I had used for the installer for adding the new customer attribute.
I relied on Magento finding and installing the module a file with the following path and name app/code/local/[namespace]/[module_name]/sql/install-01.php
I've set the installer to the value of $this during the setup of the module initially:
$installer = $this;
$installer->startSetup();
I kept the same reference for the installer and just added a new field:
$installer->addAttribute('customer','sap_bp_id',
array(
'type' => 'varchar',
'label' => 'SAP Business One Business Partner ID',
'input' => 'text',
'required' => false,
'visible' => true,
'visible_on_front' => false,
'global' => 1,
'position' => 62,
)
);
$installer->addAttribute('shipment','sap_doc_num',
array(
'type' => 'varchar',
'label' => 'SAP Business One Document Number',
'input' => 'text',
'required' => false,
'visible' => true,
'visible_on_front' => false,
'global' => 1,
'position' => 62,
)
);
It's worked just fine, but I want to know this:
Is this the correct method for adding a new attribute for different models in the same installation file? Is there a better method? Have I missed anything?
I created an install script to add two fields to customer using the script below.
But I get this error.
Source model "" not found for attribute "dob_month"
Am I not defining the model in the first line? What does that actually do? What is the best way to fix this?
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('customer', 'dob_month', array(
'label' => 'Month',
'type' => 'varchar',
'input' => 'dropdown',
'visible' => true,
'required' => true,
'position' => 1,
'option' => array (
'value' => array (
'optionone' => array('January'),
'optiontwo' => array('February'),
'optionthree' => array('March'),
'optionfour' => array('April'),
'optionfive' => array('May'),
'optionsix' => array('June'),
'optionseven' => array('July'),
'optioneight' => array('August'),
'optionnine' => array('September'),
'optionten' => array('October'),
'optioneleven' => array('November'),
'optiontwelve' => array('December')
)
)
));
$setup->addAttribute('customer', 'dob_year', array (
'label' => 'Year',
'type' => 'varchar',
'input' => 'text',
'visible' => true,
'required' => true,
'position' => 1
));
If you've already added the attribute, you'll want to use updateAttribute to set the source model value in the eav_attribute table.
<?php
$installer = Mage::getResourceModel('customer/setup','default_setup');
/***
* When working with EAV entities it's important to use their module's setup class.
* See Mage_Customer_Model_Resource_Setup::_prepareValues() to understand why.
*/
$installer->startSetup();
$installer->updateAttribute(
'customer',
'dob_month',
'source_model', //a.o.t. 'source'
'whatever/source_model',
)
$installer->endSetup();
If not, then you can use addAttribute(), which - due to the Mage_Eav setup class' _prepareValues() method - requires an alias for the source_model column as indicated in Alexei's answer ('source' as opposed to 'source_model').
Source model is used when Magento needs to know possible values of your attribute. For example, when rendering dropdown for your attribute. So no, you're not defining it. If my memory doesn't fail me, you can do that by adding 'source' to attribute definition array. Something like:
...
'source' => 'eav/entity_attribute_source_table'
...
That will mean that all your possible options are stored in tables eav_attribute_option and eav_attribute_option. So if your options from install script are successfully added to these tables, that should work. Or you can write your own source model, which I prefer more.
I am trying to add a custom input field to the account information tab of a customer in admin. i have successfully been able to create a custom attribute in the eav table for my input, but have been unsuccessful in finding how to make my input show up. curious of anyone has any good resources on how to do this?
The above link doesn't work anymore. I found a better explanation at http://www.excellencemagentoblog.com/customer-registration-fields-magento1-6 . If you just do the first steps you will only have the added fields in the admin.
The fastest way is to create a php file and access it through browser add the following content to file.
define('MAGENTO', realpath(dirname(__FILE__)));
ini_set('memory_limit', '32M');
set_time_limit (0);
require_once MAGENTO . '/../app/Mage.php';
Mage::app();
$newFields = array(
'custom_attribute' => array(
'type' => 'text',
'label' => 'Customer Custom Attribute'
)
);
$entities = array('customer');
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
foreach($newFields as $attributeName => $attributeDefs) {
foreach ($entities as $entity) {
$setup->addAttribute($entity, $attributeName, array(
'position' => 1,
'type' => $attributeDefs['type'],
'label' => $attributeDefs['label'],
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'searchable' => 0,
'filterable' => 0,
'comparable' => 0,
'visible_on_front' => 1,
'visible_in_advanced_search' => 0,
'unique' => 0,
'is_configurable' => 0,
'position' => 1,
));
}
}
You have to tell the attribute which forms it's used in:
Mage::getModel('customer/attribute')
->loadByCode('customer', 'your_attrib_code')
->setUsedInForms(array('adminhtml_customer'))
->save();
This can be put in a standard Magento upgrade script for convenience (usually the same script that originally created the attribute, or the one right after it).