我有几个自定义表格,我正在使用带有树枝的高级自定义字段(acf)。这些字段存储在自定义表中。
例如:
custom_table1
,custom_table2
,custom_table1_table2_relation
以及更多内容。
可以在管理产品面板中对其进行编辑,并将其显示为与产品数据相关的对象。 现在我将它们合并到一个关联数组。
我有这个:
/*output*/
[post] => TimberPost Object (
[ImageClass] => TimberImage
[PostClass] => TimberPost
[object_type] => post
[class] => post-8 product type-product
[id] => 1
[post_author] => 1
[post_content] => description
[custom] => Array (
[table1-field1] => data
[table1-field2] => data
[table2_field1] => data
[table2_field] => data
)
)
我喜欢这个:
$products['products'] = array(
'id' => 1,
'post_content' => 'description',
'post_title' => 'title',
'and_so_on' => 'stuff',
[ 'custom' ] => array(
'table1' => array(
'data1' => 'content',
'data2' => 'content',
),
'table2' => array(
'data1' => 'content',
'data2' => 'content',
) /*and so on*/
)
);
我试图扩展WC_Class并创建一个动作来在前面调用这些
这只是一个代码段示例
class WCIntegrationProductIntegration extends WC_Query {
function __construct() {
add_action( 'woocommerce_custom_product_data', array( $this->_get_custom_product_data ) );
}
public function _get_custom_product_data() {
global $woocommerce;
$custom = array_merge(
array(
/*get custom tables and data*/
'table' = $table1,
'data' array($fields),
)
);
$product = array_merge(
array(
/*get productss and data*/
'table' = $table1,
'data' array($fields),
)
);
$the_query = new WP_Query();
}
}
new WCIntegrationProductIntegration();
但我不明白。我有点乱了
答案 0 :(得分:0)
我不知道是否需要扩展WC类,使用WP_Post
过滤器向the_posts
对象添加自定义数据很简单。
原始示例:
add_filter( 'the_posts', function( $posts ) {
$posts[0]->custom = [ 'table1' => [] ];
return $posts;
});
add_action( 'the_post', function( $post ) {
global $product;
print_r( $post );
print_r( $product );
});
输出:
/* WP_Post Object ( [ID] => 99 [post_author] => 1 ... [custom] => Array ( [table1] => Array ... ) WC_Product_Simple Object ( [id] => 99 [post] => WP_Post Object ( [ID] => 99 [post_author] => 1 ... [custom] => Array ( [table1] => Array ... */
我敢打赌,WooCommerce也有自己的过滤器。