-2

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

max
  • 1
  • 3

3 Answers3

0

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?

Giacomo M
  • 4,450
  • 7
  • 28
  • 57
0

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>';
}
Darsh khakhkhar
  • 666
  • 5
  • 15
0

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' );
Praveen
  • 985
  • 8
  • 31