php Woocommerce 使用产品 SKU 获取产品 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23692540/
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
Woocommerce get Product id using product SKU
提问by Suneth Kalhara
I'm working on a separate templates page, which page gets woocommece product sku using custom field of wordpress post. i need to get product id of that sku for create woocommece object and do further things, here is my code.
我正在处理一个单独的模板页面,该页面使用 wordpress 帖子的自定义字段获取 woocommece 产品 sku。我需要获取该 sku 的产品 ID 以创建 woocommece 对象并做进一步的事情,这是我的代码。
global $woocommerce;
//this return sku (custom field on a wordpress post)
$sku=get_field( "product_sku" );
if($sku!=''){
//need to create object, but using sku cannot create a object,
$product = new WC_Product($sku);
echo $product->get_price_html();
}
is there way to get product id before create object, then i can pass the product id to WC_Product class constructor and create object.thank you
有没有办法在创建对象之前获取产品 ID,然后我可以将产品 ID 传递给 WC_Product 类构造函数并创建对象。谢谢
采纳答案by pmandell
You can use this function (found here). Google is your friend!
您可以使用此功能(在此处找到)。谷歌是你的朋友!
function get_product_by_sku( $sku ) {
global $wpdb;
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
if ( $product_id ) return new WC_Product( $product_id );
return null;
}
回答by Akshay Agarwal
WooCommerce 2.3finally adds support for this in core.
WooCommerce 2.3终于在核心中增加了对此的支持。
If you are using this version, you can call
如果你使用的是这个版本,你可以调用
wc_get_product_id_by_sku( $sku )
回答by Victor Smirnov
WooCommerce product is a special post type. Because of this WP_Query
might be used to find product by SKU and other parameters. This might be used as an alternative when you need to restrict your search by some other criterias.
WooCommerce 产品是一种特殊的帖子类型。因此,这WP_Query
可能用于按 SKU 和其他参数查找产品。当您需要通过其他一些条件限制搜索时,这可以用作替代方法。
For example you might want to specify language if you use Polylang(or other plugins) for site translations. Or you can restrict search by product type.
例如,如果您使用Polylang(或其他插件)进行站点翻译,您可能想要指定语言。或者您可以按产品类型限制搜索。
They do direct SQL query in the WooCommerce method get_product_id_by_sku
which I think is perfectly fine in many cases. But might not work if you use translations, it will return random product but not the one in current language.
他们在 WooCommerce 方法中直接执行 SQL 查询get_product_id_by_sku
,我认为在许多情况下都非常好。但是如果您使用翻译可能不起作用,它会返回随机产品而不是当前语言的产品。
Example code to find product by SKU using WP_Query (with restriction by product type and language):
使用 WP_Query 按 SKU 查找产品的示例代码(受产品类型和语言限制):
public function find( string $lang, string $sku ) {
$query = [
'lang' => $lang,
'post_type' => 'product',
'meta_query' => [
[
'key' => '_sku',
'value' => $sku,
'compare' => '='
]
],
'tax_query' => [
[
'taxonomy' => 'product_type',
'terms' => [ 'grouped' ],
'field' => 'name',
]
]
];
$posts = ( new WP_Query() )->query( $query );
return count( $posts ) > 0 ? $posts[0] : null;
}