user3868840
user3868840

Reputation: 381

Get product permalink by product name in Woocommerce

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.

Upvotes: 27

Views: 94211

Answers (3)

José Neto
José Neto

Reputation: 507

$product = wc_get_product( $product_id );
$permalink = $product->get_permalink();

Upvotes: 27

Alkesh Goswami
Alkesh Goswami

Reputation: 81

For URL:

$url = get_permalink($product_id)

For the title:

$name = get_the_title($product_id) 

Upvotes: 8

sabarnix
sabarnix

Reputation: 775

This code works for me

$product = get_page_by_title( 'Product Title', OBJECT, 'product' )
echo get_permalink( $product->ID );

Upvotes: 30

Related Questions