/**
  * Save customer
  *
  * @param Mage_Customer_Model_Customer $order
  */
 protected function _saveCustomerAfterOrder($order)
 {
     if ($this->_customer) {
         if (!$this->_customer->getId()) {
             $this->_customer->save();
             $order->setCustomerId($this->_customer->getId());
             $this->getBillingAddress()->setCustomerId($this->_customer->getId());
             $this->getShippingAddress()->setCustomerId($this->_customer->getId());
             $this->_customer->sendNewAccountEmail();
         } else {
             $saveCusstomerAddress = false;
             if ($this->getBillingAddress()->getSaveInAddressBook()) {
                 $billingAddress = $this->getBillingAddress()->exportCustomerAddress();
                 if ($this->getBillingAddress()->getCustomerAddressId()) {
                     $billingAddress->setId($this->getBillingAddress()->getCustomerAddressId());
                 }
                 $this->_customer->addAddress($billingAddress);
                 $saveCusstomerAddress = true;
             }
             if ($this->getShippingAddress()->getSaveInAddressBook()) {
                 $shippingAddress = $this->getShippingAddress()->exportCustomerAddress();
                 if ($this->getShippingAddress()->getCustomerAddressId()) {
                     $shippingAddress->setId($this->getShippingAddress()->getCustomerAddressId());
                 }
                 $this->_customer->addAddress($shippingAddress);
                 $saveCusstomerAddress = true;
             }
             if ($saveCusstomerAddress) {
                 $this->_customer->save();
             }
         }
     }
 }
Example #2
0
 /**
  * Save customer
  *
  * @deprecated after 1.4.0.0.
  * @param Mage_Customer_Model_Customer $order
  */
 protected function _saveCustomerAfterOrder($order)
 {
     if ($this->_customer) {
         if (!$this->_customer->getId()) {
             $billing = $this->getBillingAddress();
             $customerBilling = $billing->exportCustomerAddress();
             $shipping = $this->getShippingAddress();
             $customerShipping = $shipping->exportCustomerAddress();
             $this->_customer->addAddress($customerBilling);
             if (!$shipping->getSameAsBilling()) {
                 $this->_customer->addAddress($customerShipping);
             }
             // preliminary save to find addresses id
             $this->_customer->save();
             // setting default addresses id
             $defShipping = $shipping->getSameAsBilling() ? $customerBilling->getId() : $customerShipping->getId();
             $this->_customer->setDefaultBilling($customerBilling->getId())->setDefaultShipping($defShipping)->save();
             $order->setCustomerId($this->_customer->getId());
             $billing->setCustomerId($this->_customer->getId());
             $shipping->setCustomerId($this->_customer->getId());
             $this->_customer->sendNewAccountEmail('registered', '', $order->getStoreId());
         } else {
             $saveCusstomerAddress = false;
             if ($this->getBillingAddress()->getSaveInAddressBook()) {
                 $billingAddress = $this->getBillingAddress()->exportCustomerAddress();
                 if ($this->getBillingAddress()->getCustomerAddressId()) {
                     $billingAddress->setId($this->getBillingAddress()->getCustomerAddressId());
                 }
                 $this->_customer->addAddress($billingAddress);
                 $saveCusstomerAddress = true;
             }
             if ($this->getShippingAddress()->getSaveInAddressBook()) {
                 $shippingAddress = $this->getShippingAddress()->exportCustomerAddress();
                 if ($this->getShippingAddress()->getCustomerAddressId()) {
                     $shippingAddress->setId($this->getShippingAddress()->getCustomerAddressId());
                 }
                 $this->_customer->addAddress($shippingAddress);
                 $saveCusstomerAddress = true;
             }
             if ($saveCusstomerAddress) {
                 $this->_customer->save();
             }
         }
     }
 }
 /**
  * Setup shipping address and set as customer default if indicated.
  *
  * @param                                $customer Mage_Customer_Model_Customer
  * @param Mage_Sales_Model_Quote_Address $shipping
  *
  * @return Mage_Sales_Model_Quote_Address $shipping | null
  */
 protected function _prepareCustomerShipping(Mage_Customer_Model_Customer $customer, Mage_Sales_Model_Quote_Address $shipping)
 {
     if ($shipping && (!$shipping->getCustomerId() && !$shipping->getSameAsBilling() || !$shipping->getSameAsBilling() && $shipping->getSaveInAddressBook())) {
         $customerShipping = $shipping->exportCustomerAddress();
         $customer->addAddress($customerShipping);
         $shipping->setCustomerAddress($customerShipping);
         return $customerShipping;
     }
     return null;
 }
Example #4
0
 /**
  * Parse MailChimp <address> MergeField type to Magento's address object
  *
  * @param array $data MC address data
  * @param string $type billing or shipping
  * @param Mage_Customer_Model_Customer $customer
  * @return array Empty if noy errors, or a list of errors in an Array
  */
 protected function _McAddressToMage(array $data, $type, $customer)
 {
     $addressData = $data["{$type}_address"];
     $address = explode(str_repeat(' ', 2), $addressData);
     list($addr1, $addr2, $city, $state, $zip, $country) = $address;
     $region = Mage::getModel('directory/region')->loadByName($state, $country);
     $mgAddress = array('firstname' => $data['firstname'], 'lastname' => $data['lastname'], 'street' => array($addr1, $addr2), 'city' => $city, 'country_id' => $country, 'region' => $state, 'region_id' => !is_null($region->getId()) ? $region->getId() : null, 'postcode' => $zip, 'telephone' => 'not_provided');
     /* @var $address Mage_Customer_Model_Address */
     $address = Mage::getModel('customer/address');
     /* @var $addressForm Mage_Customer_Model_Form */
     $addressForm = Mage::getModel('customer/form');
     $addressForm->setFormCode('customer_register_address')->setEntity($address);
     $addrrequest = $addressForm->prepareRequest($mgAddress);
     $addressData = $addressForm->extractData($addrrequest);
     $addressErrors = $addressForm->validateData($addressData);
     $errors = array();
     if ($addressErrors === true) {
         $address->setId(null)->setData("is_default_{$type}", TRUE);
         $addressForm->compactData($addressData);
         $customer->addAddress($address);
         $addressErrors = $address->validate();
         if (is_array($addressErrors)) {
             $errors = array_merge($errors, $addressErrors);
         }
     } else {
         $errors = array_merge($errors, $addressErrors);
     }
     return $errors;
 }
 /**
  * Get errors on provided customer address
  *
  * @param Mage_Customer_Model_Customer $customer
  * @return array $errors
  */
 protected function _getErrorsOnCustomerAddress($customer)
 {
     $errors = array();
     /* @var $address Mage_Customer_Model_Address */
     $address = $this->_getModel('customer/address');
     /* @var $addressForm Mage_Customer_Model_Form */
     $addressForm = $this->_getModel('customer/form');
     $addressForm->setFormCode('customer_register_address')->setEntity($address);
     $addressData = $addressForm->extractData($this->getRequest(), 'address', false);
     $addressErrors = $addressForm->validateData($addressData);
     if (is_array($addressErrors)) {
         $errors = $addressErrors;
     }
     $address->setId(null)->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false))->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false));
     $addressForm->compactData($addressData);
     $customer->addAddress($address);
     $addressErrors = $address->validate();
     if (is_array($addressErrors)) {
         $errors = array_merge($errors, $addressErrors);
     }
     return $errors;
 }
Example #6
0
 /**
  * Save customer addresses.
  *
  * @param Mage_Customer_Model_Customer $customer
  * @param array $addressesData
  * @throws Mage_Core_Exception
  */
 protected function _prepareCustomerAddressesForSave($customer, array $addressesData)
 {
     $hasChanges = $customer->hasDataChanges();
     $actualAddressesIds = array();
     foreach ($addressesData as $addressData) {
         $addressId = null;
         if (array_key_exists('entity_id', $addressData)) {
             $addressId = $addressData['entity_id'];
             unset($addressData['entity_id']);
         }
         if (null !== $addressId) {
             $address = $customer->getAddressItemById($addressId);
             if (!$address || !$address->getId()) {
                 throw new Mage_Core_Exception($this->_translateHelper->__('The address with the specified ID not found.'));
             }
         } else {
             $address = $this->_addressFactory->create();
             $address->setCustomerId($customer->getId());
             // Add customer address into addresses collection
             $customer->addAddress($address);
         }
         $address->addData($addressData);
         $hasChanges = $hasChanges || $address->hasDataChanges();
         // Set post_index for detect default billing and shipping addresses
         $address->setPostIndex($addressId);
         $actualAddressesIds[] = $address->getId();
     }
     /** @var Mage_Customer_Model_Address $address */
     foreach ($customer->getAddressesCollection() as $address) {
         if (!in_array($address->getId(), $actualAddressesIds)) {
             $address->setData('_deleted', true);
             $hasChanges = true;
         }
     }
     $customer->setDataChanges($hasChanges);
 }
Example #7
0
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Magento
 * @package     Mage_ImportExport
 * @subpackage  integration_tests
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
//Create customer
$customer = new Mage_Customer_Model_Customer();
$customer->setWebsiteId(1)->setEntityId(1)->setEntityTypeId(1)->setAttributeSetId(0)->setEmail('*****@*****.**')->setPassword('password')->setGroupId(1)->setStoreId(1)->setIsActive(1)->setFirstname('Charles')->setLastname('Alston')->setGender(2);
$customer->isObjectNew(true);
// Create address
$address = new Mage_Customer_Model_Address();
//  default_billing and default_shipping information would not be saved, it is needed only for simple check
$address->addData(array('firstname' => 'Charles', 'lastname' => 'Alston', 'street' => '3781 Neuport Lane', 'city' => 'Panola', 'country_id' => 'US', 'region_id' => '51', 'postcode' => '30058', 'telephone' => '770-322-3514', 'default_billing' => 1, 'default_shipping' => 1));
// Assign customer and address
$customer->addAddress($address);
$customer->save();
// Mark last address as default billing and default shipping for current customer
$customer->setDefaultBilling($address->getId());
$customer->setDefaultShipping($address->getId());
$customer->save();
Mage::unregister('_fixture/Mage_ImportExport_Customer');
Mage::register('_fixture/Mage_ImportExport_Customer', $customer);
Example #8
0
 /**
  * Add address to customer during create account
  *
  * @param Mage_Customer_Model_Customer $customer
  * @return Mage_Customer_Model_Address|null
  */
 protected function _extractAddress($customer)
 {
     if (!$this->getRequest()->getPost('create_address')) {
         return null;
     }
     /* @var Mage_Customer_Model_Address $address */
     $address = Mage::getModel('Mage_Customer_Model_Address');
     /* @var Mage_Customer_Model_Form $addressForm */
     $addressForm = Mage::getModel('Mage_Customer_Model_Form');
     $addressForm->setFormCode('customer_register_address')->setEntity($address);
     $addressData = $addressForm->extractData($this->getRequest(), 'address', false);
     $address->setId(null)->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false))->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false));
     $addressForm->compactData($addressData);
     $customer->addAddress($address);
     return $address;
 }
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Magento
 * @package     Mage_ImportExport
 * @subpackage  integration_tests
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
//Create customer
$customer = new Mage_Customer_Model_Customer();
$customer->setWebsiteId(0)->setEntityId(1)->setEntityTypeId(1)->setAttributeSetId(0)->setEmail('*****@*****.**')->setPassword('password')->setGroupId(0)->setStoreId(0)->setIsActive(1)->setFirstname('Betsy')->setLastname('Parker')->setGender(2);
$customer->isObjectNew(true);
$customer->save();
// Create and set addresses
$addressFirst = new Mage_Customer_Model_Address();
$addressFirst->addData(array('entity_id' => 1, 'firstname' => 'Betsy', 'lastname' => 'Parker', 'street' => '1079 Rocky Road', 'city' => 'Philadelphia', 'country_id' => 'US', 'region_id' => '51', 'postcode' => '19107', 'telephone' => '215-629-9720'));
$addressFirst->isObjectNew(true);
$customer->addAddress($addressFirst);
$customer->setDefaultBilling($addressFirst->getId());
$addressSecond = new Mage_Customer_Model_Address();
$addressSecond->addData(array('entity_id' => 2, 'firstname' => 'Anthony', 'lastname' => 'Nealy', 'street' => '3176 Cambridge Court', 'city' => 'Fayetteville', 'country_id' => 'US', 'region_id' => '5', 'postcode' => '72701', 'telephone' => '479-899-9849'));
$addressSecond->isObjectNew(true);
$customer->addAddress($addressSecond);
$customer->setDefaultShipping($addressSecond->getId());
$customer->isObjectNew(true);
$customer->save();