Get product details by url key in WordPress woocommerce

I want to load products deatails on my custom page.Is there any way that i can use the url key like http://localhost/mysite/product/samsung-1.In this url samsung-1 is the key for that product.I want to use this and get product details like name,description and attributes.Can i do this using url key or i have to use id for this.thanks
I tried this code but this uses id :

<?php $post = get_post('5653'); //assuming $id has been initialized
echo "<pre/>";
print_r($post);
wp_reset_postdata();?>

2 Answers
2

You simply can use the existing function get_page_by_path() for this, which has a third parameter $post_type you can specify. In your case a post from the custom post type product is what you want.

Basic example:

$product_obj = get_page_by_path( $slug, OBJECT, 'product' );

Leave a Comment