Skip to content

Instantly share code, notes, and snippets.

@unlocomqx
Created April 8, 2016 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unlocomqx/c054941043b339c59a422cfbbac8e424 to your computer and use it in GitHub Desktop.
Save unlocomqx/c054941043b339c59a422cfbbac8e424 to your computer and use it in GitHub Desktop.
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