I am coding a WordPress plugin that goes on top of WooCommerce. This plugin basically allows WooCommerce to allocate store credits to a logged-in customer whenever he makes an order later marked as Completed
after payment. These store credits can then be used to offset the cost of a future order that the customer makes. As an example, if I buy $10 worth of items, I might get a $1 store credit. I can use any amount of my store credit to offset future purchases, e.g. I might choose to use $0.50 of my credits to offset my next order by $0.50.
I have had no problem finding the hooks to use in WP-Admin to allocate customers their store credits, but I'm having trouble adding hooks to the front-end to allow customers to use their store credits in an order.
Currently, I have the following function hooked to the woocommerce_checkout_order_processed
action, to record how much store credit the customer is intending to use on an order:
function woocommerce_checkout_order_processed($order_id) {
$offset_amt = floatval($_POST['use-store-credit']);
// This line is a glorified update_post_meta call.
Helpers\Order::set_offset_cost_amount($order_id,$offset_amt);
}
But I think saving it as a meta key seperate from the workings of WP_Order
or WP_Cart
is a bad idea, because once the order is submitted, I will have to hook onto every page the order is rendered (including the pages in WP-Admin) and manually modify the order total, because the WP_Order
will record the total without the store credit offset.
I think hooking this store credit offset as a discount or as a negative fee would be better, but I don't know which hooks to use or where in the process I should do that. Should I hook it on the cart before checkout, or should I hook it onto the order after checkout?