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?

2 Answers
2

I would suggest adding a “virtual” coupon to the order which would represent the store credit.

Coupons are applied on the cart page before checkout, this is where I would hook in.

You can use something like the woocommerce_cart_subtotal filter as per my example below:

function royal_woocommerce_filter_checkout_for_coupons( $subtotal, $compound, $cart ) {     

        // Your logic to get store credit value for a user will go here
        $store_credit = 20;

        // We only need to add a store credit coupon if they have store credit
        if($store_credit > 0){

            // Setup our virtual coupon
            $coupon_name="store-credit";
            $coupon = array($coupon_name => $store_credit);

            // Apply the store credit coupon to the cart & update totals
            $cart->applied_coupons = array($coupon_name);
            $cart->set_discount_total($store_credit);
            $cart->set_total( $cart->get_subtotal() - $store_credit);
            $cart->coupon_discount_totals = $coupon;
        }

    return $subtotal; 
}

add_filter( 'woocommerce_cart_subtotal', 'royal_woocommerce_filter_checkout_for_coupons', 10, 3 );

Leave a Reply

Your email address will not be published. Required fields are marked *