skip to Main Content

in functions.php of my templatefolder, I add this code to be able to have a second link on the product page.

but the result is an error 500.

I guess the product id part is wrong. Does anybody see how to solve this?

Woocommerce Version 3.4.5


function my_extra_button_on_product_page() {
  global $product;
echo '<a class="single_add_to_cart_button button alt" href="?add-to-cart'<?=$product->get_id() ?>'">Second Link</a>';
}

I expect that the link that gets generated, has the add-to-cart=[‘product_id’] of course with the correct Product_id

But I get error 500

3

Answers


  1. You have an error in your PHP code. When you insert PHP code in a string in a PHP file (not a template) you do not have to use the PHP tags.

    Here is it:

    function my_extra_button_on_product_page() {
        global $product;
        echo '<a class="single_add_to_cart_button button alt" href="?add-to-cart' . $product->get_id() . '">Second Link</a>';
    }
    

    NOTE
    This will produce this link

    ?add-to-cartPRODUCTID
    

    is this what you want? Don’t you need a different link?

    Login or Signup to reply.
  2. Looks like there is a syntax error in your echo statement please try this :

    function my_extra_button_on_product_page() 
    {
      global $product;
      echo '<a class="single_add_to_cart_button button alt" href="?add-to-cart"'.$product->get_id().'">Second Link</a>';
    }
    
    Login or Signup to reply.
  3. Add this code in your functions.php to display another button beside Add to cart button

    function wc_shop_demo_button() {
    global $product;
    echo '<a class="button demo_button" href="?add-to-cart' . $product->get_id() . '">View Demo</a>';
    }
    add_action( 'woocommerce_after_add_to_cart_button', 'wc_shop_demo_button' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search