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?

Answers


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();

Need Your Help

scale HTML5 Video and break aspect ratio to fill whole site

css html5 html5-video

I want to use a 4:3 video as a background on a site, but setting the width and height to 100%

How to Read in multiple CSV files from an XSLT file and output a single XML file

c#-4.0 xslt-2.0 saxon

I plan to use Saxon for an XSLT problem. I need to run my program on a schedule. When it runs it needs to select all CSV files from a directory. The number of files can be random but once processed...