How to Get WooCommerce Product ID from It's Slug?
Asked Answered
T

1

8

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.

Torrell answered 29/9, 2017 at 10:9 Comment(4)
yes! it;s depend on your slug.Aubreyaubrie
how? the page i'm in and the target product's page share the same slug.Torrell
can you show slugAubreyaubrie
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.Torrell
H
19

use the existing function get_page_by_path();

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

better explanation :

https://wordpress.stackexchange.com/questions/206886/get-product-details-by-url-key-in-wordpress-woocommerce

Howells answered 29/9, 2017 at 10:14 Comment(2)
If 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);Eberhard
This really helped me out, thanksRetort

© 2022 - 2024 — McMap. All rights reserved.