Is it possible to get an ID
of a WooCommerce product only by knowing it's slug
? I need to fetch the product's price outside the product's page.
Asked
Active
Viewed 1.3k times
7

testimonial
- 93
- 1
- 1
- 9
-
yes! it;s depend on your slug. – Ahmad Hassan Sep 29 '17 at 10:11
-
how? the page i'm in and the target product's page share the same slug. – testimonial Sep 29 '17 at 10:14
-
can you show slug – Ahmad Hassan Sep 29 '17 at 10:15
-
domain.com/product/test is the product. domain.com/course/test is the page i'm in, need to get the id of test product. – testimonial Sep 29 '17 at 10:28
1 Answers
18
use the existing function get_page_by_path();
$product_obj = get_page_by_path( $slug, OBJECT, 'product' );
better explanation :
-
4If you want the WC_Product object, you can update this a bit: ```$product_obj = new WC_Product(get_page_by_path( $slug, OBJECT, 'product' )->ID);``` – Ethan C Aug 12 '19 at 22:03
-