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.
How to Get WooCommerce Product ID from It's Slug?
Asked Answered
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 slug –
Aubreyaubrie
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
use the existing function get_page_by_path();
$product_obj = get_page_by_path( $slug, OBJECT, 'product' );
better explanation :
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, thanks –
Retort
© 2022 - 2024 — McMap. All rights reserved.