26

I am trying to get Woocommerce product permalink by given product name. I know I can get permalink by given product ID like this:

$url = get_permalink( $product_id );

But I can not find any code to get it by product name. I tried this:

$url = get_permalink( 'title' );

That does not work. Please help.

peterh
  • 11,875
  • 18
  • 85
  • 108
user3868840
  • 371
  • 1
  • 3
  • 4

3 Answers3

26

This code works for me

$product = get_page_by_title( 'Product Title', OBJECT, 'product' )
echo get_permalink( $product->ID );
sabarnix
  • 715
  • 4
  • 5
25
$product = wc_get_product( $product_id );
$permalink = $product->get_permalink();
José Neto
  • 488
  • 8
  • 12
  • 3
    While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – ysf Jun 25 '20 at 11:53
7

For URL:

$url = get_permalink($product_id)

For the title:

$name = get_the_title($product_id)