/** * Check whether specified address should be processed in after_save event handler * * @param Mage_Customer_Model_Address $address * @return bool */ protected function _canProcessAddress($address) { if ($address->getForceProcess()) { return true; } if (Mage::registry(self::VIV_CURRENTLY_SAVED_ADDRESS) != $address->getId()) { return false; } $configAddressType = Mage::helper('customer/address')->getTaxCalculationAddressType(); if ($configAddressType == Mage_Customer_Model_Address_Abstract::TYPE_SHIPPING) { return $this->_isDefaultShipping($address); } return $this->_isDefaultBilling($address); }
/** * Collect address data to xml node * Remove objects from data array and escape data values * * @param Mage_Customer_Model_Address $address * @param Mage_XmlConnect_Model_Simplexml_Element $item * @return array */ public function prepareAddressData(Mage_Customer_Model_Address $address, Mage_XmlConnect_Model_Simplexml_Element $item) { if (!$address) { return array(); } $attributes = Mage::helper('Mage_Customer_Helper_Address')->getAttributes(); $data = array('entity_id' => $address->getId()); foreach ($attributes as $attribute) { /* @var $attribute Mage_Customer_Model_Attribute */ if (!$attribute->getIsVisible()) { continue; } if ($attribute->getAttributeCode() == 'country_id') { $data['country'] = $address->getCountryModel()->getName(); $data['country_id'] = $address->getCountryId(); } else { if ($attribute->getAttributeCode() == 'region') { $data['region'] = $address->getRegion(); } else { $dataModel = Mage_Customer_Model_Attribute_Data::factory($attribute, $address); $value = $dataModel->outputValue(Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_ONELINE); if ($attribute->getFrontendInput() == 'multiline') { $values = $dataModel->outputValue(Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_ARRAY); // explode lines foreach ($values as $k => $v) { $key = sprintf('%s%d', $attribute->getAttributeCode(), $k + 1); $data[$key] = $v; } } $data[$attribute->getAttributeCode()] = $value; } } } foreach ($data as $key => $value) { if (empty($value)) { continue; } $item->addChild($key, $item->escapeXml($value)); } }
/** * Check if address is primary * * @param Mage_Customer_Model_Address $address * @return boolean */ public function isAddressPrimary(Mage_Customer_Model_Address $address) { if (!$address->getId()) { return false; } return $address->getId() == $this->getDefaultBilling() || $address->getId() == $this->getDefaultShipping(); }
/** * @deprecated not used anymore */ public function parse() { $data = $this->getData(); $entityTypeId = Mage::getSingleton('eav/config')->getEntityType('customer')->getId(); $result = array(); foreach ($data as $i => $row) { $this->setPosition('Line: ' . ($i + 1)); try { // validate SKU if (empty($row['email'])) { $this->addException(Mage::helper('customer')->__('Missing email, skipping the record'), Varien_Convert_Exception::ERROR); continue; } $this->setPosition('Line: ' . ($i + 1) . ', email: ' . $row['email']); // try to get entity_id by sku if not set /* if (empty($row['entity_id'])) { $row['entity_id'] = $this->getResource()->getProductIdBySku($row['email']); } */ // if attribute_set not set use default if (empty($row['attribute_set'])) { $row['attribute_set'] = 'Default'; } // get attribute_set_id, if not throw error $row['attribute_set_id'] = $this->getAttributeSetId($entityTypeId, $row['attribute_set']); if (!$row['attribute_set_id']) { $this->addException(Mage::helper('customer')->__("Invalid attribute set specified, skipping the record"), Varien_Convert_Exception::ERROR); continue; } if (empty($row['group'])) { $row['group'] = 'General'; } if (empty($row['firstname'])) { $this->addException(Mage::helper('customer')->__('Missing firstname, skipping the record'), Varien_Convert_Exception::ERROR); continue; } //$this->setPosition('Line: '.($i+1).', Firstname: '.$row['firstname']); if (empty($row['lastname'])) { $this->addException(Mage::helper('customer')->__('Missing lastname, skipping the record'), Varien_Convert_Exception::ERROR); continue; } //$this->setPosition('Line: '.($i+1).', Lastname: '.$row['lastname']); /* // get product type_id, if not throw error $row['type_id'] = $this->getProductTypeId($row['type']); if (!$row['type_id']) { $this->addException(Mage::helper('catalog')->__("Invalid product type specified, skipping the record"), Varien_Convert_Exception::ERROR); continue; } */ // get store ids $storeIds = $this->getStoreIds(isset($row['store']) ? $row['store'] : $this->getVar('store')); if (!$storeIds) { $this->addException(Mage::helper('customer')->__("Invalid store specified, skipping the record"), Varien_Convert_Exception::ERROR); continue; } // import data $rowError = false; foreach ($storeIds as $storeId) { $collection = $this->getCollection($storeId); //print_r($collection); $entity = $collection->getEntity(); $model = Mage::getModel('customer/customer'); $model->setStoreId($storeId); if (!empty($row['entity_id'])) { $model->load($row['entity_id']); } foreach ($row as $field => $value) { $attribute = $entity->getAttribute($field); if (!$attribute) { continue; #$this->addException(Mage::helper('catalog')->__("Unknown attribute: %s", $field), Varien_Convert_Exception::ERROR); } if ($attribute->usesSource()) { $source = $attribute->getSource(); $optionId = $this->getSourceOptionId($source, $value); if (is_null($optionId)) { $rowError = true; $this->addException(Mage::helper('customer')->__("Invalid attribute option specified for attribute %s (%s), skipping the record", $field, $value), Varien_Convert_Exception::ERROR); continue; } $value = $optionId; } $model->setData($field, $value); } //foreach ($row as $field=>$value) $billingAddress = $model->getPrimaryBillingAddress(); $customer = Mage::getModel('customer/customer')->load($model->getId()); if (!$billingAddress instanceof Mage_Customer_Model_Address) { $billingAddress = new Mage_Customer_Model_Address(); if ($customer->getId() && $customer->getDefaultBilling()) { $billingAddress->setId($customer->getDefaultBilling()); } } $regions = Mage::getResourceModel('directory/region_collection')->addRegionNameFilter($row['billing_region'])->load(); if ($regions) { foreach ($regions as $region) { $regionId = $region->getId(); } } $billingAddress->setFirstname($row['firstname']); $billingAddress->setLastname($row['lastname']); $billingAddress->setCity($row['billing_city']); $billingAddress->setRegion($row['billing_region']); $billingAddress->setRegionId($regionId); $billingAddress->setCountryId($row['billing_country']); $billingAddress->setPostcode($row['billing_postcode']); $billingAddress->setStreet(array($row['billing_street1'], $row['billing_street2'])); if (!empty($row['billing_telephone'])) { $billingAddress->setTelephone($row['billing_telephone']); } if (!$model->getDefaultBilling()) { $billingAddress->setCustomerId($model->getId()); $billingAddress->setIsDefaultBilling(true); $billingAddress->save(); $model->setDefaultBilling($billingAddress->getId()); $model->addAddress($billingAddress); if ($customer->getDefaultBilling()) { $model->setDefaultBilling($customer->getDefaultBilling()); } else { $shippingAddress->save(); $model->setDefaultShipping($billingAddress->getId()); $model->addAddress($billingAddress); } } $shippingAddress = $model->getPrimaryShippingAddress(); if (!$shippingAddress instanceof Mage_Customer_Model_Address) { $shippingAddress = new Mage_Customer_Model_Address(); if ($customer->getId() && $customer->getDefaultShipping()) { $shippingAddress->setId($customer->getDefaultShipping()); } } $regions = Mage::getResourceModel('directory/region_collection')->addRegionNameFilter($row['shipping_region'])->load(); if ($regions) { foreach ($regions as $region) { $regionId = $region->getId(); } } $shippingAddress->setFirstname($row['firstname']); $shippingAddress->setLastname($row['lastname']); $shippingAddress->setCity($row['shipping_city']); $shippingAddress->setRegion($row['shipping_region']); $shippingAddress->setRegionId($regionId); $shippingAddress->setCountryId($row['shipping_country']); $shippingAddress->setPostcode($row['shipping_postcode']); $shippingAddress->setStreet(array($row['shipping_street1'], $row['shipping_street2'])); $shippingAddress->setCustomerId($model->getId()); if (!empty($row['shipping_telephone'])) { $shippingAddress->setTelephone($row['shipping_telephone']); } if (!$model->getDefaultShipping()) { if ($customer->getDefaultShipping()) { $model->setDefaultShipping($customer->getDefaultShipping()); } else { $shippingAddress->save(); $model->setDefaultShipping($shippingAddress->getId()); $model->addAddress($shippingAddress); } $shippingAddress->setIsDefaultShipping(true); } if (!$rowError) { $collection->addItem($model); } } //foreach ($storeIds as $storeId) } catch (Exception $e) { if (!$e instanceof Mage_Dataflow_Model_Convert_Exception) { $this->addException(Mage::helper('customer')->__("Error during retrieval of option value: %s", $e->getMessage()), Mage_Dataflow_Model_Convert_Exception::FATAL); } } } $this->setData($this->_collections); return $this; }
/** * Is specified address a default shipping address? * * @param Mage_Customer_Model_Address $address * @return bool */ protected function _isDefaultShippingAddress(Mage_Customer_Model_Address $address) { return $address->getCustomer()->getDefaultShipping() == $address->getId(); }
/** * Import quote address data from customer address object * * @param Mage_Customer_Model_Address $address * @return Mage_Sales_Model_Quote_Address */ public function importCustomerAddress(Mage_Customer_Model_Address $address) { $this->setCustomerAddressId($address->getId())->setCustomerId($address->getParentId())->setEmail($address->hasEmail() ? $address->getEmail() : $address->getCustomer()->getEmail())->setFirstname($address->getFirstname())->setLastname($address->getLastname())->setCompany($address->getCompany())->setStreet($address->getStreet())->setCity($address->getCity())->setRegion($address->getRegion())->setRegionId($address->getRegionId())->setPostcode($address->getPostcode())->setCountryId($address->getCountryId())->setTelephone($address->getTelephone())->setFax($address->getFax()); return $this; }
/** * Importing customer data from text array * * @param array $row * @return uMage_Customer_Model_Customer */ public function importFromTextArray(array $row) { $this->resetErrors(); $hlp = Mage::helper('customer'); $line = $row['i']; $row = $row['row']; $regions = Mage::getResourceModel('directory/region_collection'); // $config = Mage::getSingleton('eav/config')->getEntityType('customer'); $website = Mage::getModel('core/website')->load($row['website_code'], 'code'); if (!$website->getId()) { $this->addError($hlp->__('Invalid website, skipping the record, line: %s.', $line)); } else { $row['website_id'] = $website->getWebsiteId(); $this->setWebsiteId($row['website_id']); } // Validate Email if (empty($row['email'])) { $this->addError($hlp->__('Missing email, skipping the record, line: %s.', $line)); } else { $this->loadByEmail($row['email']); } if (empty($row['entity_id'])) { if ($this->getData('entity_id')) { $this->addError($hlp->__('The customer email (%s) already exists, skipping the record, line: %s.', $row['email'], $line)); } } else { if ($row['entity_id'] != $this->getData('entity_id')) { $this->addError($hlp->__('The customer ID and email did not match, skipping the record, line: %s.', $line)); } else { $this->unsetData(); $this->load($row['entity_id']); if (isset($row['store_view'])) { $storeId = Mage::app()->getStore($row['store_view'])->getId(); if ($storeId) { $this->setStoreId($storeId); } } } } if (empty($row['website_code'])) { $this->addError($hlp->__('Missing website, skipping the record, line: %s.', $line)); } if (empty($row['group'])) { $row['group'] = 'General'; } if (empty($row['firstname'])) { $this->addError($hlp->__('Missing first name, skipping the record, line: %s.', $line)); } if (empty($row['lastname'])) { $this->addError($hlp->__('Missing last name, skipping the record, line: %s.', $line)); } if (!empty($row['password_new'])) { $this->setPassword($row['password_new']); unset($row['password_new']); if (!empty($row['password_hash'])) { unset($row['password_hash']); } } if ($errors = $this->getErrors()) { $this->unsetData(); $this->printError(join("<br />", $errors)); return; } // $entity = $this->getResource(); foreach ($row as $field => $value) { // $attribute = $entity->getAttribute($field); // if (!$attribute) { // echo $field; // continue; // } // if ($attribute->usesSource()) { // $source = $attribute->getSource(); // $optionId = $config->getSourceOptionId($source, $value); // if (is_null($optionId)) { // $this->printError($hlp->__("Invalid attribute option specified for attribute attribute %s (%s).", $field, $value), $line); // } // $value = $optionId; // } $this->setData($field, $value); } if (!$this->validateAddress($row, 'billing')) { $this->printError($hlp->__('Invalid billing address for (%s).', $row['email']), $line); } else { // Handling billing address $billingAddress = $this->getPrimaryBillingAddress(); if (!$billingAddress instanceof Mage_Customer_Model_Address) { $billingAddress = new Mage_Customer_Model_Address(); } $regions->addRegionNameFilter($row['billing_region'])->load(); if ($regions) { foreach ($regions as $region) { $regionId = $region->getId(); } } $billingAddress->setFirstname($row['firstname']); $billingAddress->setLastname($row['lastname']); $billingAddress->setCity($row['billing_city']); $billingAddress->setRegion($row['billing_region']); if (isset($regionId)) { $billingAddress->setRegionId($regionId); } $billingAddress->setCountryId($row['billing_country']); $billingAddress->setPostcode($row['billing_postcode']); if (isset($row['billing_street2'])) { $billingAddress->setStreet(array($row['billing_street1'], $row['billing_street2'])); } else { $billingAddress->setStreet(array($row['billing_street1'])); } if (isset($row['billing_telephone'])) { $billingAddress->setTelephone($row['billing_telephone']); } if (!$billingAddress->getId()) { $billingAddress->setIsDefaultBilling(true); if ($this->getDefaultBilling()) { $this->setData('default_billing', ''); } $this->addAddress($billingAddress); } // End handling billing address } if (!$this->validateAddress($row, 'shipping')) { $this->printError($hlp->__('Invalid shipping address for (%s).', $row['email']), $line); } else { // Handling shipping address $shippingAddress = $this->getPrimaryShippingAddress(); if (!$shippingAddress instanceof Mage_Customer_Model_Address) { $shippingAddress = new Mage_Customer_Model_Address(); } $regions->addRegionNameFilter($row['shipping_region'])->load(); if ($regions) { foreach ($regions as $region) { $regionId = $region->getId(); } } $shippingAddress->setFirstname($row['firstname']); $shippingAddress->setLastname($row['lastname']); $shippingAddress->setCity($row['shipping_city']); $shippingAddress->setRegion($row['shipping_region']); if (isset($regionId)) { $shippingAddress->setRegionId($regionId); } $shippingAddress->setCountryId($row['shipping_country']); $shippingAddress->setPostcode($row['shipping_postcode']); if (isset($row['shipping_street2'])) { $shippingAddress->setStreet(array($row['shipping_street1'], $row['shipping_street2'])); } else { $shippingAddress->setStreet(array($row['shipping_street1'])); } if (!empty($row['shipping_telephone'])) { $shippingAddress->setTelephone($row['shipping_telephone']); } if (!$shippingAddress->getId()) { $shippingAddress->setIsDefaultShipping(true); $this->addAddress($shippingAddress); } // End handling shipping address } if (!empty($row['is_subscribed'])) { $this->setIsSubscribed(strtolower($row['is_subscribed']) == self::SUBSCRIBED_YES ? 1 : 0); } unset($row); return $this; }
/** * @param Mage_Customer_Model_Address $customerAddress * @param array $randomData */ protected function _anonymizeOrderAddresses($customerAddress, $randomData) { $orderAddresses = Mage::getModel('sales/order_address')->getCollection()->addFieldToFilter('customer_address_id', $customerAddress->getId()); foreach ($orderAddresses as $orderAddress) { $this->_anonymizeOrderAddress($orderAddress, $randomData); } }
/** * @param array $addressData * @param Mage_Customer_Model_Address|null $newAddress * @param Mage_Customer_Model_Address|null $existingAddress * @param array $addressCollection * @param bool $dataChanged * @param bool $expectedDataChange * @dataProvider addressesDataProvider */ public function testPrepareCustomerAddressForSave(array $addressData, $newAddress, $existingAddress, array $addressCollection, $dataChanged, $expectedDataChange) { $this->_customer->setDataChanges($dataChanged); $this->_customer->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->_customer->expects($this->once())->method('getAddressesCollection')->will($this->returnValue($addressCollection)); if ($newAddress) { // Check that customer_id is set for new addresses $newAddress->expects($this->once())->method('setCustomerId')->with(1); // Check that new address is added to customer address collection $this->_customer->expects($this->once())->method('addAddress'); // Check that new address is not deleted $newAddress->expects($this->never())->method('setData')->with('_deleted', true); } // Check that address loaded from customer address collection if ($existingAddress && $addressData) { $this->_customer->expects($this->once())->method('getAddressItemById')->with($existingAddress->getId())->will($this->returnValue($existingAddress)); } // Check that new data is added $hasExistingAddress = false; foreach ($addressData as $data) { if (array_key_exists('entity_id', $addressData)) { unset($data['entity_id']); $existingAddress->expects($this->once())->method('addData')->with($data); $hasExistingAddress = true; } elseif ($newAddress) { $newAddress->expects($this->once())->method('addData')->with($data); } } // Check that addresses is deleted if (in_array($existingAddress, $addressCollection) && !$hasExistingAddress) { $existingAddress->expects($this->atLeastOnce())->method('setData')->with('_deleted', true); } $this->assertInstanceOf('Mage_Customer_Model_Customer', $this->_service->update(1, array(), $addressData), 'Incorrect instance returned'); $this->assertEquals($expectedDataChange, $this->_customer->hasDataChanges(), 'Customer change data status is incorrect'); }