Přeskočit na obsah

Akce po vytvoření dokumentu

Tento obsah zatím není dostupný ve vašem jazyce.

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.

do_action( 'wpify_woo_fakturoid_document_created', $document, $order, $wc_order, $document_type );
ParametrTypPopis
$documentobjectObjekt vytvořeného dokumentu z Fakturoidu
$orderOrderModelModel objednávky s Fakturoid daty
$wc_orderWC_OrderWooCommerce objednávka
$document_typestringTyp dokumentu: proforma, invoice, correction

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 );

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 );
/**
* 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 );

Vlastní funkci můžete vložit buď do tzv. child šablony nebo pomocí pluginu Code Snippets.