I am importing products from one platform to another using CSV. I have successfully created arrays of products from the CSV file but have difficulty creating variations from these product arrays.
A product array looks like this
Array
(
[Name] => Test Product
[Price] => 50
[Options] => Array
(
['Color'] => Array
(
['Red'] => 10
['Green'] => 20
)
['Size'] => Array
(
['Small'] => 30
['Medium'] => 40
)
)
)
The desired output for the variations array is
Array
(
[0] => Array
(
[Name] => Test Product - Red, Small
[Regular price] => 80
)
[1] => Array
(
[Name] => Test Product - Red, Medium
[Regular price] => 100
)
[2] => Array
(
[Name] => Test Product - Green, Small
[Regular price] => 100
)
[3] => Array
(
[Name] => Test Product - Green, Medium
[Regular price] => 110
)
)
My PHP code is as follows
<?php
echo "<pre>";
$product = array(
"Name" => "Test Product",
"Price" => 50,
"Options" => array(
"'Color'" => array(
"'Red'" => 10,
"'Green'" => 20,
),
"'Size'" => array(
"'Small'" => 30,
"'Medium'" => 40,
)
),
);
print_r($product);
$variations = array();
foreach ($product["Options"] as $option) {
foreach ($option as $name => $price) {
$name = str_replace("'", "", $name);
array_push($variations, array(
"Name" => "Test Product - ${name}",
"Price" => (int)$product["Regular price"] + $price,
));
}
}
print_r($variations);
I am stuck on combining the Options
. The goal is to merge the options. I tried various possibilities using the for
loop and the nested for
loop but had no luck. Can someone please guide me in the right direction? Thank you