0

I am having some trouble with Magento Observers. What I am trying to do is: as I am saving a custom product in my adminhtml form I want to my the product sku with a 3rd party API sku. To do this I created an observer for the save action (savePreDispatch) , able to get the retrieve the SKU from the third party API but now my concern is : how can save to my collection the retrieved SKU in the observer?

I have tried something like $this = $observer -> getEvent() -> getProduct() and then save the SKU but it doesn't appear to work, any ideas?

my config.xml is below

<?xml version="1.0"?>
<config>
<modules>
    <Namespace_Module>
        <version>1.0.3</version>
    </Namespace_Module>
</modules>
<global>
    <resources>
        <namespace_module_setup>
            <setup>
                <module>Namespace_Module</module>
                <class>Namespace_Module_Model_Resource_Setup</class>
            </setup>
        </namespace_module_setup>
    </resources>
    <blocks>
        <namespace_module>
            <class>Namespace_Module_Block</class>
        </namespace_module>
    </blocks>
    <helpers>
        <namespace_module>
            <class>Namespace_Module_Helper</class>
        </namespace_module>
    </helpers>
    <models>
        <namespace_module>
            <class>Namespace_Module_Model</class>
            <resourceModel>namespace_module_resource</resourceModel>
        </namespace_module>
        <namespace_module_resource>
            <class>Namespace_Module_Model_Resource</class>
            <entities>
                <product>
                    <table>namespace_module_product</table>
                </product>
            </entities>
        </namespace_module_resource>
    </models>
</global>
<adminhtml>
  <events>
    <catalog_product_save_before>
      <observers>
        <namespace_module>
          <type>singleton</type>
            <class>namespace_module/observer</class>
            <method>catalogProductSaveBefore</method>
        </namespace_module>
      </observers>
    </catalog_product_save_before>
  </events>    
</adminhtml>
</config>
1

Do notuse $this, an observer runs outside the product model. I do not suggest to use the predispatch action too, use the model save events.

Use catalog_product_save_before event in adminhtml scope.

...
<adminhtml>
...
    <events>
        <catalog_product_save_before>
            <observers>
                <my_observer>
                    <type>singleton</type>
                    <class>my_module/observer</class>
                    <method>catalogProductSaveBefore</method>
                </my_observer>
            </observers>
        </catalog_product_save_before>
        ...
    </events>
...
</adminhtml>
...

And do something like this:

...
$product = $observer->getEvent()->getProduct();
...
$product->setSku($newSku);
...
  • Grazie @RiccardoT for the answer , however as far as I understand from magento the event catalog_product_save_before in the adminhtml fires up when I add a new product in the adminhtml right? However I want to fire up the same event but for my own module and for my products which are somehow separated entites, but I am having troubles in finding the right route for it, could you guide me ? Should it be something like <model_product_save_before> ? Thank you – Dan Jul 8 '16 at 7:33
  • Can you please add more details? – Phoenix128_RiccardoT Jul 8 '16 at 7:34
  • <catalog_product_save_before> <observers> <namespace_module> <type>singleton</type> <class>namespace_module/observer</class> <method>catalogProductSaveBefore</method> </namespace_module> </observers> </catalog_product_save_before> I have tried this code following your example but the event never fires up so I'm guessing it's some routes problem I'm missing out – Dan Jul 8 '16 at 10:04
  • Routes are not involved here, it should work at model level. Did you configure the "model" class path on your module? – Phoenix128_RiccardoT Jul 8 '16 at 10:16
  • yeah I am pretty sure the Model class is in place – Dan Jul 8 '16 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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