Akce po vytvoření dokumentu
Po úspěšném vytvoření dokumentu ve Fakturoidu je spuštěna akce, která umožňuje reagovat na tuto událost - například odeslat notifikaci, aktualizovat externí systém nebo provést další operace.
Akce po vytvoření dokumentu
Section titled “Akce po vytvoření dokumentu”do_action( 'wpify_woo_fakturoid_document_created', $document, $order, $wc_order, $document_type );Parametry akce
Section titled “Parametry akce”| Parametr | Typ | Popis |
|---|---|---|
$document | object | Objekt vytvořeného dokumentu z Fakturoidu |
$order | OrderModel | Model objednávky s Fakturoid daty |
$wc_order | WC_Order | WooCommerce objednávka |
$document_type | string | Typ dokumentu: proforma, invoice, correction |
Příklady použití
Section titled “Příklady použití”Logování vytvořených dokumentů
Section titled “Logování vytvořených dokumentů”Pro debugování a sledování vytvářených dokumentů.
/** * Log created Fakturoid documents * * @param object $document Created document * @param OrderModel $order Order model * @param WC_Order $wc_order WC order * @param string $document_type Document type * * @return void */function log_fakturoid_document( $document, $order, $wc_order, $document_type ): void { error_log( sprintf( 'Fakturoid %s #%d created for order #%s', $document_type, $document->id, $wc_order->get_order_number() ) );}add_action( 'wpify_woo_fakturoid_document_created', 'log_fakturoid_document', 10, 4 );Odeslání notifikace po vytvoření faktury
Section titled “Odeslání notifikace po vytvoření faktury”/** * Send notification after invoice creation * * @param object $document Created document * @param OrderModel $order Order model * @param WC_Order $wc_order WC order * @param string $document_type Document type * * @return void */function notify_on_invoice_created( $document, $order, $wc_order, $document_type ): void { // Pouze pro faktury (ne proformy) if ( $document_type !== 'invoice' ) { return; }
// Odeslat email administrátorovi wp_mail( get_option( 'admin_email' ), 'Nová faktura vytvořena', sprintf( 'Pro objednávku #%s byla vytvořena faktura č. %s', $wc_order->get_order_number(), $document->number ) );}add_action( 'wpify_woo_fakturoid_document_created', 'notify_on_invoice_created', 10, 4 );Uložení čísla faktury do custom pole
Section titled “Uložení čísla faktury do custom pole”Plugin již ukládá ID a URL dokumentu do meta dat objednávky. Tento příklad ukazuje, jak uložit další údaje (např. číslo faktury) do vlastního pole.
/** * Save invoice number to custom field * * @param object $document Created document * @param OrderModel $order Order model * @param WC_Order $wc_order WC order * @param string $document_type Document type * * @return void */function save_invoice_number_to_meta( $document, $order, $wc_order, $document_type ): void { if ( $document_type === 'invoice' ) { $wc_order->update_meta_data( '_fakturoid_invoice_number', $document->number ); $wc_order->save(); }}add_action( 'wpify_woo_fakturoid_document_created', 'save_invoice_number_to_meta', 10, 4 );Synchronizace s externím systémem
Section titled “Synchronizace s externím systémem”/** * Sync document to external system * * @param object $document Created document * @param OrderModel $order Order model * @param WC_Order $wc_order WC order * @param string $document_type Document type * * @return void */function sync_to_external_system( $document, $order, $wc_order, $document_type ): void { // Příklad synchronizace s CRM $api_data = array( 'order_id' => $wc_order->get_id(), 'document_type' => $document_type, 'document_id' => $document->id, 'document_number'=> $document->number, 'total' => $document->total, 'created_at' => current_time( 'mysql' ), );
wp_remote_post( 'https://api.your-crm.com/invoices', array( 'body' => wp_json_encode( $api_data ), 'headers' => array( 'Content-Type' => 'application/json', ), ) );}add_action( 'wpify_woo_fakturoid_document_created', 'sync_to_external_system', 10, 4 );Kde umístit kód
Section titled “Kde umístit kód”Vlastní funkci můžete vložit buď do tzv. child šablony nebo pomocí pluginu Code Snippets.