/** * @param int $addressId * @param string $addressType * * @return ShopgateAddress */ protected function _getAddress($addressId, $addressType) { $addressItem = new ShopgateAddress(); /** @var AddressCore $addressCore */ $addressCore = new Address($addressId); $addressItem->setId($addressCore->id); switch ($addressType) { case ShopgateCustomerPrestashop::DEFAULT_CUSTOMER_ADDRESS_IDENTIFIER_DELIVERY: $addressItem->setIsDeliveryAddress(true); break; case ShopgateCustomerPrestashop::DEFAULT_CUSTOMER_ADDRESS_IDENTIFIER_INVOICE: $addressItem->setIsInvoiceAddress(true); break; } $addressItem->setFirstName($addressCore->firstname); $addressItem->setLastName($addressCore->lastname); $addressItem->setCompany($addressCore->company); $addressItem->setStreet1($addressCore->address1); $addressItem->setStreet2($addressCore->address2); $addressItem->setZipcode($addressCore->postcode); $addressItem->setCity($addressCore->city); $addressItem->setCountry(Country::getIsoById($addressCore->id_country)); $states = State::getStates($this->getPlugin()->getLanguageId()); foreach ($states as $state) { /** @var StateCore $state */ if ($state['id_country'] == $addressCore->id_state) { $addressItem->setState($state->iso_code); } } return $addressItem; }
/** * Get Magento region model by given ShopgateAddress * * @param ShopgateAddress $address * @return Mage_Directory_Model_Region|NULL */ public function getMagentoRegionByShopgateAddress(ShopgateAddress $address) { $map = Mage::helper('shopgate')->_getIsoToMagentoMapping(); if (!$address->getState()) { return new Varien_Object(); } $state = preg_replace("/{$address->getCountry()}\\-/", "", $address->getState()); $region = Mage::getModel("directory/region")->getCollection()->addRegionCodeFilter($state)->addCountryFilter($address->getCountry())->getFirstItem(); // If no region was found if (!$region->getId() && !empty($state) && isset($map[$address->getCountry()][$state])) { $regionCode = $map[$address->getCountry()][$state]; /** @var Mage_Directory_Model_Resource_Region_Collection $region */ $region = Mage::getModel("directory/region")->getCollection()->addRegionCodeFilter($regionCode)->addCountryFilter($address->getCountry())->getFirstItem(); } return $region; }
protected function getPSAddress(Customer $customer, ShopgateAddress $shopgateAddress) { // Get country $id_country = Country::getByIso($shopgateAddress->getCountry()); if (!$id_country) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_UNKNOWN_COUNTRY_CODE, 'Invalid country code:' . $id_country, true); } // Get state $id_state = 0; if ($shopgateAddress->getState()) { $id_state = (int) Db::getInstance()->getValue('SELECT `id_state` FROM `' . _DB_PREFIX_ . 'state` WHERE `id_country` = ' . $id_country . ' AND `iso_code` = \'' . pSQL(Tools::substr($shopgateAddress->getState(), 3, 2)) . '\''); } // Create alias $alias = Tools::substr('Shopgate_' . $customer->id . '_' . sha1($customer->id . '-' . $shopgateAddress->getFirstName() . '-' . $shopgateAddress->getLastName() . '-' . $shopgateAddress->getCompany() . '-' . $shopgateAddress->getStreet1() . '-' . $shopgateAddress->getStreet2() . '-' . $shopgateAddress->getZipcode() . '-' . $shopgateAddress->getCity()), 0, 32); // Try getting address id by alias $id_address = Db::getInstance()->getValue('SELECT `id_address` FROM `' . _DB_PREFIX_ . 'address` WHERE `alias` = \'' . pSQL($alias) . '\' AND `id_customer`=' . $customer->id); // Get or create address $address = new Address($id_address ? $id_address : null); if (!$address->id) { $address->id_customer = $customer->id; $address->id_country = $id_country; $address->id_state = $id_state; $address->country = Country::getNameById($this->id_lang, $address->id_country); $address->alias = $alias; $address->company = $shopgateAddress->getCompany(); $address->lastname = $shopgateAddress->getLastName(); $address->firstname = $shopgateAddress->getFirstName(); $address->address1 = $shopgateAddress->getStreet1(); $address->address2 = $shopgateAddress->getStreet2(); $address->postcode = $shopgateAddress->getZipcode(); $address->city = $shopgateAddress->getCity(); $address->phone = $shopgateAddress->getPhone(); $address->phone_mobile = $shopgateAddress->getMobile(); if (!$address->add()) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_DATABASE_ERROR, 'Unable to create address', true); } } return $address; }
/** * @param ShopgateAddress|mixed[] $value */ public function setDeliveryAddress($value) { if (!is_object($value) && !$value instanceof ShopgateAddress && !is_array($value)) { $this->delivery_address = null; return; } if (is_array($value)) { $value = new ShopgateAddress($value); $value->setIsDeliveryAddress(true); $value->setIsInvoiceAddress(false); } $this->delivery_address = $value; }
/** * @param Mage_Sales_Model_Order_Address $address * @return array */ protected function _getShopgateAddressFromOrderAddress($address) { $shopgateAddress = new ShopgateAddress(); $shopgateAddress->setFirstName($address->getFirstname()); $shopgateAddress->setLastName($address->getLastname()); $shopgateAddress->setGender($this->_getCustomerHelper()->getShopgateCustomerGender($address)); $shopgateAddress->setCompany($address->getCompany()); $shopgateAddress->setPhone($address->getTelephone()); $shopgateAddress->setStreet1($address->getStreet1()); $shopgateAddress->setStreet2($address->getStreet2()); $shopgateAddress->setCity($address->getCity()); $shopgateAddress->setZipcode($address->getPostcode()); $shopgateAddress->setCountry($address->getCountry()); $shopgateAddress->setState($this->_getHelper()->getIsoStateByMagentoRegion($address)); return $shopgateAddress->toArray(); }
/** * @param Mage_Customer_Model_Address_Abstract|Mage_Sales_Model_Abstract $magentoObject * @param ShopgateOrder|ShopgateAddress|ShopgateCustomer $shopgateObject * @return mixed */ public function setCustomFields($magentoObject, $shopgateObject) { foreach ($shopgateObject->getCustomFields() as $field) { $magentoObject->setData($field->getInternalFieldName(), $field->getValue()); } return $magentoObject; }
public function visitAddress(ShopgateAddress $a) { // get and iterate simple properties $properties = $this->iterateSimpleProperties($a->buildProperties()); // iterate ShopgateOrderCustomField objects $properties['custom_fields'] = $this->iterateObjectList($properties['custom_fields']); // update array $this->array = $properties; }
/** * @param $address * @param $customer * @return int * @throws ShopgateLibraryException */ public function createAddress(ShopgateAddress $address, $customer) { /** @var AddressCore | Address $addressModel */ $addressItem = new Address(); $addressItem->id_customer = $customer->id; $addressItem->lastname = $address->getLastName(); $addressItem->firstname = $address->getFirstName(); if ($address->getCompany()) { $addressItem->company = $address->getCompany(); } $addressItem->address1 = $address->getStreet1(); if ($address->getStreet2()) { $addressItem->address2 = $address->getStreet2(); } $addressItem->city = $address->getCity(); $addressItem->postcode = $address->getZipcode(); if (!Validate::isLanguageIsoCode($address->getCountry())) { $customer->delete(); throw new ShopgateLibraryException(ShopgateLibraryException::REGISTER_FAILED_TO_ADD_USER, 'invalid country code: ' . $address->getCountry(), true); } $addressItem->id_country = Country::getByIso($address->getCountry()); /** * prepare states */ $stateParts = explode('-', $address->getState()); if (count($stateParts) == 2) { $address->setState($stateParts[1]); } if ($address->getState() && !Validate::isStateIsoCode($address->getState())) { $customer->delete(); throw new ShopgateLibraryException(ShopgateLibraryException::REGISTER_FAILED_TO_ADD_USER, 'invalid state code: ' . $address->getState(), true); } else { $addressItem->id_state = State::getIdByIso($address->getState()); } $addressItem->alias = $address->getIsDeliveryAddress() ? $this->getModule()->l('Default delivery address') : $this->getModule()->l('Default'); $addressItem->alias = $address->getIsInvoiceAddress() ? $this->getModule()->l('Default invoice address') : $this->getModule()->l('Default'); $addressItem->phone = $address->getPhone(); $addressItem->phone_mobile = $address->getMobile(); $shopgateCustomFieldsHelper = new ShopgateCustomFieldsHelper(); $shopgateCustomFieldsHelper->saveCustomFields($addressItem, $address->getCustomFields()); $validateMessage = $addressItem->validateFields(false, true); if ($validateMessage !== true) { $customer->delete(); throw new ShopgateLibraryException(ShopgateLibraryException::REGISTER_FAILED_TO_ADD_USER, $validateMessage, true); } $addressItem->save(); return $addressItem->id; }
public function visitAddress(ShopgateAddress $a) { // get properties and iterate (no complex types in ShopgateAddress objects) $this->array = $this->iterateSimpleProperties($a->buildProperties()); }
/** * @param ShopgateAddress $address * @return AddressCore */ protected function _createAddress(ShopgateAddress $address) { /** @var AddressCore $resultAddress */ $resultAddress = new Address(); $resultAddress->id_country = $this->_getCountryIdByIsoCode($address->getCountry()); $resultAddress->alias = self::DEFAULT_ADDRESS_ALIAS; $resultAddress->firstname = $address->getFirstName(); $resultAddress->lastname = $address->getLastName(); $resultAddress->address1 = $address->getStreet1(); $resultAddress->postcode = $address->getZipcode(); $resultAddress->city = $address->getCity(); $resultAddress->country = $address->getCountry(); $resultAddress->phone = $address->getPhone() ? $address->getPhone() : 1; $resultAddress->phone_mobile = $address->getMobile() ? $address->getMobile() : 1; /** * check is state iso code available */ if ($address->getState() != '') { $resultAddress->id_state = $this->getStateIdByIsoCode($address->getState()); } $resultAddress->company = $address->getCompany(); return $resultAddress; }
/** * @param $user * @param $pass * @return ShopgateCustomer * @throws ShopgateLibraryException */ public function getCustomer($user, $pass) { $customerId = $this->getCustomerIdByEmailAndPassword($user, $pass); if (!$customerId) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_WRONG_USERNAME_OR_PASSWORD, 'Username or password is incorrect'); } /** @var CustomerCore $customer */ $customer = new Customer($customerId); $shopgateCustomer = new ShopgateCustomer(); $shopgateCustomer->setCustomerId($customer->id); $shopgateCustomer->setCustomerNumber($customer->id); $shopgateCustomer->setFirstName($customer->firstname); $shopgateCustomer->setLastName($customer->lastname); $shopgateCustomer->setGender($this->mapGender($customer->id_gender)); $shopgateCustomer->setBirthday($customer->birthday); $shopgateCustomer->setMail($customer->email); $shopgateCustomer->setNewsletterSubscription($customer->newsletter); $shopgateCustomer->setCustomerToken(ShopgateCustomerPrestashop::getToken($customer)); $addresses = array(); foreach ($customer->getAddresses($this->getPlugin()->getLanguageId()) as $address) { $addressItem = new ShopgateAddress(); $addressItem->setId($address['id_address']); $addressItem->setFirstName($address['firstname']); $addressItem->setLastName($address['lastname']); $addressItem->setCompany($address['company']); $addressItem->setStreet1($address['address1']); $addressItem->setStreet2($address['address2']); $addressItem->setCity($address['city']); $addressItem->setZipcode($address['postcode']); $addressItem->setCountry($address['country']); $addressItem->setState($address['state']); $addressItem->setPhone($address['phone']); $addressItem->setMobile($address['phone_mobile']); if ($address['alias'] == 'Default invoice address') { $addressItem->setAddressType(ShopgateAddress::INVOICE); } elseif ($address['alias'] == 'Default delivery address') { $addressItem->setAddressType(ShopgateAddress::DELIVERY); } else { $addressItem->setAddressType(ShopgateAddress::BOTH); } $addresses[] = $addressItem; } $shopgateCustomer->setAddresses($addresses); /** * customer groups */ $customerGroups = array(); if (is_array($customer->getGroups())) { foreach ($customer->getGroups() as $customerGroupId) { $groupItem = new Group($customerGroupId, $this->getPlugin()->getLanguageId(), $this->getPlugin()->getContext()->shop->id ? $this->getPlugin()->getContext()->shop->id : false); $group = new ShopgateCustomerGroup(); $group->setId($groupItem->id); $group->setName($groupItem->name); $customerGroups[] = $group; } } $shopgateCustomer->setCustomerGroups($customerGroups); return $shopgateCustomer; }
/** * @param ShopgateCustomer $shopgateCustomer * @param Mage_Customer_Model_Customer $magentoCustomer * @return ShopgateCustomer */ protected function _getCustomerSetAddresses(&$shopgateCustomer, $magentoCustomer) { $aAddresses = array(); foreach ($magentoCustomer->getAddresses() as $magentoCustomerAddress) { /** @var Mage_Customer_Model_Address $magentoCustomerAddress */ $shopgateAddress = new ShopgateAddress(); $shopgateAddress->setId($magentoCustomerAddress->getId()); $shopgateAddress->setIsDeliveryAddress(1); $shopgateAddress->setIsInvoiceAddress(1); $shopgateAddress->setFirstName($magentoCustomerAddress->getFirstname()); $shopgateAddress->setLastName($magentoCustomerAddress->getLastname()); $shopgateAddress->setGender($this->_getCustomerHelper()->getShopgateCustomerGender($magentoCustomerAddress)); $shopgateAddress->setCompany($magentoCustomerAddress->getCompany()); $shopgateAddress->setMail($magentoCustomerAddress->getMail()); $shopgateAddress->setPhone($magentoCustomerAddress->getTelephone()); $shopgateAddress->setStreet1($magentoCustomerAddress->getStreet1()); $shopgateAddress->setStreet2($magentoCustomerAddress->getStreet2()); $shopgateAddress->setCity($magentoCustomerAddress->getCity()); $shopgateAddress->setZipcode($magentoCustomerAddress->getPostcode()); $shopgateAddress->setCountry($magentoCustomerAddress->getCountry()); $shopgateAddress->setState($this->_getHelper()->getIsoStateByMagentoRegion($magentoCustomerAddress)); $aAddresses[] = $shopgateAddress; } $shopgateCustomer->setAddresses($aAddresses); return $shopgateCustomer; }