public function testSetCustomAttributes() { $customerAttributes = ['warehouse_zip' => [AttributeValue::ATTRIBUTE_CODE => 'warehouse_zip', AttributeValue::VALUE => '78777'], 'warehouse_alternate' => [AttributeValue::ATTRIBUTE_CODE => 'warehouse_alternate', AttributeValue::VALUE => '90051']]; $attributeValue1 = $this->_valueBuilder->populateWithArray($customerAttributes['warehouse_zip'])->create(); $attributeValue2 = $this->_valueBuilder->populateWithArray($customerAttributes['warehouse_alternate'])->create(); $address = $this->_customerBuilder->setCustomAttributes([$attributeValue1, $attributeValue2])->create(); $this->assertEquals('78777', $address->getCustomAttribute('warehouse_zip')->getValue()); $this->assertEquals('90051', $address->getCustomAttribute('warehouse_alternate')->getValue()); $this->assertEquals($customerAttributes, $address->__toArray()[Customer::CUSTOM_ATTRIBUTES_KEY]); }
/** * Convert custom attribute data array to array of AttributeValue Data Object * * @param array $customAttributesValueArray * @param string $returnType * @param string $dataObjectClassName * @return AttributeValue[] */ protected function convertCustomAttributeValue($customAttributesValueArray, $returnType, $dataObjectClassName) { $result = []; $allAttributes = $this->serviceConfigReader->read(); $dataObjectClassName = ltrim($dataObjectClassName, '\\'); if (!isset($allAttributes[$dataObjectClassName])) { return $this->_convertValue($customAttributesValueArray, $returnType); } $dataObjectAttributes = $allAttributes[$dataObjectClassName]; $camelCaseAttributeCodeKey = lcfirst(SimpleDataObjectConverter::snakeCaseToUpperCamelCase(AttributeValue::ATTRIBUTE_CODE)); foreach ($customAttributesValueArray as $customAttribute) { if (isset($customAttribute[AttributeValue::ATTRIBUTE_CODE])) { $customAttributeCode = $customAttribute[AttributeValue::ATTRIBUTE_CODE]; } else { if (isset($customAttribute[$camelCaseAttributeCodeKey])) { $customAttributeCode = $customAttribute[$camelCaseAttributeCodeKey]; } else { $customAttributeCode = null; } } //Check if type is defined, else default to mixed $type = isset($dataObjectAttributes[$customAttributeCode]) ? $dataObjectAttributes[$customAttributeCode] : TypeProcessor::ANY_TYPE; $customAttributeValue = $customAttribute[AttributeValue::VALUE]; if (is_array($customAttributeValue)) { //If type for AttributeValue's value as array is mixed, further processing is not possible if ($type === TypeProcessor::ANY_TYPE) { continue; } //If custom attribute value is an array then its a data object type $attributeValue = $this->_createFromArray($type, $customAttributeValue); } else { $attributeValue = $this->_convertValue($customAttributeValue, $type); } //Populate the attribute value data object once the value for custom attribute is derived based on type $result[] = $this->attributeValueBuilder->setAttributeCode($customAttributeCode)->setValue($attributeValue)->create(); } return $result; }