Custom add to cart button for certain product IDs if product appears in WooCommerce completed orders

Solution 1:

Actually your question is simple and it is a matter of adding an if/else conditions. You can use in_array() to execute the if condition for 1 or more productIDs.

Note: Since WooCommerce 3 use $product->get_id() instead of $product->id

For WooCommerce shop and archives pages:

Replace

// When NOT empty
if ( ! empty( $order_id ) ) {
    // Setting
    $button_text_view_order = __( 'View order now', 'woocommerce' );
    
    // Get view order url
    $view_order_url = wc_get_endpoint_url( 'view-order', $order_id, wc_get_page_permalink( 'myaccount' ) );
    
    // New link + text
    $sprintf = sprintf(
        '<a href="%s" class="%s">%s</a>',
        esc_url( $view_order_url ),
        esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
        esc_html( $button_text_view_order )
    );
}

With

// When NOT empty
if ( ! empty( $order_id ) ) {
    // Products IDs, several can be added, separated by a comma
    $product_ids = array( 45 );
    
    // Checks if a value exists in an array
    if ( in_array( $product->get_id(), $product_ids ) ) {
        // Button text
        $button_text = __( 'For certain productIDs', 'woocommerce' );
        
        // Button url
        $button_url = site_url( '/custom-link/' );          
    } else {    
        // Button text - view order
        $button_text = __( 'View order now', 'woocommerce' );
        
        // Button url - get view order url
        $button_url = wc_get_endpoint_url( 'view-order', $order_id, wc_get_page_permalink( 'myaccount' ) );
    }
    
    // New link + text
    $sprintf = sprintf(
        '<a href="%s" class="%s">%s</a>',
        esc_url( $button_url ),
        esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
        esc_html( $button_text )
    );
}

For single product page:

Replace

// When NOT empty
if ( ! empty( $order_id ) ) {
    // Remove default add to cart button and add a custom one
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    
    // Add action, priority 30 and pass data to add action function
    add_action( 'woocommerce_single_product_summary', function() use ( $order_id ) {
        // Setting
        $button_text_view_order = __( 'View order now', 'woocommerce' );
        
        // Get view order url
        $view_order_url = wc_get_endpoint_url( 'view-order', $order_id, wc_get_page_permalink( 'myaccount' ) );
        
        echo '<a href="' . $view_order_url . '" class="button">' . $button_text_view_order . '</a>';
    }, 30, 0 );
}

With

// When NOT empty
if ( ! empty( $order_id ) ) {
    // Remove default add to cart button and add a custom one
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    
    // Products IDs, several can be added, separated by a comma
    $product_ids = array( 30 );
    
    // Checks if a value exists in an array
    if ( in_array( $product->get_id(), $product_ids ) ) {
        // Button text
        $button_text = __( 'For certain productIDs', 'woocommerce' );
        
        // Button url
        $button_url = site_url( '/custom-link/' );          
    } else {    
        // Button text - view order
        $button_text = __( 'View order now', 'woocommerce' );
        
        // Button url - get view order url
        $button_url = wc_get_endpoint_url( 'view-order', $order_id, wc_get_page_permalink( 'myaccount' ) );
    }
    
    // Add action, priority 30 and pass data to add action function
    add_action( 'woocommerce_single_product_summary', function() use ( $button_text, $button_url ) {            
        // New custom button, adjust classes if necessary (theme related)
        echo '<a href="' . $button_url . '" class="single_add_to_cart_button button alt">' . $button_text . '</a>';
    }, 30, 0 );
}

The full end result:

function get_order_id_by_product_id( $product_id ) {
    global $wpdb;
    
    // Get user ID
    $user_id = get_current_user_id();

    // Get order ID by product ID
    $order_id = $wpdb->get_var( "
        SELECT p.ID FROM {$wpdb->prefix}posts AS p
        INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
        WHERE p.post_status IN ( 'wc-completed' )
        AND pm.meta_key = '_customer_user'
        AND pm.meta_value = '$user_id'
        AND woim.meta_key IN ( '_product_id', '_variation_id' )
        AND woim.meta_value = '$product_id'
        LIMIT 1
    " );

    // Return
    return $order_id;
}

// On WooCommerce shop and archives pages
function filter_woocommerce_loop_add_to_cart_link( $sprintf, $product, $args ) {
    // Only for logged in users
    if ( ! is_user_logged_in() ) return $sprintf;
    
    // Only for single type products
    if ( ! $product->is_type( 'simple' ) ) return $sprintf;
    
    // Call fuction and get order ID
    $order_id = get_order_id_by_product_id( $product->get_id() );
    
    // When NOT empty
    if ( ! empty( $order_id ) ) {
        // Products IDs, several can be added, separated by a comma
        $product_ids = array( 30 );
        
        // Checks if a value exists in an array
        if ( in_array( $product->get_id(), $product_ids ) ) {
            // Button text
            $button_text = __( 'For certain productIDs', 'woocommerce' );
            
            // Button url
            $button_url = site_url( '/custom-link/' );          
        } else {    
            // Button text - view order
            $button_text = __( 'View order now', 'woocommerce' );
            
            // Button url - get view order url
            $button_url = wc_get_endpoint_url( 'view-order', $order_id, wc_get_page_permalink( 'myaccount' ) );
        }
        
        // New link + text
        $sprintf = sprintf(
            '<a href="%s" class="%s">%s</a>',
            esc_url( $button_url ),
            esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
            esc_html( $button_text )
        );
    }
    
    return $sprintf;
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'filter_woocommerce_loop_add_to_cart_link', 10, 3 );

// On single product page, replacing the single add to cart product button by a custom button
function action_woocommerce_single_product_summary() {
    global $product;

    // Only for logged in users
    if ( ! is_user_logged_in() ) return;
    
    // Only for single type products
    if ( ! $product->is_type( 'simple' ) ) return;
    
    // Call fuction and get order ID
    $order_id = get_order_id_by_product_id( $product->get_id() );
    
    // When NOT empty
    if ( ! empty( $order_id ) ) {
        // Remove default add to cart button and add a custom one
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        
        // Products IDs, several can be added, separated by a comma
        $product_ids = array( 30 );
        
        // Checks if a value exists in an array
        if ( in_array( $product->get_id(), $product_ids ) ) {
            // Button text
            $button_text = __( 'For certain productIDs', 'woocommerce' );
            
            // Button url
            $button_url = site_url( '/custom-link/' );          
        } else {    
            // Button text - view order
            $button_text = __( 'View order now', 'woocommerce' );
            
            // Button url - get view order url
            $button_url = wc_get_endpoint_url( 'view-order', $order_id, wc_get_page_permalink( 'myaccount' ) );
        }
        
        // Add action, priority 30 and pass data to add action function
        add_action( 'woocommerce_single_product_summary', function() use ( $button_text, $button_url ) {            
            // New custom button, adjust classes if necessary (theme related)
            echo '<a href="' . $button_url . '" class="single_add_to_cart_button button alt">' . $button_text . '</a>';
        }, 30, 0 );
    }
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 1 );