hootersam Posted December 11, 2013 Share Posted December 11, 2013 (edited) Using prestashop 1.5.6.1. We can actually assign several suppliers for one product. Lets say its Apple and Shure. And we HAVE TO choose default supplier for that product. Ok lets say its Apple. Now on product page i have in variables only Apple supplier informations. Is there any way to pass object with list of ALL suppliers assigned to this product? (not only one marked as default?) Something like: [suppliers_tab] [1] [supplier id] => 1 [supplier name] => Apple [supplier desc] => ... [2] [supplier id] => 2 [supplier name] => Shure [supplier desc] => .... Let's say i would like to show list of all suppliers for this product on product page. Any help with this? Thanks in advance. Edited December 12, 2013 by hootersam (see edit history) Link to comment Share on other sites More sharing options...
hootersam Posted December 11, 2013 Author Share Posted December 11, 2013 Ok so i analized Supplier class (classes/Supplier.php), and there is no such function which returns list of suppliers by given product_id. I found this function most close to what i want ot achieve: public static function getSuppliers($get_nb_products = false, $id_lang = 0, $active = true, $p = false, $n = false, $all_groups = false) It returns all suppliers with number of products associated with them optionally. So i guess i have to add new argument to that function like "product_id", and the modify existing sql query adding WHERE clause. But that SQL query is sooo confusing: foreach ($suppliers as $key => $supplier) { $sql = ' SELECT DISTINCT(ps.`id_product`) FROM `'._DB_PREFIX_.'product_supplier` ps JOIN `'._DB_PREFIX_.'product` p ON (ps.`id_product`= p.`id_product`) '.Shop::addSqlAssociation('product', 'p').' WHERE ps.`id_supplier` = '.(int)$supplier['id_supplier'].' AND ps.id_product_attribute = 0'. ($active ? ' AND product_shop.`active` = 1' : ''). ' AND product_shop.`visibility` NOT IN ("none")'. ($all_groups ? '' :' AND ps.`id_product` IN ( SELECT cp.`id_product` FROM `'._DB_PREFIX_.'category_group` cg LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`) WHERE cg.`id_group` '.$sql_groups.' )'); $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); $suppliers[$key]['nb_products'] = count($result); } } Don't have idea how to achive that :/ Any simplier solution to retrive all suppliers for given product on product page? Link to comment Share on other sites More sharing options...
PascalVG Posted December 11, 2013 Share Posted December 11, 2013 Hi hooter, try this: classes/ProductSupplier.php: /** * For a given product, retrieves its suppliers * * @param int $id_product * @param int $group_by_supplier * @return Collection */ public static function getSupplierCollection($id_product, $group_by_supplier = true) { $suppliers = new Collection('ProductSupplier'); $suppliers->where('id_product', '=', (int)$id_product); if ($group_by_supplier) $suppliers->groupBy('id_supplier'); return $suppliers; } Hope that is what you need, pascal Link to comment Share on other sites More sharing options...
hootersam Posted December 11, 2013 Author Share Posted December 11, 2013 (edited) Thanks a lot for this reply! It looks like its that what i'm looking for, but it returns Collection Object and i have no idea how i can handle it. In my module i am calling this function: $product_all_suppliers = ProductSupplier::getSupplierCollection($id_product, true);$smarty->assign(array('product_all_suppliers' => $product_all_suppliers)); And i get on product page: $product_all_suppliers Smarty_Variable Object (3) ->value = Collection Object (0) ->nocache = false ->scope = "Smarty root" I dont see any sub arrays in it or something. Can you help with that? edit: Of course i had to use getResults method $suppliers_table = $product_all_suppliers->getResults(); Now everything is fine :] Edited December 11, 2013 by hootersam (see edit history) Link to comment Share on other sites More sharing options...
hootersam Posted December 11, 2013 Author Share Posted December 11, 2013 One more question because can't find the method: How i can get all supplier information (address, description, telephone etc.) by id_supplier? Can you point me that function? Thanks! Link to comment Share on other sites More sharing options...
PascalVG Posted December 12, 2013 Share Posted December 12, 2013 Glad it works! The supplier info you can get by loading the full supplier: $supplier = new Supplier($id_supplier, $this->context->language->id); Then just use the attributes of supplier to get what you need. If this all works, please mark the topic as [sOLVED]. (See my footer on how to) pascal. Link to comment Share on other sites More sharing options...
hootersam Posted December 12, 2013 Author Share Posted December 12, 2013 Thanks again for pointing out that function. But Supplier object actually not contain all information, it lacks od address, addrsss2, telephone etc: My code to grab suppliers and assign them to variable: $product_all_suppliers = ProductSupplier::getSupplierCollection($id_product, true); $result = $product_all_suppliers->getResults(); foreach($result as $key=>&$supplier) { $supplier_object = new Supplier($supplier->id_supplier, $this->context->language->id); $supplier = $supplier_object; } Nad what i get in product page is: Smarty_Variable Object (3)->value = Array (2) 0 => Supplier Object (12) ->id = 1 ->id_supplier = "1" ->name = "AppleStore" ->description = "<p>Test description</p>" ->date_add = "2013-11-27 13:45:04" ->date_upd = "2013-12-12 14:41:35" ->link_rewrite = "applestore" ->meta_title = "Meta title test" ->meta_keywords = "" ->meta_description = "Desc meta" ->active = "1" ->id_shop_list = null 1 => Supplier Object (12) ->id = 2 ->id_supplier = "2" ->name = "Shure Online Store" ->description = null ->date_add = "2013-11-27 13:45:04" ->date_upd = "2013-11-27 13:45:04" ->link_rewrite = "shure-online-store" ->meta_title = null ->meta_keywords = null ->meta_description = null ->active = "1" ->id_shop_list = null->nocache = false->scope = "Smarty root" So what more function should i use to get all information in that supplier object before i assgin to smarty variable? And one more question: Is this ok to create object in foreach loop every time it passes? That will not cause some kind of memory leakage server side? Or desctructor does all the job? Link to comment Share on other sites More sharing options...
hootersam Posted December 12, 2013 Author Share Posted December 12, 2013 I figured it out form another topic, here is working code: foreach($result as $key=>&$supplier) { $id_address = Address::getAddressIdBySupplierId($supplier->id_supplier); if ($id_address > 0) { $address = new Address((int)$id_address); } $supplier_object = new Supplier($supplier->id_supplier, $this->context->language->id); $supplier = $supplier_object; $supplier->address = $address; } You have to assgin smarty $result variable. Then on product page you get: Smarty_Variable Object (3) ->value = Array (2) 0 => Supplier Object (13) ->id = 1 ->id_supplier = "1" ->name = "AppleStore" ->description = "<p>Test description</p>" ->date_add = "2013-11-27 13:45:04" ->date_upd = "2013-12-12 14:41:35" ->link_rewrite = "applestore" ->meta_title = "Meta title test" ->meta_keywords = "" ->meta_description = "Desc meta" ->active = "1" ->id_shop_list = null ->address = Address Object (25) ->id_customer = "0" ->id_manufacturer = "0" ->id_supplier = "1" ->id_warehouse = "0" ->id_country = "21" ->id_state = "32" ->country = " Stany Zjednoczone" ->alias = "AppleStore" ->company = "Apple" ->lastname = "supplier" ->firstname = "supplier" ->address1 = "767 Fifth Ave." ->address2 = "http://testurl.com" ->postcode = "10153" ->city = "New York" ->other = "" ->phone = "(212) 336-1440" ->phone_mobile = "" ->vat_number = "" ->dni = "" ->date_add = "2013-11-27 13:45:04" ->date_upd = "2013-12-12 14:39:45" ->deleted = "0" ->id = 3 ->id_shop_list = null 1 => Supplier Object (13) ->id = 2 ->id_supplier = "2" ->name = "Shure Online Store" ->description = null ->date_add = "2013-11-27 13:45:04" ->date_upd = "2013-11-27 13:45:04" ->link_rewrite = "shure-online-store" ->meta_title = null ->meta_keywords = null ->meta_description = null ->active = "1" ->id_shop_list = null ->address = Address Object (25) ->id_customer = "0" ->id_manufacturer = "0" ->id_supplier = "2" ->id_warehouse = "0" ->id_country = "21" ->id_state = "13" ->country = " Stany Zjednoczone" ->alias = "supplier" ->company = "Shure" ->lastname = "supplier" ->firstname = "supplier" ->address1 = "5800 W. Touhy Ave" ->address2 = null ->postcode = "60714" ->city = "Niles" ->other = null ->phone = "800-434-3350" ->phone_mobile = null ->vat_number = null ->dni = null ->date_add = "2013-11-27 13:45:04" ->date_upd = "2013-11-27 13:45:04" ->deleted = "0" ->id = 4 ->id_shop_list = null ->nocache = false ->scope = "Smarty root" Which is exactly what i wanted to achieve Marking as solved and thanks for help! 1 Link to comment Share on other sites More sharing options...
PascalVG Posted December 13, 2013 Share Posted December 13, 2013 Perfect! Thanks for the update, pascal Link to comment Share on other sites More sharing options...
quimetal Posted January 22, 2015 Share Posted January 22, 2015 Hi; I've been trying to use the code but I don't know where should I copy it, someone can tell me which lines of code should use and where to copy them? Many thanks to all! Link to comment Share on other sites More sharing options...
hootersam Posted January 22, 2015 Author Share Posted January 22, 2015 (edited) Hi there. It was some time ago, but i found module that i was modyfing to do that. Simply installing it should do the job. Template which should be viewed on product page (hookdisplayProductTab) is extramanufacturer.tpl - there are all variables carrying wanted information. Changes that i made in code written in this post you can find in extramanufacturer.php starting at line 215. Template file has hardcoded polish language words so you have to clean this for yourself and modify template to match your site look. Hope it helps extramanufacturer.zip Edited January 22, 2015 by hootersam (see edit history) Link to comment Share on other sites More sharing options...
quimetal Posted January 26, 2015 Share Posted January 26, 2015 Hello! I have been testing the module to your attached and work me in ps 1.5 or 1.6 ps. I tested installing the original module and then replace the files that you send me and the module stops working, could be needed some extra modification of the code in other files? Hootersam: thank you very much for the help! Link to comment Share on other sites More sharing options...
hootersam Posted January 27, 2015 Author Share Posted January 27, 2015 It's 1.5.6.1 PS compatible. Didn't tested this on 1.6. It not require another modifications outside module, everything is inside this package. As i said it was some time ago i was writing this, project was cancelled and i end up with this, but this module was actually finished. It was working with custom template - maybe that is the case. Sorry but can't help you more with that :/ 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