1
votes

I am using Woocommerce Product Add-on plugin to allow visitor to select multiple options on simple product. As the product is simple product Woocommerce display an Add to cart button on products page view instead of Choose option button linking on Product detail page where visitor can choose options.

I am looking to create a function that will display Choose option button on product by ID.

Here is my starting code.

add_filter( 'woocommerce_loop_add_to_cart_link', 'change_product_button' );
    function change_product_button() {
    global $product;
    $id = $product->id;
    if ($id == 91){
        echo '<a href="'.$product->get_permalink( $product->id ).'" class="button">' . __('View Product', 'woocommerce') . '</a>';
    } else {
      // display usual add to cart button
    }
}

Or maybe we can force product by id as non purchasable to simulate a variable product.

1
just to confirm: you want to display "view product" or "choose options" instead of "Add to cart" on the pages that list products right? like on a category page or the store page right?jnhghy - Alexandru Jantea
This absolutely what I am looking forGreg

1 Answers

4
votes

This is a working solution

add_filter( 'woocommerce_loop_add_to_cart_link', 'change_product_button', 10, 2 );
function change_product_button($html, $product) {
    $values = array(190, 91);
    $id = $product->id;
    if(in_array($id, $values)){
        $html= '<a href="'.$product->get_permalink( $product->id ).'" class="button">' . __('Choose an option', 'woocommerce') . '</a>';
    } else {
        $html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
        $html .= woocommerce_quantity_input( array(), $product, false );
        $html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
        $html .= '</form>';
    }
    return $html;
}