jamie
jamie

Reputation: 47

Woocommerce - Try to merge product data with data from custom tables

i have several custom tables and i'm using advanced custom fields (acf) with twig. These fields stored in the custom tables.

for example:

custom_table1, custom_table2, custom_table1_table2_relation and more of this.

They can be edited in the admin product panel and displays as objects in in relation with the product data. Now i will merge those to an associative array.

i have this:


/*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
        )
    )   

and i like to have this:


$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*/
    )
);

I've tried to extended a WC_Class and creating a action to call these in the fronted

this is just an snippet example


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

But i don't get it. I'm a bit in a mess

Upvotes: 0

Views: 369

Answers (1)

Danijel
Danijel

Reputation: 12689

I dont know if extending WC classes is necessary, adding custom data to a WP_Post object is simple with the_posts filter.

Raw example:

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 );
});

Output:

/*
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
            ...

*/

I bet that WooCommerce also has its own filter for that.

Upvotes: 0

Related Questions