magento - Adding attribute options programmatically are not available for use immediatly -
I am creating the Magento attribute option through a script, but then I need to get and use the new ID Directly in the same script
This time it is not pulling the ID - if I delete the script and start it again, then it picks up the option and gives the id, but it is not part of the same script .
Here's the code I'm using:
$ attr = Dana :: getModel ('list / product') - & gt; GetResource () - & gt; GetAttribute ($ key); If ($ attr-> uses the source (source)) {$ vattr_id = $ attr- & gt; GetSource () - & gt; GetOptionId ($ value); } And {resonate "not a source"; $ Vattr_id = false; } If ($ vattr_id) {return $ vattr_id; } And ($ attr_model = Mage :: getModel ( 'catalog / resource_eav_attribute'); $ Attr = $ attr_model- & gt; Lodbikod ( 'catalog_product', $ key); $ Attr_id = $ attr- & gt; getAttributeId (); $ Option ['attribute_id'] = $ attr_id; $ option ['value'] [$ value] [0] = $ value; $ option ['value'] [$ value] [1] = $ value; $ setup = new Mage_Eav_Model_Entity_Setup ( 'core_setup'); $ setup & gt; addAttributeOption ($ option); $ Attr = Mage :: getModel ( 'catalog / product') - & gt; getResource () - & gt; getAttribute ($ key); if ($ attr- & gt; uses the source (source)) {$ vattr_id = $ attr- & gt; getSource () - & gt; getOptionId ($ value); echo "AttrID: $ vattr_id";}}
It is running (required ma Ge :: app ()
etc.), makes this option, you can see it at the rear end of the Magento, but $ vattr_id
is NULL
If I reload the script, then the attribute option is found in the first block as it should be.
I think how it is with the model, but it is not certain that I where should
function getAttributeOptionId ($ attributeName, $ AttributeValue ) {$ @ Attribute on Mage_Eav_Mo Del_Entity_Attribute * / $ attribute = Dana :: getModel ("eav / entity_attribute") - & gt; LoadByCode ("catalog_product", $ attributeName); // Check Attribute Code if ($ attribute-> getId ()) {$ source = $ attribute- & gt; GetSource (); $ Option = $ source- & gt; GetAllOptions (); // Existing ID foreign currency ($ $ option $ as option value) {if ($ attributeValue == $ optionValue ["label"]) {return option $ Value ["value"]; }} // The new options made $ addOptionData = array ( "attribute_id" = & gt; $ attribute- & gt; getId (), "value" = & gt; array (array ($ attributeValue)); $ Setup = new Mage_Eav_Model_Entity_Setup ( 'core_setup'); $ setup & gt; addAttributeOption ($ addOptionData); // new ID $ attribute = union: the Milmodel ( "Iavi / Antiti_tribut") - & gt; Lodbikod ( "catalog_product", $ attributeName) ; $ Source = $ attribute- & gt; getSource (); $ options = $ source- & gt; getAllOptions (); Forex ($ option $ option value) {if ($ attributeValue == $ optionValue ["label "]) {Return option $ value [" value "];}}} return tap;} echo getAttributeOptionId (" brand "," Intel ");
Comments
Post a Comment