php Woocommerce - 如何在插件中检查产品类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26766257/
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 - How to check product type in plugin
提问by Cloud_Ratha
I'm pretty new to wordpress/woocommerce and just started playing with creating a custom plugin.
我对 wordpress/woocommerce 很陌生,刚刚开始尝试创建自定义插件。
So far I have I have added my custom woocommerce settings via the api.
到目前为止,我已经通过 api 添加了我的自定义 woocommerce 设置。
I've run into a problem where I want to add a custom field on a single product in the product data tab.
我遇到了一个问题,我想在产品数据选项卡中的单个产品上添加自定义字段。
I managed to display it using the following code:
我设法使用以下代码显示它:
add_action( 'woocommerce_product_options_general_product_data', array( $this, 'cuzd_general_fields' ) );
add_action( 'woocommerce_process_product_meta', array( $this, 'cuzd_general_fields_save') );
However now I need to check if the product type is simple or variation. I tried the following:
但是现在我需要检查产品类型是简单还是变化。我尝试了以下方法:
$product = new WC_Product( get_the_ID() );
if( $product->is_type( 'simple' ) ) {
//
}
However I get an error:
但是我收到一个错误:
Fatal error: Class 'WC_Product' not found in ....
I have a good feeling I'm trying to initiate the Product class before its been called. I most likely have the whole format of the class plugin wrong. Any reading material with good instruction / best practice would be appreciated.
我有一种很好的感觉,我正在尝试在 Product 类被调用之前启动它。我很可能把类插件的整个格式弄错了。任何具有良好指导/最佳实践的阅读材料将不胜感激。
Otherwise if the above is a simple fix please let me know.
否则,如果以上是一个简单的修复,请告诉我。
回答by helgatheviking
The earliest you could expect to access any Woo classes would be the woocommerce_loaded
hook which is now fired in the plugins_loaded
hook. If you are saving on the woocommerce_process_product_meta
hook then any callback there would have all the classes properly loaded. If you are testing outside of that callback (and not attached to any hook at all.... it would be possible for the classes to not all be properly loaded.
您可以期望访问任何 Woo 类的最早时间woocommerce_loaded
是现在在plugins_loaded
钩子中触发的钩子。如果您保存在woocommerce_process_product_meta
钩子上,那么任何回调都会正确加载所有类。如果您在该回调之外进行测试(并且根本没有附加到任何钩子上.... 类可能无法全部正确加载。
Additionally, if you are attempting to call get_the_ID()
before the WP_Post
object has been set up you won't get a correct value.
此外,如果您get_the_ID()
在设置WP_Post
对象之前尝试调用,您将无法获得正确的值。
A more complete cuzd_general_fields_save
routine would look like:
更完整的cuzd_general_fields_save
例程如下所示:
/**
* Save meta box data.
*
* @param int $post_id WP post id.
*/
public function cuzd_general_fields_save( $post_id ) {
$_product = wc_get_product( $post_id );
if( $_product->is_type( 'simple' ) ) {
// do stuff for simple products
} else {
// do stuff for everything else
}
$_product->save();
}
An update for Woo 3.0 would be to use the woocommerce_admin_process_product_object
so you no longer need to instantiate the product object or run save()
as Woo will handle that in core.
Woo 3.0 的更新将使用 ,woocommerce_admin_process_product_object
因此您不再需要实例化产品对象或运行,save()
因为 Woo 将在核心中处理它。
add_action( 'woocommerce_admin_process_product_object', array( $this, 'cuzd_general_fields_save') );
and the callback would then be modified to:
然后回调将修改为:
/**
* Save meta box data.
*
* @param obj $_product WC_Product.
*/
public function cuzd_general_fields_save( $_product ) {
if( $_product->is_type( 'simple' ) ) {
// do stuff for simple products
} else {
// do stuff for everything else
}
}
回答by Cragmonkey
Here's one way to check the product type without creating an instance of the product:
这是在不创建产品实例的情况下检查产品类型的一种方法:
$product_type = get_the_terms( $product_id,'product_type')[0]->slug;