Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JarrydLong/32dbe5ccc4ff2c21a7452aa0aebf4d4c to your computer and use it in GitHub Desktop.
Save JarrydLong/32dbe5ccc4ff2c21a7452aa0aebf4d4c to your computer and use it in GitHub Desktop.
<?php //do not copy
/**
* This code snippet checks if there's more than one item in the cart,
* finds the cheapest product and discounts it from the cart's total.
*/
function buyone_getone_free_discount() {
$contents = edd_get_cart_contents();
$cart_items = array();
if( $contents ) {
foreach( $contents as $item ) {
$download = new EDD_Download( $item['id'] );
$price = $download->get_price();
$cart_items[$download->post_title] = $price;
}
}
asort( $cart_items );
$lowest_price = floatval( reset( $cart_items ) );
$cart_names = array_keys( $cart_items );
$amount = $lowest_price * -1;
EDD()->fees->remove_fee( 'bogofree_sale' );
if( count( $cart_items ) >= 2 && ! EDD()->fees->has_fees() ) {
EDD()->fees->add_fee( $amount, 'Buy One Get One Free Sale ('.$cart_names[0].')', 'bogofree_sale' );
} else {
EDD()->fees->remove_fee( 'bogofree_sale' );
}
}
add_action( 'init', 'buyone_getone_free_discount', 9999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment