Malik Muhammad Kamran
Malik Muhammad Kamran

Reputation: 21

How can I get the product url?

I am adding an extra button on single product page. I would like to get the button url from the product.

I already have the following code

add_action( 'woocommerce_single_product_summary', 'my_extra_button_on_product_page', 30 );
function my_extra_button_on_product_page() {
    global $product;
    echo '<a href="URL">Extra Button</a>';
}

what will be the editing to get href from the product on single product page?

Upvotes: 1

Views: 2743

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29660

What you are looking for is the following get_permalink( $product->get_id() );

https://businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/

then you get

function my_extra_button_on_product_page() {
    global $product;

    // Get product id
    $product_id = $product->get_id();

    // Get url
    $url = get_permalink( $product_id );

    echo '<a href="' . $url . '">Extra Button</a>';
}
add_action( 'woocommerce_single_product_summary', 'my_extra_button_on_product_page', 30 );

Upvotes: 1

Related Questions