获取 Woocommerce 复合产品组件 [英] Get Woocommerce Composite Product Components
问题描述
我将 Woocommerce 与 Woocommerce 复合产品扩展程序一起使用(它允许您将其他产品组合起来制作产品).
我想弄清楚复合产品是否有货",但通常的函数 is_in_stock() 不适用于复合产品.
如果有更合适的函数,我想知道它是什么.
或者,下面的函数 'wc_get_product()' 返回以下内容,包括组件产品 ID (assigned_ids):
$product = wc_get_product($productid);var_dump($product);
返回:
object(WC_Product_Composite)#11305 (23) {["extended_data":"WC_Product_Composite":private]=>数组(7){[add_to_cart_form_location"]=>字符串(7)默认"[shop_price_calc"]=>字符串(7)默认"}["composite_meta":"WC_Product_Composite":private]=>数组(4){[1477435352]=>数组(19){[component_id"]=>字符串(10)1477435352"[查询类型"]=>字符串(11)product_ids"[assigned_ids"]=>数组(2){[0]=>整数(541)[1]=>整数(588)}}}
基本上,我想要的是提取并循环遍历assigned_ids"子数组,检查每个子数组是否有库存,但除了将其打印到屏幕之外,我无法访问此对象的属性,因为它们是私有的"(我完全不熟悉的东西).我相信我可以通过在 Wordpress/插件文件中查找类来解决这个问题,但我不知道如何找到它.任何帮助将不胜感激.
库存(复合产品要复杂得多):
关于复合产品中的库存和库存管理,显然有
WC_CP_Stock_Manager
和WC_CP_Stock_Manager_Item
专用类,所以它似乎没有以经典的方式工作项目"和集合"的一些概念.此方法和功能在
includes/class-wc-cp-stock-manager.php
核心文件中可用.
访问数据:
如您所见,要获取 WC_Product_Composite
对象保护属性,您应该需要使用继承的 WC_Product
方法或更具体地说 WC_Product_Composite
方法记录在核心代码类中.
现在,访问(未受保护的)数据的最简单方法是使用以下方法继承自WC_Data
类:
//获取 WC_Product_Composite 对象的实例$product = wc_get_product( $product_id );//获取未受保护数组中的数据$product_data = $product->get_data();//输出原始数据(用于测试)echo '';打印_r( $product_data );echo '</pre>';//获取未受保护数组中的特殊元数据(如果存在)$product_meta_data = $product->get_meta_data();//输出原始特殊元数据(用于测试)echo '';print_r( $product_meta_data );echo '</pre>';
或者你也可以使用一些 WC_Product_Composite
方法:
//获取 WC_Product_Composite 对象的实例$product = wc_get_product( $product_id );//获取组件ID$component_data = $product->get_component_id();//获取组件$component_data = $product->get_component();//获取原始组件数据$component_data = $product->get_component_data();
您需要以任何方式进入核心文件代码以了解事情的工作原理并为复合产品找到正确的方法.
I am using Woocommerce with the Woocommerce Composite Products extension (Which allows you to make a product out of a combination of other products).
I am trying to figure out if a composite product is 'in stock', but the usual function is_in_stock() doesn't work on composites.
If there is a more appropriate function, I would like to know what it is.
Alternatrively, The function 'wc_get_product()' below returns the below, including componentproduct IDs (assigned_ids):
$product = wc_get_product($productid);
var_dump($product);
Returns:
object(WC_Product_Composite)#11305 (23) {
["extended_data":"WC_Product_Composite":private]=>
array(7) {
["add_to_cart_form_location"]=>
string(7) "default"
["shop_price_calc"]=>
string(7) "default"
}
["composite_meta":"WC_Product_Composite":private]=>
array(4) {
[1477435352]=>
array(19) {
["component_id"]=>
string(10) "1477435352"
["query_type"]=>
string(11) "product_ids"
["assigned_ids"]=>
array(2) {
[0]=>
int(541)
[1]=>
int(588)
}
}
}
Basically, what I want is to extract and loop through the 'assigned_ids' subarray checking if each is in stock, but aside from printing this to the screen, I'm unable to access this object's properties because they are 'private' (something I'm completely unfamiliar with). I believe I can possibly figure this out by hunting down the class in Wordpress / the plugin files but I have no idea how to find it. Any help would be much appreciated.
Stock (much more complicated for composite products):
Regarding Stock and stock management in Composite products, apparently there is
WC_CP_Stock_Manager
andWC_CP_Stock_Manager_Item
dedicated classes for that, so it doesn't seem to work in a classic way with some concepts of "items" and "collections".This methods and functionalities are available in
includes/class-wc-cp-stock-manager.php
core files.
Accessing the data:
As you can see, to get the WC_Product_Composite
object protected properties, you should need to use the inherited WC_Product
methods or more specifically WC_Product_Composite
methods documented in the core code class.
Now, The easiest way to access (unprotected) data is to use the following methods inherited from WC_Data
Class:
// Get an instance of the WC_Product_Composite object
$product = wc_get_product( $product_id );
// Get the data in an unprotected array
$product_data = $product->get_data();
// Output the raw data (for test)
echo '<pre>'; print_r( $product_data ); echo '</pre>';
// Get special meta data in an unprotected array (if it exist)
$product_meta_data = $product->get_meta_data();
// Output the raw special meta data (for test)
echo '<pre>'; print_r( $product_meta_data ); echo '</pre>';
Or you could use also some WC_Product_Composite
methods as:
// Get an instance of the WC_Product_Composite object
$product = wc_get_product( $product_id );
// Get the component Id
$component_data = $product->get_component_id();
// Get the component
$component_data = $product->get_component();
// Get the raw component data
$component_data = $product->get_component_data();
Any way you will need to get into core files code to understand how things works and to get the right methods for Composite products.
这篇关于获取 Woocommerce 复合产品组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!