Freelance Expert Technique Magento - Magento : Observer

CONTACT
RSS

Magento : Observer

Nous allons ici voir comment créer un Observer sous Magento.

A quoi sert donc un Observer ? Et bien c’est très simple ! Vous avez peut ĂŞtre dĂ©jĂ  remarquĂ© que Magento “diffuse” des évènements au cours des diffĂ©rentes actions que vous pouvez rĂ©aliser au sein du code. Cette mĂŞme diffusion peut ĂŞtre utilisĂ©e afin de lancer l’exĂ©cution d’un script spĂ©cifique. Dans cet exemple, nous allons crĂ©er un observer sur la validation (le paiement) d’une commande.
( Une liste relativement exhaustive des différents évènements Magento est disponible en fin de post).

Tout d’abord, nous devons implĂ©menter une petite config au sein du config.xml du module qui aura vocation Ă  contenir le code exĂ©cutĂ©. Il s’agit en fait de dire Ă  Magento “Quand cet Ă©vènements se dĂ©clenchera, tu exĂ©cuteras le code suivant”.

<?xml version="1.0"?>
<config>
<!-- ..... -->
<global>
 <events>
   <sales_order_invoice_pay>
	<observers>
    	    <Charlous_Module_Observer>
		   <type>singleton</type>
		   <class>Charlous_Flux_Model_Observer</class>
		   <method>export</method>
 	    </Charlous_Flux_Observer>
	</observers>
    </sales_order_invoice_pay>
  </events>
  <!-- ... -->
 </global>
</config>

Cette configuration se fait donc entre les balises global puis events. Vous ouvrez une entrĂ©e au sein de laquelle vous allez dĂ©clarer le ou les Ă©vènements que vous souhaitez utiliser comme “Ă©couteur”. L’évènement que j’utilise ici est sales_order_invoice_pay, il s’agit donc comme son nom l’indique, du moment oĂą la commande est payĂ©e.

Comme vous pouvez le constater, lors de l’activation de l’évènement sales_order_invoice_pay, mon module “Charlous/Flux” sera appelĂ© pour exĂ©cuter la mĂ©thode “export” de mon Observer se trouvant dans app/code/local/Charlous/Flux/Model/Observer.php

Il ne nous reste plus qu’Ă  crĂ©er notre model Observer.php

Nous créons donc notre Observer.php dans le répertoire Model de notre module Magento, qui contient le code suivant :

class Charlous_Flux_Model_Observer extends Varien_Event_Observer{
	public function __construct(){}

	public function export($observer){
		/*Cette fonction sera appelé lorsqu'une commande sera payée*/
               die('ça marche !');
	}

Et voilà, ça marche :D
Enfin ça plante plutĂ´t, bah oui avec un die() après une commande, il n’y a pas grand intĂ©rĂŞt …
Bon sinon, je vous file ici un module qui reprend ce mĂŞme code, ça pourra ainsi vous servir d’exemple :)
(Contenu Ă  dĂ©compresser et Ă  copier Ă  la racine de l’apply Magento, comme n’importe quel module)

Voilà, maintenant vous avez compris comment implémenter un Observer :)
Pour vous aider, je vous donne ci-dessous une liste plus ou moins complète des différents événements :

Controller Routing Events

controller_action_layout_render_before_'.$this->getFullActionName
controller_action_layout_render_before
controller_action_nocookies
controller_action_noroute
controller_action_postdispatch
controller_action_predispatch
controller_front_init_before
controller_front_init_routers

Generic Object Saving Events

$this->_eventPrefix.'_add_is_active_filter
$this->_eventPrefix.'_delete_after
$this->_eventPrefix.'_delete_after_done
$this->_eventPrefix.'_delete_before
$this->_eventPrefix.'_delete_commit_after
$this->_eventPrefix . '_load_after
$this->_eventPrefix.'_load_after
$this->_eventPrefix . '_load_before
$this->_eventPrefix.'_move_after
$this->_eventPrefix.'_move_before
$this->_eventPrefix.'_'.$process.'_process_run_after
$this->_eventPrefix.'_'.$process.'_process_run_before
$this->_eventPrefix.'_save_after
$this->_eventPrefix.'_save_before
$this->_eventPrefix.'_save_commit_after
$this->_eventPrefix.'_validate_after
$this->_eventPrefix.'_validate_before

All Other Events

adminhtml_banner_edit_tab_content_before_prepare_form
adminhtml_block_eav_attribute_edit_form_init
adminhtml_block_html_before
adminhtml_catalog_category_tabs
adminhtml_catalog_category_tree_is_moveable
adminhtml_catalog_product_attribute_set_main_html_before
adminhtml_catalog_product_attribute_set_toolbar_main_html_before
adminhtml_catalog_product_edit_element_types
adminhtml_catalog_product_edit_prepare_form
adminhtml_catalog_product_edit_tab_attributes_create_html_before
adminhtml_catalog_product_form_prepare_excluded_field_list
adminhtml_cms_page_edit_tab_content_prepare_form
adminhtml_cms_page_edit_tab_design_prepare_form
adminhtml_cms_page_edit_tab_main_prepare_form
adminhtml_cms_page_edit_tab_meta_prepare_form
adminhtml_cmspage_on_delete
adminhtml_controller_action_predispatch_start
adminhtml_controller_catalogrule_prepare_save
adminhtml_controller_salesrule_prepare_save
adminhtml_customer_prepare_save
adminhtml_customer_save_after
adminhtml_product_attribute_types
adminhtml_sales_order_create_process_data
adminhtml_sales_order_creditmemo_register_before
adminhtml_staging_backup_edit_tab_rollback_after_prepare_columns
adminhtml_widget_container_html_before
adminhtml_widget_grid_filter_collection
admin_permissions_role_prepare_save
admin_session_user_login_failed
admin_session_user_login_success
admin_system_config_changed_section_{$section
admin_user_authenticate_after
api_user_authenticated
application_clean_cache
bundle_product_view_config
catalog_block_product_list_collection
catalog_category_change_products
catalog_category_prepare_save
catalog_category_tree_init_inactive_category_ids
catalog_category_tree_move_after
catalog_category_tree_move_before
catalog_controller_category_delete
catalog_controller_category_init_after
catalog_controller_category_init_before
catalog_controller_product_delete
catalog_controller_product_init
catalog_controller_product_init_after
catalog_controller_product_init_before
catalog_controller_product_view
catalog_helper_output_construct
catalogindex_get_minimal_price
catalogindex_plain_reindex_after
catalogindex_prepare_price_select
catalog_model_product_duplicate
catalog_prepare_price_select
catalog_product_collection_apply_limitations_after
catalog_product_collection_before_add_count_to_categories
catalog_product_collection_load_after
catalog_product_collection_load_before
catalog_product_compare_add_product
catalog_product_compare_item_collection_clear
catalog_product_compare_remove_product
catalog_product_edit_action
catalog_product_flat_prepare_columns
catalog_product_flat_prepare_indexes
catalog_product_flat_rebuild
catalog_product_flat_update_product
catalog_product_get_final_price
catalog_product_import_after
catalog_product_is_salable_after
catalog_product_is_salable_before
catalog_product_new_action
catalog_product_prepare_index_select
catalog_product_prepare_save
catalog_product_status_update
catalog_product_to_website_change
catalog_product_upsell
catalog_product_view_config
catalog_product_website_update
catalog_product_website_update_before
catalogrule_after_apply
catalogrule_before_apply
catalogsearch_reset_search_result
category_move
checkout_allow_guest
checkout_cart_add_product_complete
checkout_cart_info_item_unset_product_before
checkout_cart_product_add_after
checkout_cart_save_after
checkout_cart_update_items_after
checkout_cart_update_items_before
checkout_controller_onepage_save_shipping_method
checkout_multishipping_controller_success_action
checkout_onepage_controller_success_action
checkout_quote_destroy
checkout_quote_init
checkout_type_multishipping_set_shipping_items
checkout_type_onepage_save_order
checkout_type_onepage_save_order_after
cms_controller_router_match_before
cms_page_get_available_statuses
cms_page_prepare_save
cms_wysiwyg_config_prepare
core_block_abstract_prepare_layout_after
core_block_abstract_prepare_layout_before
core_block_abstract_to_html_after
core_block_abstract_to_html_before
core_collection_abstract_load_after
core_collection_abstract_load_before
core_layout_update_updates_get_after
core_locale_set_locale
customer_customer_authenticated
customer_login
customer_logout
customer_registration_is_allowed
customer_session_init
eav_attribute_get_backend_type_by_input
eav_attribute_get_default_value_by_input
eav_collection_abstract_load_before
enterprise_catalog_permissions_is_allowed_category
enterprise_giftcardaccount_add
enterprise_giftcardaccount_charge
enterprise_giftcardaccount_create
enterprise_staging_controller_staging_delete
http_response_send_before
log_log_clean_after
log_log_clean_before
log_visitor_collection_load_before
model_delete_after
model_delete_before
model_delete_commit_after
model_load_after
model_save_after
model_save_before
model_save_commit_after
on_enterprise_staging_merge
on_enterprise_staging_save
on_view_report
payment_method_is_active
prepare_catalog_product_index_select
prepare_catalog_product_price_index_table
resource_get_tablename
review_controller_product_init
review_controller_product_init_after
review_controller_product_init_before
review_review_collection_load_before
rss_catalog_category_xml_callback
rss_catalog_new_xml_callback
rss_catalog_special_xml_callback
rss_catalog_tagged_item_xml_callback
rss_order_new_collection_select
rule_environment_collect
sales_convert_order_item_to_quote_item
sales_convert_order_to_quote
sales_convert_quote_address_to_order
sales_convert_quote_address_to_order_address
sales_convert_quote_item_to_order_item
sales_convert_quote_payment_to_order_payment
sales_convert_quote_to_order
sales_order_creditmemo_cancel
sales_order_creditmemo_refund
sales_order_invoice_cancel
sales_order_invoice_pay
sales_order_item_cancel
sales_order_payment_cancel
sales_order_payment_cancel_creditmemo
sales_order_payment_cancel_invoice
sales_order_payment_capture
sales_order_payment_pay
sales_order_payment_place_end
sales_order_payment_place_start
sales_order_payment_refund
sales_order_payment_void
sales_order_place_after
sales_order_place_before
sales_quote_add_item
sales_quote_address_discount_item
sales_quote_item_collection_products_after_load
sales_quote_item_qty_set_after
sales_quote_item_set_product
sales_quote_remove_item
salesrule_rule_condition_combine
salesrule_validator_process
sales_sale_collection_query_before
sendfriend_product
staging_edit_action
staging_website_create_after
store_delete
store_group_save
tag_tag_product_collection_load_after
tax_rate_data_fetch
tax_settings_change_after
websiterestriction_frontend
wishlist_add_product
wishlist_items_renewed
wishlist_share
CATÉGORIES :

Magento

, Php

, Tutoriels

    • zied
    • August 7th, 2011

    merci pour le tuto, mais ça ne marche pas chez moi

  1. No trackbacks yet.