/** * @param JBCartVariant $variant * @return array */ public function getVariantData(JBCartVariant $variant) { $vars = $this->app->jbvars; $data = array(); if ($variant->isBasic()) { $elements = $variant->getCore(); } elseif ($variant->is(-1)) { $elements = $variant->all(); } else { $elements = $variant->getSimple(); } if (count($elements)) { /**@type JBCartElementPrice $element */ foreach ($elements as $paramId => $element) { $value = $element->getSearchData(); $value = $this->_helper->getValue($value); $string = (string) $value; $numeric = is_numeric($value) ? $vars->number($value) : null; $date = $this->_helper->isDate($value) ?: null; if (!$this->_helper->isEmpty($string) || (is_numeric($numeric) || !$this->_helper->isEmpty($date))) { $key = $this->_item->id . '__' . $this->identifier . '__' . $variant->getId() . '__' . $element->id(); $data[$key] = array('item_id' => $this->_item->id, 'element_id' => $this->identifier, 'param_id' => $element->id(), 'value_s' => $string, 'value_n' => $numeric, 'value_d' => $date, 'variant' => $variant->getId()); } } } return $data; }
/** * @param JBCartVariant $variant * @return $this */ public function bindVariant(JBCartVariant $variant) { if ($this->_item !== null) { $simple = $variant->getSimple(); $data = $this->data(); $values = (array) $data->get('values', array()); $selected = (array) $data->get('selected', array()); $variations = (array) $data->get('variations', array()); $variations[$variant->getId()] = $variant->data(); if (!$variant->isBasic()) { $values[$variant->getId()] = array_filter(array_map(create_function('$element', 'return JString::strlen($element->getValue(true)) > 0 ? (array)$element->data() : null;'), $simple)); $_selected = array_filter(array_map(create_function('$element', 'return JString::strlen($element->getValue(true)) > 0 ? JString::trim($element->getValue(true)) : null;'), $simple)); if ($_selected) { foreach ($_selected as $key => $value) { $selected[$key][$value] = $value; } } } // Reset keys $variations = array_values($variations); $values = array_values($values); $data->set('variations', $variations); $data->set('selected', $selected); $data->set('values', $variant->isBasic() ? array(self::BASIC_VARIANT => array()) : $values); $this->_item->elements->set($this->identifier, (array) $data); } return $this; }