function variationRow(&$obj, $val, $record) { $obj->write(); //make sure product is in DB //TODO: or find existing variation $variation = DataObject::get_one('ProductVariation', "InternalItemID = '{$val}'"); if (!$variation) { $variation = new ProductVariation(); $variation->InternalItemID = $val; $variation->ProductID = $obj->ID; //link to product $variation->write(); } $varcols = array('->processVariation', '->processVariation1', '->processVariation2', '->processVariation3', '->processVariation4', '->processVariation5', '->processVariation6'); foreach ($varcols as $col) { if (isset($record[$col])) { $parts = explode(":", $record[$col]); if (count($parts) == 2) { $attributetype = trim($parts[0]); $attributevalues = explode(",", $parts[1]); //get rid of empty values foreach ($attributevalues as $key => $value) { if (!$value || trim($value) == "") { unset($attributevalues[$key]); } } if (count($attributevalues) == 1) { $attributetype = ProductAttributeType::find_or_make($attributetype); foreach ($attributevalues as $key => $value) { $val = trim($value); if ($val != "" && $val != null) { $attributevalues[$key] = $val; } //remove outside spaces from values } $attributetype->addValues($attributevalues); //create and add values to attribute type $obj->VariationAttributes()->add($attributetype); //add variation attribute type to product //TODO: if existing variation, then remove current values //record vairation attribute values (variation1, 2 etc) foreach ($attributetype->convertArrayToValues($attributevalues) as $value) { $variation->AttributeValues()->add($value); break; } } } } } //copy db values into variation (InternalItemID, Price, Stock, etc) ...there will be unknowns from extensions. $dbfields = $variation->db(); foreach ($record as $field => $value) { if (isset($dbfields[$field])) { $variation->{$field} = $value; } } $variation->write(); }
/** * add an attribute type to the product * * @param String | Int | ProductAttributeType $attributeTypeObject * * @return ProductAttributeType */ function addAttributeType($attributeTypeObject) { if (intval($attributeTypeObject) === $attributeTypeObject) { $attributeTypeObject = ProductAttributeType::get()->byID(intval($attributeTypeObject)); } if (is_string($attributeTypeObject)) { $attributeTypeObject = ProductAttributeType::find_or_make($attributeTypeObject); } if ($attributeTypeObject && $attributeTypeObject instanceof ProductAttributeType) { $existingTypes = $this->owner->VariationAttributes(); $existingTypes->add($attributeTypeObject); return $attributeTypeObject; } else { user_error($attributeTypeObject . " is broken"); } }