How to add a random unique code before checkout?

Droger99

Regular Member
Joined
Sep 12, 2017
Messages
206
Reaction score
45
Helo guys,

Hope y'all doing all right!

I am building an e-commerce site using wordpress+woocomerce.
I was wondering if anyone know how to add a random unique code before checkout..

For example, if somebody has a subtotal amount of $30,00.
I would like to add some random numbers in their subtotals, $30,23 for example (23 is the random number).

Please, tell me any possible way to do this.. I appreciate any comment :D

Thanks!
 
You can use the woocommerce_calculate_totals hook to modify cart totals. Your code should look something like this:

PHP:
add_action( 'woocommerce_calculate_totals', 'my_woocommerce_calculate_totals' );
function my_woocommerce_calculate_totals( $cart ) {
    if( $cart->subtotal > 30 ) {
        $cart->subtotal += 0.23;
    }
}

Using the $cart object you can access to everything (tax, products, discounts, ...).
 
What's the goal? Literally just take a few extra cents per purchase & hope nobody cares? I guess that'd add up overtime.
 
Back
Top