Last active
April 9, 2023 13:11
-
-
Save hedqvist/80fea968695a8604167021b4f6b53aad to your computer and use it in GitHub Desktop.
Fortnox - Personnummer
This file contains hidden or 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 | |
/** | |
* @snippet WooCommerce - Fortnox plugin by Redlight Media - Add custom field to Fortnox via functions.php | |
* @author Redlight Media AB / Christopher Hedqvist | |
* @compatible WooCommerce 3.5.2 | |
*/ | |
function redlight_fortnox_pnr_guest_customer( $customer, $order_id ) { | |
$organisationsnummer = get_post_meta($order_id, 'organisationsnummer', true); | |
if(!empty($organisationsnummer)){ | |
$customer['Customer']['OrganisationNumber'] = $organisationsnummer; | |
$customer['Customer']['Type'] = 'COMPANY'; | |
} | |
return $customer; | |
} | |
add_filter( 'obj_fortnox_guest_customer_data', 'redlight_fortnox_pnr_guest_customer', 10, 2 ); | |
/** | |
* @snippet WooCommerce - Fortnox plugin by Redlight Media - Add custom field to Fortnox via functions.php | |
* @author Redlight Media AB / Christopher Hedqvist | |
* @compatible WooCommerce 3.5.2 | |
*/ | |
function redlight_fortnox_pnr_customer( $customer, $user ) { | |
if(!empty($_POST['organisationsnummer'])){ | |
$customer['Customer']['OrganisationNumber'] = $_POST['organisationsnummer']; | |
$customer['Customer']['Type'] = 'COMPANY'; | |
}elseif(!empty(get_user_meta( $user->ID, 'organisationsnummer',true ))){ | |
$customer['Customer']['OrganisationNumber'] = get_user_meta( $user->ID, 'organisationsnummer',true ); | |
$customer['Customer']['Type'] = 'COMPANY'; | |
}else{ | |
$wc_customer = new WC_Customer($user->ID); | |
$last_order = $wc_customer->get_last_order(); | |
if(!empty($last_order)){ | |
$customer['Customer']['OrganisationNumber'] = $last_order->get_meta('organisationsnummer'); | |
$customer['Customer']['Type'] = 'COMPANY'; | |
} | |
} | |
return $customer; | |
} | |
add_filter( 'obj_fortnox_customer_data', 'redlight_fortnox_pnr_customer', 10, 2 ); |
@TomasAxelsson Well spotted, its fixed now :)
Thanks for the help over phone @hedqvist!
Just as note for other people who uses this example:
Fortnox need "personnummer" and "organisationsnummer" in exactly correct format to import it.
Correct format for "personnummer" is YYYYMMDD-XXXX
Correct format for "organisationsnummer" is XXXXXX-XXXX
So, if things aren't working have a look at the formats.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I think you have a missing ; at row 30.
Keep up your good work!
Cheers
Tomas