Woocommerce: How do I get the product slug from the id?
I currently have the product id available in the cart and I need to retrieve the slug. How can I do this?
You can use get_post
$product = get_post( 27 ); $slug = $product->post_name; echo $slug;
Alternatively to get_post, you can use get_product if you already have a product or need it for other purpose
$_pf = new WC_Product_Factory(); $product = $_pf->get_product($product_id); $slug = $product->get_slug();