Created
November 8, 2022 19:52
-
-
Save JarrydLong/32dbe5ccc4ff2c21a7452aa0aebf4d4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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