skip to Main Content

I’m seeking assistance on how to modify the existing code to achieve the following:

Apply a 15% discount only for the first 3 instances of a specific product (ID: 848) for each logged-in user.

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);
    }
}

2

Answers


  1. This should work for your code. It’s untested, but I’ve added a user_meta to store the number of times a user received the discount. It then checks to make sure it can do the discount.

    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' ), true ) ) {
            $user_id        = get_current_user_id();
            $first_three    = get_user_meta( $user_id, 'first_three_discount', true );
            $number_applied = ( ! empty( $first_three ) ) ? absint( $first_three ) : 0;
            if ( 3 > $number_applied ) {
                $discount = $cart->subtotal * 0.15;
                $cart->add_fee( '15% Discount', -$discount, true );
                $number_applied++;
                update_user_meta( $user_id, 'first_three_discount', $number_applied );
            }
        }
    }
    
    Login or Signup to reply.
  2. The following code will register the count of purchased product ID 848 as user metadata. From there, it will be possible to make a discount only on First 3 purchases of the product ID 848.

    add_action( 'woocommerce_checkout_update_customer', 'save_customer_custom_metadata' );
    function save_customer_custom_metadata( $customer ) {
        if ( ! is_user_logged_in() ) return;
    
        $targeted_id = 848;
        $trip_count  = (int) $customer->get_meta('trip_848_count');
    
        foreach( WC()->cart->get_cart() as $item ) {
            if ( in_array($targeted_id, $item['product_id']) ) {
                $customer->update_meta_data('trip_count', $trip_count + intval($item['quantity']));
            }
        }
    }
    
    add_action( 'woocommerce_cart_calculate_fees', 'custom_discount_for_logged_in_users' );
    function custom_discount_for_logged_in_users( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( ! is_user_logged_in() ) 
            return;
    
        $targeted_id = 848;
        $trip_count  = (int) WC()->customer->get_meta('trip_848_count');
    
        foreach( $cart->get_cart() as $item ) {
            if ( in_array($targeted_id, $item['product_id']) ) {
                $trip_count += intval($item['quantity']);
            }
        }
    
        if ( $trip_count > 3 ) {
            $discount = $cart->subtotal * 0.15;
            $cart->add_fee( __('15% Discount'), -$discount, true );
        }
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). It should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search