ashopping.it Posted November 17, 2019 Share Posted November 17, 2019 I'm trying to get the address of the suppliers but the foreach only gives me the last address! Help! Where am I doing wrong? public function getWidgetVariables( $hookName = null, array $configuration = array() ) { $sellers = Supplier::getSuppliers( false, (int)Context::getContext()->language->id, $active = true, $p = false, $n = false, $allGroup = false, $group_by = false, $withProduct = true ); if (!empty($sellers)) { foreach ($sellers as &$seller) { $seller['image'] = $this->context->language->iso_code . '-default'; $seller['link'] = $this->context->link->getSupplierLink($seller['id_supplier']); $seller_address_id = Address::getAddressIdBySupplierId($seller['id_supplier']); $seller_address = new Address($seller_address_id); } } return array( 'sellers' => $sellers, 'seller_address_id' => $seller_address_id, 'seller_address' => $seller_address, 'page_link' => $this->context->link->getPageLink('supplier'), 'text_list_nb' => Configuration::get('SELLERS_DISPLAY_TEXT_NB'), 'seller_display_type' => Configuration::get('SELLER_DISPLAY_TYPE'), 'display_link_seller' => Configuration::get('PS_DISPLAY_SUPPLIERS'), ); } Output of {$seller_address->address1} xxxxxxxx Output of {$seller_address|print_r} Address Object ( [id_customer] => 0 [id_manufacturer] => 0 [id_supplier] => 4 [id_warehouse] => 0 [id_country] => 10 [id_state] => 197 [country] => Italy [alias] => Test [company] => [lastname] => supplier [firstname] => supplier [address1] => xxxxxxxx [address2] => [postcode] => 61122 [city] => Pesaro [other] => [phone] => 3297860571 [phone_mobile] => [vat_number] => [dni] => [date_add] => 2019-11-17 17:08:18 [date_upd] => 2019-11-17 17:08:18 [deleted] => 0 [webserviceParameters:protected] => Array ( [objectsNodeName] => addresses [fields] => Array ( [id_customer] => Array ( [xlink_resource] => customers ) [id_manufacturer] => Array ( [xlink_resource] => manufacturers ) [id_supplier] => Array ( [xlink_resource] => suppliers ) [id_warehouse] => Array ( [xlink_resource] => warehouse ) [id_country] => Array ( [xlink_resource] => countries ) [id_state] => Array ( [xlink_resource] => states ) ) ) [id] => 12 [id_lang:protected] => [id_shop:protected] => [id_shop_list] => Array ( ) [get_shop_from_context:protected] => 1 [table:protected] => address [identifier:protected] => id_address [fieldsRequired:protected] => Array ( [0] => id_country [1] => alias [2] => lastname [3] => firstname [4] => address1 [5] => city ) [fieldsSize:protected] => Array ( [alias] => 32 [company] => 255 [lastname] => 255 [firstname] => 255 [address1] => 128 [address2] => 128 [postcode] => 12 [city] => 64 [other] => 300 [phone] => 32 [phone_mobile] => 32 [dni] => 16 ) [fieldsValidate:protected] => Array ( [id_customer] => isNullOrUnsignedId [id_manufacturer] => isNullOrUnsignedId [id_supplier] => isNullOrUnsignedId [id_warehouse] => isNullOrUnsignedId [id_country] => isUnsignedId [id_state] => isNullOrUnsignedId [alias] => isGenericName [company] => isGenericName [lastname] => isName [firstname] => isName [vat_number] => isGenericName [address1] => isAddress [address2] => isAddress [postcode] => isPostCode [city] => isCityName [other] => isMessage [phone] => isPhoneNumber [phone_mobile] => isPhoneNumber [dni] => isDniLite [deleted] => isBool [date_add] => isDate [date_upd] => isDate ) [fieldsRequiredLang:protected] => Array ( ) [fieldsSizeLang:protected] => Array ( ) [fieldsValidateLang:protected] => Array ( ) [tables:protected] => Array ( ) [image_dir:protected] => [image_format:protected] => jpg [translator:protected] => [def:protected] => Array ( [table] => address [primary] => id_address [fields] => Array ( [id_customer] => Array ( [type] => 1 [validate] => isNullOrUnsignedId [copy_post] => ) [id_manufacturer] => Array ( [type] => 1 [validate] => isNullOrUnsignedId [copy_post] => ) [id_supplier] => Array ( [type] => 1 [validate] => isNullOrUnsignedId [copy_post] => ) [id_warehouse] => Array ( [type] => 1 [validate] => isNullOrUnsignedId [copy_post] => ) [id_country] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [id_state] => Array ( [type] => 1 [validate] => isNullOrUnsignedId ) [alias] => Array ( [type] => 3 [validate] => isGenericName [required] => 1 => 32 ) [company] => Array ( [type] => 3 [validate] => isGenericName => 255 ) [lastname] => Array ( [type] => 3 [validate] => isName [required] => 1 => 255 ) [firstname] => Array ( [type] => 3 [validate] => isName [required] => 1 => 255 ) [vat_number] => Array ( [type] => 3 [validate] => isGenericName ) [address1] => Array ( [type] => 3 [validate] => isAddress [required] => 1 => 128 ) [address2] => Array ( [type] => 3 [validate] => isAddress => 128 ) [postcode] => Array ( [type] => 3 [validate] => isPostCode => 12 ) [city] => Array ( [type] => 3 [validate] => isCityName [required] => 1 => 64 ) [other] => Array ( [type] => 3 [validate] => isMessage => 300 ) [phone] => Array ( [type] => 3 [validate] => isPhoneNumber => 32 ) [phone_mobile] => Array ( [type] => 3 [validate] => isPhoneNumber => 32 ) [dni] => Array ( [type] => 3 [validate] => isDniLite => 16 ) [deleted] => Array ( [type] => 2 [validate] => isBool [copy_post] => ) [date_add] => Array ( [type] => 5 [validate] => isDate [copy_post] => ) [date_upd] => Array ( [type] => 5 [validate] => isDate [copy_post] => ) ) [classname] => Address ) [update_fields:protected] => [force_id] => ) 1 Link to comment Share on other sites More sharing options...
EvaF Posted November 17, 2019 Share Posted November 17, 2019 yes, because $seller_address_id = Address::getAddressIdBySupplierId($seller['id_supplier']); public static function getAddressIdBySupplierId($id_supplier) { ... return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query); // getValue returns a value from the first row, first column of a SELECT query. } you can either override getAddressIdBySupplierId function, or maybe it is enough to write own function and inspire with Customer->getAddresses function something like that: public function getOwnAddressesIdBySupplierId($idSupplier, $idLang) { $cacheId = 'Supplier::getAddressesId' . '-' . (int) $idSupplier . '-' . (int) $idLang); if (!Cache::isStored($cacheId)) { $sql = 'SELECT a.id_address FROM `' . _DB_PREFIX_ . 'address` WHERE `id_lang` = ' . (int) $idLang . ' AND `id_supplier` = ' . (int) $idSupplier . ' AND `deleted` = 0 AND id_customer=0 AND id_manufacturer=0 AND id_warehouse=0 '; $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); Cache::store($cacheId, $result); return $result; } public function getWidgetVariables( $hookName = null, array $configuration = array() ) { $sellers = Supplier::getSuppliers(...); if (!empty($sellers)) { foreach ($sellers as &$seller) { $seller['image'] = $this->context->language->iso_code . '-default'; $seller['link'] = $this->context->link->getSupplierLink($seller['id_supplier']); $seller_addresses_id = Address::getOwnAddressesIdBySupplierId((int) $seller['id_supplier'], (int)Context::getContext()->language->id ); $seller_address = array(); foreach ($seller_addresses_id as $seller_address_id ) { $seller_address[] = new Address($seller_address_id); } } } return array(...); } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now