hakeryk2 Posted April 20, 2016 Share Posted April 20, 2016 (edited) Hello community, Here is the issue - right now in Admin -> Product page -> Associations tab and in Accessories field I can add a product that is related to this one but it works in only one way. It is not creating relation between each other in both products at one time - I have to go to another product, edit it and create association again. Is there is any way to force Presta to do it in both ways? Edited April 21, 2016 by hakeryk2 (see edit history) Link to comment Share on other sites More sharing options...
erouvier29 Posted April 21, 2016 Share Posted April 21, 2016 Here is an override of Product.php that could do the job. Product.php 2 Link to comment Share on other sites More sharing options...
hakeryk2 Posted April 21, 2016 Author Share Posted April 21, 2016 Hey, thank You for reply. It wasn't working in 1.6.1.4 when I override it (I dunno why) but when I manually change the classes than it works. Thank You for Your help! Much appreciated! Link to comment Share on other sites More sharing options...
erouvier29 Posted April 21, 2016 Share Posted April 21, 2016 (edited) You're welcome. Did you remove the file /cache/class_index.php after having copied /override/classes/Product.php? Edited April 21, 2016 by erouvier29 (see edit history) 1 Link to comment Share on other sites More sharing options...
hakeryk2 Posted April 21, 2016 Author Share Posted April 21, 2016 Oh, I didn't - that was the issue. Thanks for letting me know - it will be helpfulf hint for the future. Link to comment Share on other sites More sharing options...
Vito Posted February 18, 2018 Share Posted February 18, 2018 (edited) Thank you, erouvier29 for the idea! Having this functionality we can save a lot of time, by entering just one relation Product1-> to Product2, and as result automatically having Product2->Product1 mirrored relation. I just can not understand why PS haven't include such option into standard PS version? It would be very nice idea to have somewhere in the PS backoffice checkbox option to choose between standard single or bi-directional products relation. One more thing for those who have installed 1.7.2 version - I adopted your code in this way: public static function changeAccessoriesForProduct($accessories_id, $product_id) { foreach ($accessories_id as $id_product_2) { Db::getInstance()->insert('accessory', array( 'id_product_1' => (int)$product_id, 'id_product_2' => (int)$id_product_2 )); // next 3 lines added to mirror for both ways relation Db::getInstance()->insert('accessory', array( 'id_product_1' => (int)$id_product_2, 'id_product_2' => (int)$product_id, )); } } public function deleteAccessories() { // the next line is commented by Vito // return Db::getInstance()->delete('accessory', 'id_product_1 = '.(int)$this->id); // next 3 lines added for removing of both relations $res = Db::getInstance()->delete('accessory', 'id_product_1 = '.(int)$this->id); $res &= Db::getInstance()->delete('accessory', 'id_product_2 = '.(int)$this->id); return $res; } public function deleteFromAccessories() { // the next line is commented by Vito // return Db::getInstance()->delete('accessory', 'id_product_2 = '.(int)$this->id); // next line is added for removing of both mirorred relations return $this->deleteAccessories(); } Edited February 18, 2018 by Vito (see edit history) 2 1 Link to comment Share on other sites More sharing options...
CatchMeZZo Posted January 16, 2020 Share Posted January 16, 2020 (edited) <?php class Product extends ProductCore { public static function changeAccessoriesForProduct($accessories_id, $product_id) { foreach ($accessories_id as $id_product_2) { Db::getInstance()->insert('accessory', array( 'id_product_1' => (int)$product_id, 'id_product_2' => (int)$id_product_2 )); // next 3 lines added to mirror for both ways relation Db::getInstance()->insert('accessory', array( 'id_product_1' => (int)$id_product_2, 'id_product_2' => (int)$product_id, )); } } public function deleteAccessories() { // the next line is commented by Vito // return Db::getInstance()->delete('accessory', 'id_product_1 = '.(int)$this->id); // next 3 lines added for removing of both relations $res = Db::getInstance()->delete('accessory', 'id_product_1 = '.(int)$this->id); $res &= Db::getInstance()->delete('accessory', 'id_product_2 = '.(int)$this->id); return $res; } public function deleteFromAccessories() { // the next line is commented by Vito // return Db::getInstance()->delete('accessory', 'id_product_2 = '.(int)$this->id); // next line is added for removing of both mirorred relations return $this->deleteAccessories(); } } I try to add this Code in /override/classes/ but it not work with 1.7.6.2. The cache is empty. Anyone an idea? Edited January 16, 2020 by CatchMeZZo (see edit history) Link to comment Share on other sites More sharing options...
kuch Posted November 23, 2020 Share Posted November 23, 2020 On 1/16/2020 at 3:59 AM, CatchMeZZo said: I try to add this Code in /override/classes/ but it not work with 1.7.6.2. The cache is empty. Anyone an idea? When i try use this code in override it not working but direct in classes/Product.php it's working. You should add one more function to override and then it works with override: /** * Link accessories with product * Wrapper to static method changeAccessories($accessories_id, $product_id). * * @param array $accessories_id Accessories ids */ public function changeAccessories($accessories_id) { self::changeAccessoriesForProduct($accessories_id, $this->id); } Link to comment Share on other sites More sharing options...
DanMarius1 Posted March 17, 2023 Share Posted March 17, 2023 Hi! There is a solution to solve the bidirectional related products in Presta 1.7.8.8? Thanks! Link to comment Share on other sites More sharing options...
Dreamer83 Posted July 4, 2023 Share Posted July 4, 2023 On 23/11/2020 at 16:31, kuch said: hi is there a method to exclude inactive product when i choose accessories? Link to comment Share on other sites More sharing options...
Panther.software Posted January 26 Share Posted January 26 For fully functional script including duplicate checking and "one to many" and "many to one" (bidirectional) solution. Contact us. 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