2

Hi guys i have this array when i do print_r($p)

Array
(
    [0] => Array
        (
            [product] => Array
                (
                    [title] => test
                    [id] => 9
                    [created_at] => 2015-08-11 19:32:05
                    [isNew] => 
                    [type] => simple
                    [status] => publish
                    [price] => 10.00
                    [regular_price] => 10.00
                    [sale_price] => 6.00
                    [stock_quantity] => 19999985
                    [featured] => 1
                    [on_sale] => 
                    [description] => 
                    [short_description] => 
                    [categories] => Array
                        (
                        )

                    [tags] => Array
                        (
                        )

                    [images] => Array
                        (
                            [0] => Array
                                (
                                    [src] => 
                                )

                        )

                    [featured_src] => 
                    [attributes] => Array
                        (
                        )

                    [variations] => 
                )

        )

    [1] => Array
        (
            [product] => Array
                (
                    [title] => test222222
                    [id] => 97
                    [created_at] => 2015-08-31 17:40:54
                    [isNew] => 
                    [type] => variation
                    [status] => publish
                    [price] => 1
                    [regular_price] => 2
                    [sale_price] => 1
                    [stock_quantity] => 1999974
                    [featured] => 1
                    [on_sale] => 1
                    [description] => <p>tasdasd</p>

                    [short_description] => 
                    [categories] => Array
                        (
                        )

                    [tags] => Array
                        (
                        )

                    [images] => Array
                        (
                            [0] => Array
                                (
                                    [src] => 
                                )

                        )

                    [featured_src] => 
                    [attributes] => Array
                        (
                            [0] => Array
                                (
                                    [name] => Color
                                    [slug] => Color
                                    [position] => 0
                                    [visible] => 1
                                    [variation] => 1
                                    [options] => Array
                                        (
                                            [0] => black
                                            [1] => White
                                        )

                                )

                        )

                    [variations] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 98
                                    [price] => 1
                                    [regular_price] => 2
                                    [stock] => 199969
                                    [color] => black
                                )

                            [1] => Array
                                (
                                    [id] => 97
                                    [price] => 1
                                    [regular_price] => 2
                                    [stock] => 1999974
                                    [color] => White
                                )

                        )

                )

        )

    [2] => Array
        (
            [product] => Array
                (
                    [title] => test222222
                    [id] => 98
                    [created_at] => 2015-08-31 17:40:54
                    [isNew] => 
                    [type] => variation
                    [status] => publish
                    [price] => 1
                    [regular_price] => 2
                    [sale_price] => 1
                    [stock_quantity] => 199969
                    [featured] => 1
                    [on_sale] => 1
                    [description] => <p>tasdasd</p>

                    [short_description] => 
                    [categories] => Array
                        (
                        )

                    [tags] => Array
                        (
                        )

                    [images] => Array
                        (
                            [0] => Array
                                (
                                    [src] => 
                                )

                        )

                    [featured_src] => 
                    [attributes] => Array
                        (
                            [0] => Array
                                (
                                    [name] => Color
                                    [slug] => Color
                                    [position] => 0
                                    [visible] => 1
                                    [variation] => 1
                                    [options] => Array
                                        (
                                            [0] => black
                                            [1] => White
                                        )

                                )

                        )

                    [variations] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 98
                                    [price] => 1
                                    [regular_price] => 2
                                    [stock] => 199969
                                    [color] => black
                                )

                            [1] => Array
                                (
                                    [id] => 97
                                    [price] => 1
                                    [regular_price] => 2
                                    [stock] => 1999974
                                    [color] => White
                                )

                        )

                )

        )

    [3] => Array
        (
            [product] => Array
                (
                    [title] => test222222
                    [id] => 76
                    [created_at] => 2015-08-31 17:40:54
                    [isNew] => 
                    [type] => variable
                    [status] => publish
                    [price] => 0.00
                    [regular_price] => 0.00
                    [sale_price] => 0.00
                    [stock_quantity] => 50000
                    [featured] => 1
                    [on_sale] => 1
                    [description] => <p>tasdasd</p>

                    [short_description] => 
                    [categories] => Array
                        (
                        )

                    [tags] => Array
                        (
                        )

                    [images] => Array
                        (
                            [0] => Array
                                (
                                    [src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
                                )

                        )

                    [featured_src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
                    [attributes] => Array
                        (
                            [0] => Array
                                (
                                    [name] => Color
                                    [slug] => Color
                                    [position] => 0
                                    [visible] => 1
                                    [variation] => 1
                                    [options] => Array
                                        (
                                            [0] => black
                                            [1] => White
                                        )

                                )

                        )

                    [variations] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 98
                                    [price] => 1
                                    [regular_price] => 2
                                    [stock] => 199969
                                    [color] => black
                                )

                            [1] => Array
                                (
                                    [id] => 97
                                    [price] => 1
                                    [regular_price] => 2
                                    [stock] => 1999974
                                    [color] => White
                                )

                        )

                )

        )

)
null

i get this with this function

public function test(){
            global $wpdb;
            global $Pproduct;
            global $woocommerce;

            $productIds = 9_97_98_76;
            $pId = explode("_", $productIds);
            foreach($pId as $productID){
                $product[] = $Pproduct->get_product($productID, $fields);
                $p = $product;

            }
            print_r($p);

how do i do a foreach loop again to get variation attributes? in given product id 9_97_98_76, product id 97 & 98 are variation products of 76.

I want to get the title of the product and variable attributes, how do i code foreach so that the result returns as the following array : test_test222222 white_test222222 black_test222222 ???

2
  • 1
    Um, what are you ultimately trying to do? Where is this array coming from? If you want to query certain products, why not use WP_Query? Commented Nov 13, 2015 at 5:32
  • Agreeing with @helgatheviking - If there's a way to do this using the WordPress functions, you should. Commented Nov 13, 2015 at 8:17

2 Answers 2

1

try this.. Hope you are getting the product object for the productID using the get_product() function. Then try array_push to insert all filtered product objects in 1 array.

$product = Array();
 foreach($pId as $productID){
                array_push($product, $Pproduct->get_product($productID, $fields));

            }

now try the following

    foreach($product as $temp) {
        echo $temp[variations];
   }
Sign up to request clarification or add additional context in comments.

2 Comments

like this? i try nth happen $product = Array(); foreach($pId as $productID){ array_push($product, $Pproduct->get_product($productID, $fields)); foreach($product as $temp) { $ii = $temp[variations]; } } return $ii
yeah.. anyway you can get any object value using another foreach as i mentioned earlier. give a upvote to that answer if it worked for you. so that it'll be useful for someone else.
0

Try this will get your title

$pId = explode("_", $products);
foreach($pId as $id){
$product = $Pproduct->get_product($id, $fields);
$title = $product['product']['title'];
echo $title."</br>";
}

Comments