Automatically Close Donations When Fundraising Goal is Reached

In Donation Platform for WooCommerce, donation products can continue to accept contributions even after reaching the specified goal. This is to ensure a seamless experience for donors and avoid scenarios where donations are cut off prematurely, which can happen due to timing overlaps or multiple users adding the same donation product to their cart. For example, if only $500 is needed to reach the goal, but two donors simultaneously add $100 and $450 donations to their carts, the goal may be exceeded before the donations are processed.

If you’d like to control donations more strictly once the goal is met, you have two options:

1. Manually Marking the Donation Product as Out of Stock

Once the donation goal is reached, you can manually set the product status to “Out of Stock” in WooCommerce. This will prevent any further donations from being made to that specific product.

2. Automatically Marking the Donation Product as Out of Stock

To automate this process, you can use the following code snippet (e.g. by using Code Snippets Plugin) to set the donation product as out of stock once the goal has been reached:

add_action('wcdp_goal_product_status', function($revenue, $goal_db, $product_id) {
    if ($revenue < $goal_db) return;

    // Set the product status to "sold out"
    $product = wc_get_product($product_id);
	if (!$product) return;

    $product->set_stock_status('outofstock');
    $product->save();
}, 10, 3);

How the Code Works

This code checks if the total donations ($revenue) have met or exceeded the goal amount ($goal_db). If they have, the product is marked as “out of stock,” automatically preventing further donations. This setup provides a convenient way to manage your product’s stock status based on the progress toward the donation goal.

Important Considerations

  • Pending Payments: The progress bar for your donation goal is only updated after a donation is completed. Payments that are pending or awaiting confirmation may still be processed and contribute to the total, which could result in donations slightly exceeding the goal.
  • Larger Donations than Remaining Amount: Some donors may contribute amounts larger than what’s needed to reach the goal, especially if they are unaware of the remaining balance. This can lead to exceeding the goal, particularly if large contributions are made close to the target.

By implementing these options, you can effectively manage donations once a goal is achieved, ensuring transparency with donors while retaining flexibility for your campaign needs.