WooCommerce Integration With Nonprofit Choice
Let customers choose the nonprofit you donate to, then fulfill that donation through Change
This guide will walk through each step of integrating WooCommerce with Change so shoppers can select a nonprofit at checkout.
1. Create a Change account
Sign up for Change, then visit the Developers page. Click the “View test keys” switch to see your sandbox keys. Use these keys while building and testing your Change integration. You’ll need your public and secret keys when you create donations from WooCommerce.
2. Add nonprofit selection at checkout
To add a nonprofit field under Shipping on checkout, use the woocommerce_after_order_notes hook. Then save the customer’s choice on the order with woocommerce_checkout_update_order_meta.
Add each snippet below to your functions.php file.
Add the field to checkout
This inserts the selector under the shipping details on checkout.
/**
* Add the nonprofit field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'nonprofit_field' );
function nonprofit_field( $checkout ) {
echo '<div id="nonprofit"><h2>' . 'Choose a nonprofit' . '</h2>';
woocommerce_form_field( 'nonprofit', array(
'type' => 'select',
'class' => array('form-row-wide'),
'options' => array('Know Your Rights Camp' => 'Know Your Rights Camp', 'Watsi' => 'Watsi', 'Feeding America' => 'Feeding America'),
'label' => 'YOUR COMPANY will donate $3 to the nonprofit of your choice.',
'placeholder' => 'Select...',
), 'Know Your Rights Camp');
echo '</div>';
}Save the nonprofit on the order
When the customer places the order, persist the selected nonprofit so you can use it when creating the donation.
/**
* Update the order meta with nonprofit value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'nonprofit_update_order_meta' );
function nonprofit_update_order_meta( $order_id ) {
if ( empty( $_POST['nonprofit'] ) ) {
return;
}
// Get these Nonprofit ids from https://api.getchange.io/nonprofits.
$nonprofit_ids = array(
'Know Your Rights Camp' => 'n_XI19548HjWhTUzRUYwAQoBqt',
'Watsi' => 'n_IfEoPCaPqVsFAUI5xl0CBUOx',
'Feeding America' => 'n_ykwxyhbBfPOSnGxh6EdAq2iK',
);
$nonprofit = sanitize_text_field( $_POST['nonprofit'] );
$nonprofit_id = $nonprofit_ids[$nonprofit];
if ( empty( $nonprofit_id ) ) {
// The nonprofit is unknown. Use 'Know Your Rights Camp' as a default.
$nonprofit_id = 'n_XI19548HjWhTUzRUYwAQoBqt';
}
$order = wc_get_order( $order_id );
$order->update_meta_data( 'nonprofit_id', $nonprofit_id );
$order->save();
}3. Create a donation when payment completes
To create a donation after the customer pays, use the woocommerce_payment_complete hook. That runs whenever a payment is completed for an order.
Add the following code to your functions.php file:
Fill in your details
Make sure to fill in the TODO details in this code snippet.
/**
* Make a donation once payment is completed
*/
add_action( 'woocommerce_payment_complete', 'make_donation' );
function make_donation( $order_id ) {
// TODO: Configure these options.
// `amount` The amount of the donation in cents.
// `public_key` Get this from https://api.getchange.io. Test key starts with pk_test, Prod key starts with pk_live.
// `secret_key` Get this from https://api.getchange.io. Test key starts with sk_test, Prod key starts with sk_live.
$amount = 500;
$public_key = '';
$secret_key = '';
// Get the nonprofit
$order = wc_get_order( $order_id );
$nonprofit_id = $order->get_meta( 'nonprofit_id' );
// Prepare the donation
$url = 'https://api.getchange.io/api/v1/donations';
$data = array('amount' => $amount, 'nonprofit_id' => $nonprofit_id, 'funds_collected' => false);
$options = array(
'http' => array(
'header' => array("Content-type: application/json", "Authorization: Basic " . base64_encode($public_key . ":" . $secret_key)),
'method' => 'POST',
'content' => json_encode($data)
)
);
// Send the donation
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
// Save the donation_id on the Order
$donation_id = json_decode($result, true)['id'];
$order->update_meta_data( 'donation_id', $donation_id );
$order->save();
}Conclusion
You now have a path from checkout to donation: the customer’s nonprofit choice is stored on the order, and when payment completes, WooCommerce calls the Change API to create the donation and saves the returned donation_id on the order for your own reporting or follow-up.
If you add or change nonprofits in the checkout dropdown, update both the labels in nonprofit_field and the corresponding IDs in nonprofit_update_order_meta so they always match.