在Woocommerce 3中访问WC_Product保护的数据 [英] Accessing WC_Product protected data in Woocommerce 3
问题描述
我有这个答案供参考: Woocommerce通过ID获取产品价值…关于函数 wc_get_product()
返回受保护的数据。
I have this answer for reference: Woocommerce Get Product Values by ID … It is about the function wc_get_product()
that returns protected data.
getter在哪里为该功能定义的方法?
如何在 wc_get_product()
中访问受保护的数据?
Where are the getter methods defined for that function?
How can i access the protected data inside wc_get_product()
?
此答案线程告诉 WC_Product_Factory
类正在使用。可以扩展此类以访问 wc_get_product()
内部的受保护数据吗?
This Answer thread is telling that WC_Product_Factory
class is being used. Can this class be extended to access the protected data inside wc_get_product()
?
wc_get_product()
似乎是一个函数,但是它如何返回对象?
The wc_get_product()
seems to be a function, but how does it return an object?
推荐答案
wc_get_product($ product_id)
函数提供 WC_Product
实例对象(来自产品ID)可以使用所有可用的 WC_Product $ c访问数据的地方$ c>方法
和 WC_Product
子类,具体取决于产品类型:
The wc_get_product( $product_id)
function gives the WC_Product
instance object (from a product ID) where data can be accessed with all available WC_Product
methods and WC_Product
sub-classes depending on the product type:
// Get the instance of the WC_Product Object
$product = wc_get_product( $product_id);
// Using `WC_Product` methods examples to get specific related data values:
$product_type = $product->get_type(); // product Type
$product_id = $product->get_id(); // product ID
$product_name = $product->get_name(); // product name
$product_sku = $product->get_sku(); // product SKU
$product_price = $product->get_price(); // product price
// And so on…
// The raw display of the object protected data (Just for testing)
echo '<pre>'; print_r( $product ); echo '</pre>';
您可以使用 WC_Data
方法 get_data()
,它将为您提供可访问的数据数组:
You can unprotect the data using the WC_Data
method get_data()
that will give you an accessible array of the data:
// Get the instance of the WC_Product Object
$product = wc_get_product( $product_id);
// Get the accessible array of product properties:
$data = $product->get_data();
// get specific related data values:
$product_id = $data['id']; // product ID
$product_name = $data['name']; // product name
$product_sku = $data['sku']; // product SKU
$product_price = $data['price']; // product price
// And so on…
// The raw display of the unprotected data array (Just for testing)
echo '<pre>'; print_r( $data ); echo '</pre>';
对于特定的自定义元数据,您可以使用
WC_Data
方法get_meta()
。因此,如果自定义元键例如是_custom_height
,则您将使用:
For specific custom meta data you can use the
WC_Data
methodget_meta()
. So if the custom meta key is for example_custom_height
you will use:
$custom_product_height = $product->get_meta( '_custom_height' );
Woocommerce API官方文档:
Official Woocommerce API Documentation:
-
WC_Product
方法列表 -
WC_Product_External
方法列表 -
WC_Product_Grouped
方法列表 -
WC_Product_Simple
列表方法 -
WC_Product_Variable
方法列表 -
WC_Product_Variation
方法列表 -
WC_Data
方法列表
WC_Product
list of methodsWC_Product_External
list of methodsWC_Product_Grouped
list of methodsWC_Product_Simple
list of methodsWC_Product_Variable
list of methodsWC_Product_Variation
list of methodsWC_Data
list of methods
这篇关于在Woocommerce 3中访问WC_Product保护的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!