Implementing Discount Feature with Usage Limitation for Logged-In Users in wordpress

20 Views Asked by At

Discount Feature Implementation: I'm currently working on implementing a discount feature on my website. I want to provide users with a discount, but with a limitation: each logged-in user should only be able to avail the discount three times.

Potential Solution: To achieve this, I'm considering adding a column to the user database to track the usage of the discount. This column would serve as a tracker, preventing users from accessing the discount after it has been used three times. I've drafted a code snippet to apply the discount, but I'm uncertain about the potential impact of modifying the user database.

add_action('woocommerce_cart_calculate_fees', 'custom_discount_for_logged_in_users', 10, 1);

function custom_discount_for_logged_in_users($cart) {
    $target_product_id = 848;
    
    if (is_user_logged_in() && in_array($target_product_id, array_column($cart->get_cart(), 'product_id'))) {
        $discount = $cart->subtotal * 0.15;
        $cart->add_fee('15% Discount', -$discount, true);
    }
}

Concerns: I'm unsure whether adding this column to the user database could potentially affect the functionality of my website. Could modifying the user database impact performance or cause any unforeseen issues? Any suggestions or insights on how to approach this challenge effectively would be greatly appreciated!

0

There are 0 best solutions below