wordpress Woocommerce - 在产品单页中获取 SKU

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19088804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:22:12  来源:igfitidea点击:

Woocommerce - Get SKU in product single page

wordpressgetwoocommercesinglepage

提问by carl_mace

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?

我想在 Woocommerce 的产品单页中获取 SKU。我<?php get_sku(); ?>根据 Woocommerce 文档(http://docs.woothemes.com/wc-apidocs/class-WC_Product.html)尝试了哪个是特色功能,但它只会打破循环。该怎么办?

回答by Sarah

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

get_sku()是 WC_Product 类的一个方法,所以你需要有一个产品来调用它,像这样:

<?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:

如果您在 WooCommerce 产品循环中,这应该可以工作,并且是 WooCommerce 模板文件中使用的内容。如果您还没有对产品对象的引用,您可能需要在文件顶部添加以下行才能访问它:

global $product;