Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tommyshellberg/f040888040182208c728121a2fb5cc88 to your computer and use it in GitHub Desktop.
Save tommyshellberg/f040888040182208c728121a2fb5cc88 to your computer and use it in GitHub Desktop.
WooCommerce Xero Override Line Item Account Code By SKU
add_filter('woocommerce_xero_line_item_account_code', 'wc_custom_xero_account_code', 10, 2);
function wc_custom_xero_account_code($account_code, $line_item) {
// $line_item->get_item_code() returns the SKU of the line item.
if( $line_item->get_item_code() == 'ACME') {
return '460';
}
return $account_code;
}
@spigotdesign
Copy link

Hi Tommy - did you get this script to work for you? I'm giving it a go and something here is preventing invoices from being sent to Xero.

@tommyshellberg
Copy link
Author

I'm giving it a go and something here is preventing invoices from being sent to Xero.

I assume that you replaced the '460' code with your own code from Xero? Did you ensure that the account code exists and is active in your Xero account? And that it's linked to a compatible account type(revenue/sales for instance, I don't think it will work with an expense account)?

@spigotdesign
Copy link

Hey thanks for the reply. Yes I've replaced the 460 with our own, including trying the default code set in the plugin settings. The accounts are active and set as sales. The error I'm getting is : ERROR creating Xero invoice: ErrorNumber: 10 ErrorType: ValidationException Message: A validation exception occurred Detail: Account code must be specified
This error occurs even when the if condition is false.

@tommyshellberg
Copy link
Author

Hey thanks for the reply. Yes I've replaced the 460 with our own, including trying the default code set in the plugin settings. The accounts are active and set as sales. The error I'm getting is : ERROR creating Xero invoice: ErrorNumber: 10 ErrorType: ValidationException Message: A validation exception occurred Detail: Account code must be specified
This error occurs even when the if condition is false.

I have tested this on my own site and it's working; most probably your site's Xero plugin settings do not have Send Inventory Items checked. If that is not checked the Xero plugin won't look at the SKU at all and ignore the item code(SKU).

@jasonkkruger
Copy link

Is it possible to use this for multiple products at the same time?

@coopersita
Copy link

coopersita commented Mar 8, 2023

Hi. Is it possible to get the product id? My codes depend on the type of product, so I need to check the product category to assign the correct code. Thanks.

Also, where is that code (ACME) stored? Is it the product SKU or what is it exactly? Maybe I can use that somehow.

@coopersita
Copy link

Sorry. I missed the comment on the code. It's a SKU. Strangely, when I send it to the error log, I get nothing, not the SKU or anything... Maybe their code has changed.

@eludlow
Copy link

eludlow commented Jan 12, 2024

Hi there - looking at adapting this to assign categories of my products to different accounts. Can I just check this is the latest version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment