Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce - Get SKU in product single page

I want go get the SKU in my product single pages in Woocommerce. I tried <?php get_sku(); ?> which is a featured function according to the Woocommerce docs (http://docs.woothemes.com/wc-apidocs/class-WC_Product.html) but it will just break the loop. What to do?

like image 962
carl_mace Avatar asked Sep 30 '13 07:09

carl_mace


People also ask

How do I get product SKU in WooCommerce?

Once you click on the edit option, you find all the available settings for that individual product. Scroll down and click on the inventory and there will be the option to set a WooCommerce product SKU.

How do I show all items on one page in WooCommerce?

In the WordPress admin, go to WooCommerce > Settings > Products > Product tables. Add your license key and read through all the settings, choosing the ones that you want for your WooCommerce all products list. Now create a page where you want to list all products in a table (Pages > Add New.

How do I get product information by product ID in WooCommerce?

$productId = 164; $product = wc_get_product( $productId ); echo $product->get_title(); echo $product->get_price_html(); Note, that the short description is merely the post's post_excerpt . If using outside of the loop (where $post is automatically defined) then you would need to get the post directly.


1 Answers

get_sku() is a method of the WC_Product class, so you need to have a product on which to call it, like this:

<?php echo $product->get_sku(); ?> 

This should work if you are inside the WooCommerce product loop, and is what is used in the WooCommerce template files. If you don't already have a reference to the product object, you may need to add the following line at the top of the file in order to access it:

global $product; 
like image 77
Sarah Avatar answered Oct 03 '22 01:10

Sarah