Jump to content

[Solved] Product accessories (related products) associated in both ways


hakeryk2

Recommended Posts

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 by hakeryk2 (see edit history)
Link to comment
Share on other sites

  • 1 year later...

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 by Vito (see edit history)
  • Like 2
  • Thanks 1
Link to comment
Share on other sites

  • 1 year later...
<?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 by CatchMeZZo (see edit history)
Link to comment
Share on other sites

  • 10 months later...
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

  • 2 years later...
  • 3 months later...
  • 6 months later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...