' ', 'lastname' => ' ', ); protected $_customerAddressDefaultValues = array ( 'firstname' => ' ', 'lastname' => ' ', 'country_id' => 'DK', 'street' => ' ', 'postcode' => '9000', 'city' => 'Aalborg', 'telephone' => '131313' ); protected $_addressExternalIdAttr = "address_external_id"; protected $_customerExternalIdAttr = "customer_external_id"; protected $_idAttributesChecked = false; protected $_customerGroups = array(); protected $_attributeCodeChecks = array(); public function getCustomerGroups () { } public function getCustomers () { } public function fillGroupCodes() { if ( !count($this->_customerGroups) ) { $_customerGroups = Mage::getModel('customer/group')->getCollection(); foreach ( $_customerGroups as $group ) { $this->_customerGroups[$group->getCode()] = $group->getId(); } } } public function putCustomerGroups( $customer_groups ) { $this->fillGroupCodes(); foreach ( $customer_groups as $group ) { if ( !$this->_customerGroups[$group->getCode()] ) { $customer_group=Mage::getModel('customer/group'); $customer_group->setCode( $group->getCode() ); $customer_group->setTaxClassId(3); $customer_group->save(); //var_dump ( $customer_group ); //die('Saved customer group, okay?'); $this->_customerGroups[$group->getCode()] = 1; } } } public function addExternalIdAttribute( $attributeCode, $objectType ) { //$newAttributeName = "address_external_id"; $attribute = array( 'type' => 'varchar', 'label' => 'External ID', 'visible' => false, 'required' => false, 'user_defined' => false, 'searchable' => false, 'filterable' => false, 'comparable' => false, ); $setup = new Mage_Eav_Model_Entity_Setup('core_setup'); //Add to customer $setup->addAttribute($objectType, $attributeCode, $attribute); } public function addAddress ( $newAddress, $customer_id ) { $data = $newAddress->getData(); $address = Mage::getModel("customer/address"); foreach ( $this->_customerAddressDefaultValues as $_code => $_value ) { if ( $data[$_code] === NULL ) $data[$_code] = $_value; } $address->setData ( $data ); $address->setCustomerId($customer_id); $address->save(); } protected function array_equal($a, $b) { return (is_array($a) && is_array($b) && array_diff($a, $b) === array_diff($b, $a)); } public function compareObjects ( $localObject, $object ) { $data = $object->getData(); $localData = $localObject->getData(); $do_save = false; //var_dump ( $localData ); foreach ( $data as $_attributeCode => $_attributeValue ) { $array_check = false; if ( is_array ( $_attributeValue ) ) { $array_check = $this->array_equal ( $_attributeValue, $localObject->getData ( $_attributeCode ) ); } $street_check = false; if ( $_attributeCode == "street" ) { $street_check = ( ( (trim($localObject->getStreet(1)) == trim($_attributeValue[0])) && (trim($localObject->getStreet(2)) == trim($_attributeValue[1])) ) || (trim($localObject->getStreet(1)) == trim($_attributeValue[1]) && !$_attributeValue[0] ) ); } if ( $localObject instanceof Mage_Customer_Model_Customer ) $entity_type = 'customer'; if ( $localObject instanceof Mage_Customer_Model_Address ) $entity_type = 'customer_address'; if ( !isset ( $this->_attributeCodeChecks[$_attributeCode] ) ) { // is it a valid customer attribute? $attributeModel = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type, $_attributeCode); $attribute_check = (boolean)( $attributeModel->getId() ); $this->_attributeCodeChecks[$entity_type . "_" . $_attributeCode] = $attribute_check; } else { $attribute_check = $this->_attributeCodeChecks[$entity_type . "_" . $_attributeCode]; } if ( in_array ( $_attributeCode, $this->_updateAttributes ) && $_attributeValue != NULL && $_attributeValue != $localObject->getData ( $_attributeCode ) && ( array_key_exists ( $_attributeCode, $localData ) || $attribute_check ) && !$array_check && !$street_check ) { $localObject->setData ( $_attributeCode, $_attributeValue ); $this->logEvent ( 'Customers', 'Customers', "Attrbute $_attributeCode has changed\n" ); $do_save = true; } } return $do_save; } public function checkExternalIds() { if ( !$this->_idAttributesChecked ) { $add_entries = array ( 'customer_address' => $this->_addressExternalIdAttr, 'customer' => $this->_customerExternalIdAttr ); foreach ( $add_entries as $entity_type => $attribute_code ) { $attr = Mage::getModel('eav/entity_attribute')->loadByCode( $entity_type, $attribute_code ); if ( !$attr->getAttributeId() ) { $this->addExternalIdAttribute ( $attribute_code, $entity_type ); } else { } } $this->_idAttributesChecked = true; } } public function findAddressByExternalId ( $addressId ) { $collection = Mage::getResourceModel('customer/address_collection'); $result = $collection ->addAttributeToSelect($this->_addressExternalIdAttr) ->addAttributeToFilter($this->_addressExternalIdAttr, $addressId ) ->load(); foreach ( $result as $entry ) return $entry; return false; } public function findCustomerByExternalId ( $customer ) { $collection = Mage::getModel('customer/customer')->getCollection(); if ( $customer->getWebsiteIds() ) { $collection->addStaticField ( "website_id" ); $collection->addFieldToFilter ( "website_id", array ( "in" => $customer->getWebsiteIds() ) ); } $result = $collection ->addAttributeToSelect($this->_customerExternalIdAttr) ->addAttributeToFilter($this->_customerExternalIdAttr, $customer->getData($this->_customerExternalIdAttr) ) ->load(); // return the 1st entry foreach ( $result as $entry ) { $localCustomer = Mage::getModel('customer/customer')->load ( $entry->getId() ); return $localCustomer; } return false; } public function putCustomers ( $customers ) { $this->checkExternalIds(); $this->fillGroupCodes(); foreach ( $customers as $customer ) { //var_dump ( $customer ); if ( !$customer->getData ( $this->_customerExternalIdAttr ) ) continue; //$localCustomer = Mage::getModel("customer/customer"); //$localCustomer->setWebsiteId ( $customer->getWebsiteId() ); $entry = $this->findCustomerByExternalId ( $customer ); if ( $entry ) { $localCustomer = Mage::getModel("customer/customer")->load ( $entry->getId() ); } else { $localCustomer = Mage::getModel('customer/customer'); } $addresses = $customer->getCustomerAddresses(); $localAddresses = $localCustomer->getAddresses(); if ( $customer->getGroupCode() ) { $customer->setGroupId ( $this->_customerGroups[$customer->getGroupCode()] ); } if ( $localCustomer->getId() ) { // update operation $do_save = $this->compareObjects ( $localCustomer, $customer ); if ( $do_save ) { $this->logEvent ( 'Customers', 'Customers', "Saving a customer, email:".$localCustomer->getEmail()."\n"); $localCustomer->save(); } foreach ( $addresses as $address ) { $do_save = false; $matched = false; foreach ($localAddresses as $localAddress ) { $localAddress = Mage::getModel('customer/address')->load ( $localAddress->getId() ); $external_id = $localAddress->getData ( $this->_addressExternalIdAttr ); if ( $external_id && ( $external_id == $address->getData ( $this->_addressExternalIdAttr ) ) ) { $matched = true; $do_save = $this->compareObjects ( $localAddress, $address ); if ( !$localAddress->getIsDefaultBilling() && $address->getIsDefaultBilling() ) { $this->logEvent ( 'Customers', 'Customers', "Updating address $external_id, default addresses mismatch\n"); $localAddress->setIsDefaultBilling(1); $do_save = true; } if ( !$localAddress->getIsDefaultShipping() && $address->getIsDefaultShipping() ) { $this->logEvent ( 'Customers', 'Customers', "Updating address $external_id, default addresses mismatch\n"); $localAddress->setIsDefaultShipping(1); $do_save = true; } if ( $do_save ) { $this->logEvent ( 'Customers', 'Customers', "Updating address $external_id\n"); $localAddress->save(); } } } if ( !$matched ) { $this->addAddress ( $address, $localCustomer->getId() ); } } } else { // create operation $websites = $customer->getWebsiteIds(); if ( !count($websites) ) $websites = array(0); foreach ( $websites as $website_id ) { $newCustomer = Mage::getModel('customer/customer'); $newCustomer->setWebsiteId ( $website_id ); $newCustomer->setEmail ( $customer->getEmail() ); $_values = $customer->getData(); $this->logEvent ( 'Customers', 'Customers', "Adding new customer, email: " . $customer->getEmail() . "\n"); foreach ( $this->_customerDefaultValues as $_code => $_value ) { $newCustomer->setData ( $_code, $_value ); } foreach ( $_values as $_code => $_value ) { if ( ( in_array ( $_code, $this->_createAttributes ) || $_code == $this->_customerExternalIdAttr ) && strlen($_value)>0 ) $newCustomer->setData ( $_code, $_value ); } $newCustomer->save(); foreach ( $addresses as $address ) { $this->logEvent ( 'Customers', 'Customers', "Adding new address\n"); $this->addAddress ( $address, $newCustomer->getId() ); } } } } } }