questions

WooCommerce: “SELECT OPTIONS” on a simple product?

Rate this post

I want to display 3 random products on my landing page from my WooCommerce shop and I did that. The problem is that I want every product that is randomly displayed on my landing page to have a “SELECT OPTIONS” button. But seems like all simple products have “ADD TO CART”.

So, is there a way to have a “SELECT OPTIONS” button on simple products which redirects to single product page (the same as in case of variable products)?

Thank you in advance.

 

✔️Solution:

There is woocommerce_loop_add_to_cart_link for this. You can put the following code inside of the functions.php of your theme:

add_filter( 'woocommerce_loop_add_to_cart_link', 'custom_add_to_cart_button', 10, 2 );
function custom_add_to_cart_button($button,$product) {
    // Do not change the button for variable products
    if( $product->is_type('variable') ) return $button;

    $buttontext = __( "SELECT OPTIONS", "woocommerce" ); // your button text

    // replace the button to be a link to the product detail page
    return '<a class="button" href="' . $product->get_permalink() . '">' . $buttontext . '</a>';
}

With this code, you check if it is a variable product. If it is, just return the button. If it is not (so it is a simple product), replace the button with a link to the product detail page.

Leave a Reply

Your email address will not be published.

Back to top button