Created
April 8, 2016 21:28
-
-
Save unlocomqx/c054941043b339c59a422cfbbac8e424 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function addCustomizedData(array $products, Cart $cart) | |
{ | |
return array_map(function (array $product) use ($cart) { | |
$product['customizations'] = []; | |
$data = Product::getAllCustomizedDatas($cart->id, null, true, null, (int)$product['id_customization']); | |
if (!$data) { | |
$data = []; | |
} | |
$id_product = (int) $product['id_product']; | |
$id_product_attribute = (int) $product['id_product_attribute']; | |
if (array_key_exists($id_product, $data)) { | |
if (array_key_exists($id_product_attribute, $data[$id_product])) { | |
foreach ($data[$id_product] as $byAddress) { | |
foreach ($byAddress as $customizations) { | |
foreach ($customizations as $customization) { | |
$presentedCustomization = [ | |
'quantity' => $customization['quantity'], | |
'fields' => [], | |
'id_customization' => null, | |
]; | |
foreach ($customization['datas'] as $byType) { | |
foreach ($byType as $data) { | |
$field = []; | |
switch ($data['type']) { | |
case Product::CUSTOMIZE_FILE: | |
$field['type'] = 'image'; | |
$field['image'] = $this->imageRetriever->getCustomizationImage( | |
$data['value'] | |
); | |
break; | |
case Product::CUSTOMIZE_TEXTFIELD: | |
$field['type'] = 'text'; | |
$field['text'] = $data['value']; | |
break; | |
default: | |
$field['type'] = null; | |
} | |
$field['label'] = $data['name']; | |
$presentedCustomization['id_customization'] = $data['id_customization']; | |
$presentedCustomization['fields'][] = $field; | |
} | |
} | |
$product['up_quantity_url'] = $this->link->getUpQuantityCartURL( | |
$product['id_product'], | |
$product['id_product_attribute'], | |
$presentedCustomization['id_customization'] | |
); | |
$product['down_quantity_url'] = $this->link->getDownQuantityCartURL( | |
$product['id_product'], | |
$product['id_product_attribute'], | |
$presentedCustomization['id_customization'] | |
); | |
$product['remove_from_cart_url'] = $this->link->getRemoveFromCartURL( | |
$product['id_product'], | |
$product['id_product_attribute'], | |
$presentedCustomization['id_customization'] | |
); | |
$presentedCustomization['up_quantity_url'] = $this->link->getUpQuantityCartURL( | |
$product['id_product'], | |
$product['id_product_attribute'], | |
$presentedCustomization['id_customization'] | |
); | |
$presentedCustomization['down_quantity_url'] = $this->link->getDownQuantityCartURL( | |
$product['id_product'], | |
$product['id_product_attribute'], | |
$presentedCustomization['id_customization'] | |
); | |
$presentedCustomization['remove_from_cart_url'] = $this->link->getRemoveFromCartURL( | |
$product['id_product'], | |
$product['id_product_attribute'], | |
$presentedCustomization['id_customization'] | |
); | |
$product['customizations'][] = $presentedCustomization; | |
} | |
} | |
} | |
} | |
} | |
usort($product['customizations'], function (array $a, array $b) { | |
if ( | |
$a['quantity'] > $b['quantity'] | |
|| count($a['fields']) > count($b['fields']) | |
|| $a['id_customization'] > $b['id_customization'] | |
) { | |
return -1; | |
} else { | |
return 1; | |
} | |
}); | |
return $product; | |
}, $products); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment