I am adding a custom fee to the cart by hooking into the woocommerce_cart_calculate_fees
action like so:
if (isset($_GET['discount'])) {
add_action('woocommerce_cart_calculate_fees', 'add_loyalty_discounts');
}
function add_loyalty_discounts(WC_Cart $cart)
{
$total = 0;
global $woocommerce;
foreach (WC()->cart->get_cart() as $cart_item_key => $item) {
$total += $item['data']->regular_price;
$woocommerce->cart->cart_contents[$cart_item_key]['discount_amount'] = $_GET['discount'];
}
if ($total < $_GET['discount']) {
wc_add_notice('You must spend more than €' . $_GET['discount'] . ' to use your loyalty discount.', 'error');
} else {
$current_user = wp_get_current_user();
$loyalty_points = empty(get_user_meta($current_user->ID, 'loyalty_points', true)) ? 0 : get_user_meta($current_user->ID, 'loyalty_points', true);
$loyalty_points = 100;
if ($loyalty_points < $_GET['discount']) {
wc_add_notice('You do not have enough loyalty points to apply this discount.', 'error');
} else {
$cart->add_fee('Loyalty Discount', -intval($_GET['discount']), true);
}
}
}
The fee appears on the cart page but not persist onto the checkout and order pages.
I've tried using $cart->set_session()
but this had no effect.
Update: I have created a fresh WP site with a blank theme (blankslate), and only the latest version of WooCommerce installed and I still get the error.