Woocommerce - 尝试将产品数据与自定义表中的数据合并

问题描述 投票:0回答:1

我有几个自定义表格,并且我正在使用带有 twig 的高级自定义字段(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();

但我不明白。我有点乱

php wordpress woocommerce extending extending-classes
1个回答
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 对象
    (
        [ID] => 99
        [post_author] => 1
        ...
        [自定义] => 数组
            (
                [表1] => 数组
        ...
    )

WC_产品_简单对象
(
    [id] => 99
    [post] => WP_Post 对象
        (
            [ID] => 99
            [post_author] => 1
            ...
            [自定义] => 数组
                (
                    [表1] => 数组
            ...

*/

我敢打赌,WooCommerce 也有自己的过滤器。

© www.soinside.com 2019 - 2024. All rights reserved.