Okay, So I am wanting to find information in an array and get a block returned based on the credentials passed. The way I am doing it right now is not working, I'm looking for a shorter process and a more fool proof process.
Right now I have this:
public function get_product($product_id, $color, $size)
{
$results = $this->pf->get('products/'.$product_id);
$vars = $results['variants'];
$details = array();
foreach($vars as $var)
{
if(!in_array($product_id, $details))
{
if($var['product_id'] == $product_id)
{
if($var['size'] == $size)
{
if($var['color'] == $color)
{
$details[$var['id']] = array(
'id' => $var['id'],
'name' => $var['name'],
'image' => $var['image'],
'price' => $var['price'],
);
}
}
}
}
}
return $details;
}
This receives a product_id, a color, and a size. Sometimes $color
is null, Sometimes $size
is null, and sometimes both $color
and $size
are null and we just need to find the one array that matches the $product_id
.
What I am wanting returned is this:
$details[$var['id']] = array(
'id' => $var['id'],
'name' => $var['name'],
'image' => $var['image'],
'price' => $var['price'],
);
Right now nothing gets returned. $results
returns this for an example: (This is what I need to search.)
{
"code": 200,
"result": {
"product": {
"id": 1,
"type": "POSTER",
"brand": null,
"model": "Poster",
"image": "https://d1yg28hrivmbqm.cloudfront.net/products/poster_18x24.jpg",
"variant_count": 9,
"files": [
{
"id": "default",
"title": "Print file",
"additional_price": null
},
{
"id": "preview",
"title": "Mockup",
"additional_price": null
}
],
"options": []
},
"variants": [
{
"id": 4464,
"product_id": 1,
"name": "Poster 12×12",
"size": "12×12",
"color": null,
"color_code": null,
"image": "https://d1yg28hrivmbqm.cloudfront.net/products/1/4464.jpg",
"price": "9.00"
},
Notice how color
is returned as null
. size
can be that way to. So basically I am wanting a quicker and better way to search the returned array for the specified product_id, size, and color. So I need returned and matching the corresponding variants block that matches the variables submitted.
I hope I've made sense of what I'm trying to accomplish.
UPDATE
This is what I am needing.
So on my site the customers chooses a product, in this case a poster. Before adding it to the cart they are prompted to select a size. Let's say a 12x12. The way the API works is that it has a "top" item and then has smaller items "variants" that include the size and color. Each variant is a poster with a different size. The only way to obtain the poster product, is by receiving every variant for the poster. But each "variant" has a different "id" to send to the api to order the correct product.
So, I receive the product and it's variants in bulk or every color and size as it's own variant.
"variants": [
{
"id": 4464,
"product_id": 1,
"name": "Poster 12×12",
"size": "12×12",
"color": null,
"color_code": null,
"image": "https://d1yg28hrivmbqm.cloudfront.net/products/1/4464.jpg",
"price": "9.00"
},
{
"id": 1349,
"product_id": 1,
"name": "Poster 12×16",
"size": "12×16",
"color": null,
"color_code": null,
"image": "https://d1yg28hrivmbqm.cloudfront.net/products/1/1349.jpg",
"price": "11.00"
},
But remember the customer wanted a poster that was 12x12? We only need to send the demand to print a 12x12 poster. So we need to send to the api the ID for the variant that matches the 12x12 size.
I need a way to search through each variant for a product and find the correct variant that matches the product_id of the poster, and the size requirements of 12x12.
{
"id": 4464,
"product_id": 1,
"name": "Poster 12×12",
"size": "12×12",
"color": null,
"color_code": null,
"image": "https://d1yg28hrivmbqm.cloudfront.net/products/1/4464.jpg",
"price": "9.00"
},
Once I find that correct variant, I need to collect all that information into a new array and return it.
//Get the product based on the supplied product_id. ($results)
//Break that array down into just the variants. ($vars)
//Search the $vars array for a block that matches the product_id.
//Search those $vars blocks for a single one that matches the size.
//If color is supplied, search those $vars blocks for a single one that matches the color.
//If size and color are supplied, a single block should be returned that matches all three variables (product_id, size, and color). Sometimes size and/or color is `null`. But a product_id is always supplied.
I hope the clears up what I am needing a little better.