Skip to content

Instantly share code, notes, and snippets.

@nightbook
Last active August 29, 2015 14:09
Show Gist options
  • Save nightbook/f5ab39cd39751013d547 to your computer and use it in GitHub Desktop.
Save nightbook/f5ab39cd39751013d547 to your computer and use it in GitHub Desktop.
Woocommerce Bootle Deposit
<?php
/**
* WooCommerce Bottle Deposit
* --------------------------
* Add bottle deposit fee to checkout
*/
function woo_add_bottle_deposit_fee($cart_object) {
global $woocommerce;
$deposit = 0;
$count1 = 0;
$count2 = 0;
foreach ( $cart_object->cart_contents as $key => $value ) {
$product_id = $value['product_id'];
$quantity = $value['quantity'];
$product_object = get_product($product_id);
$product_size = intval($product_object->get_attribute('bottle-size'));
if ($product_size) {
if ($product_size <= 1000) {
$deposit += 0.1 * $quantity;
$count1++;
} elseif ($product_size > 1000) {
$deposit += 0.2 * $quantity;
$count2++;
}
}
}
if ($deposit > 0) {
$amount = '';
if ($count1 > 0 || $count2 > 0) {
if ($count1 > 0) { $amount .= ' (<1L @ 10¢) x '.$count1; }
if ($count1 > 0 && $count2) { $amount .= ' | '; }
if ($count2 > 0) { $amount .= '(>1L @ 20¢) x '.$count2; }
}
$woocommerce->cart->add_fee( 'Bottle Deposit'.$amount, $deposit, false, '' );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_bottle_deposit_fee' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment