0

いくつかのカスタム テーブルがあり、小枝で高度なカスタム フィールド (acf) を使用しています。これらのフィールドはカスタム テーブルに保存されます。

例えば:

custom_table1custom_table2custom_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();

しかし、私はそれを取得しません。私は少し混乱しています

4

1 に答える 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
        [投稿者] => 1
        ...
        [カスタム] => 配列
            (
                [テーブル1] =>配列
        ...
    )

WC_Product_Simple オブジェクト
(
    [ID] => 99
    [post] => WP_Post オブジェクト
        (
            [ID] => 99
            [投稿者] => 1
            ...
            [カスタム] => 配列
                (
                    [テーブル1] =>配列
            ...

*/

WooCommerce にも独自のフィルターがあるに違いありません。

于 2015-09-18T15:10:44.307 に答える