Created
January 5, 2024 12:45
-
-
Save webdados/f19c2dfece7cd679bb60a68b975a30ab to your computer and use it in GitHub Desktop.
Get and store the AT Code after Moloni creates a "Bill of Lading" (Guia de Transporte) on WooCommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'moloni_after_close_document', 'moloni_store_at_code' ); | |
/* Store Moloni AT Code - https://wordpress.org/support/topic/codigo-at-no-meta-da-encomenda/ */ | |
function moloni_store_at_code( $document_builder ) { | |
// Only for "Guias de Transporte" | |
if ( $document_builder->documentType != 'billsOfLading' ) { | |
return $document_builder; | |
} | |
$document_id = $document_builder->getDocumentId() ?? 0; | |
if ( $document_id <= 0 ) { | |
return $document_builder; | |
} | |
// Get info from Moloni | |
$args = array( | |
'document_id' => $document_id, | |
); | |
$document = \Moloni\Curl::simple( 'documents/getOne', $args ); | |
if ( empty( $document ) || ! is_array( $document ) ) { | |
return $document_builder; | |
} | |
// Check for AT Code and store it | |
$at_code = $document['transport_code'] ?? ''; // Moloni Live mode | |
//$at_code = $document['doc_unico'] ?? ''; // Moloni Test mode - Because without AT Communication there will never be a 'transport_code' | |
if ( ! empty( $at_code ) ) { | |
$order = wc_get_order( $document_builder->order->get_id() ); | |
$order->update_meta_data( '_moloni_at_code', trim( $at_code ) ); | |
$order->save(); | |
} | |
// Exit | |
return $document_builder; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment