×

 

Home Contact Us Payments SC Commerce SC Commerce Docs SC Commerce Snippets

Simply Computing

We make websites. Simple.

Ask for donation in checkout process

You might be offering free items for download on your site, but suggesting those who do so make a donation. This code adds buttons in the checkout process to allows users to optionally choose a range of donation amounts. In this example the amounts are $2, $5 and $10. You will need to set up these amounts as products and then add in the product IDs.

		/**
 * Ask for donation in checkout process
 */

add_action( 'woocommerce_review_order_before_submit', 'cc_checkout_donation', 9999 );

function cc_checkout_donation() {

  $product_ids = array(223, 224, 225); // IDs for products created for each of the donation amounts eg $2, $5 and $10
  $in_cart = false;
  foreach(WC()->cart->get_cart() as $cart_item) {
    $product_in_cart = $cart_item['product_id'];
    if (in_array( $product_in_cart, $product_ids)) {
      $in_cart = true;
      break;
    }
  }

  if (! $in_cart) {
    echo '<h4>Make a Donation?</h4>';
    echo '<p><a class="button" style="margin-right: 1em; width: auto" href="?add-to-cart=223"> $2 </a><a class="button" style="margin-right: 1em; width: auto" href="?add-to-cart=224"> $5 </a><a class="button" style="width: auto" href="?add-to-cart=225"> $10 </a></p>';
  }
}
	
   View Raw Code ID: 9940