function extractSubItemData($productAndOptions, $order = null) { //debug($productAndOptions); $currency = !empty($order['ShopOrder']['currency']) ? $order['ShopOrder']['currency'] : ShopConfig::load('currency'); $extract_data = array('shop_product_subproduct_id' => 'ShopProductSubproduct.id', 'shop_subproduct_id' => 'ShopSubproduct.id', 'descr' => array('ShopSubproduct.label'), 'nb' => array('nb'), 'id' => array('id'), 'item_price' => array('ShopSubproduct.currency_prices.' . $currency, 'ShopSubproduct.price'), 'item_operator' => array('ShopSubproduct.operator'), 'type' => 'ShopSubproduct.type', 'ShopSubproduct' => 'ShopSubproduct'); if (!empty($order['ShopOrder']['confirm'])) { $extract_data = Set::merge(array('descr' => array('descr'), 'item_operator' => array('item_operator'), 'item_price' => array('item_price')), $extract_data); } $orderItemMode = isset($productAndOptions['item_price']); if ($orderItemMode) { $p =& $productAndOptions; } else { $p = $this->extractOrderItemData($p2 = $productAndOptions, $order); } $cur_price = $p['item_price']; $p['item_alone_price'] = $cur_price; App::import('Lib', 'Shop.ShopConfig'); $types = ShopConfig::getSubProductTypes(); $subItems = null; if (!empty($types) && !empty($p['SubItem'])) { $this->ShopSubproduct = ClassRegistry::init('Shop.ShopSubproduct'); $ids = array(); //============ normalize data ============// $subItems = $this->_getNormalizedSubItem($types, $p['SubItem'], $ids); //debug($ids); //============ fetch SubProducts ============// $this->ShopSubproduct->ShopProductSubproduct->Behaviors->attach('Containable'); $this->ShopSubproduct->ShopProductSubproduct->contain(array('ShopSubproduct')); $subProduct = $this->ShopSubproduct->ShopProductSubproduct->find('all', array('conditions' => array('ShopProductSubproduct.shop_product_id' => $p['product_id'], 'ShopProductSubproduct.shop_subproduct_id' => $ids))); $subProduct = SetMulti::group($subProduct, 'ShopSubproduct.id', array('singleArray' => false)); //debug($subProduct); foreach ($subItems as $type => &$items) { foreach ($items as &$subItem) { if (!empty($subProduct[$subItem['id']])) { $subItem = array_merge($subProduct[$subItem['id']], $subItem); } unset($subItem); } } } if (!empty($productAndOptions['ShopOrdersSubitem'])) { $subItems = SetMulti::group($productAndOptions['ShopOrdersSubitem'], 'ShopSubproduct.type'); } if (!empty($types) && !empty($subItems)) { //============ merge data ============// $finalSubItems = array(); foreach ($subItems as $type => &$items) { foreach ($items as &$subItem) { $data = SetMulti::extractHierarchicMulti($extract_data, $subItem); if (!empty($subItem['parent'])) { $data['parent'] =& $subItem['parent']; } SetMulti::replaceRef($subItem, $data); $finalSubItems[] =& $subItem; unset($subItem); } unset($items); } //debug($finalSubItems); return $finalSubItems; } return null; }