Post by speedingorange » Tue Feb 07, 2017 8:06 pm

Hi All,

I have tried to set up one way product linking following some information here: viewtopic.php?f=20&t=20402&start=0 intended for opencart 1.5. It worked on 2.0 but has a quirk and I hope someone can put me on the right tracks as to what I have got wrong.

Firstly, it is working in that if you :

Link "Product A" to "Product B" Then "product B" does not show the link back (which is desired) in either the admin or front end.

The Problem/bug is that if you open and save "Product B" on the admin side it deletes the link completely from "Product A" meaning that "product B" no longer shows up as related when viewing "Product A".

This is the code I currently have in admin/model/catalog/product.php (in what I think are the appropriate areas)

Code: Select all

if (isset($data['product_related'])) {
								foreach ($data['product_related'] as $related_id) {
									$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "' AND related_id = '" . (int)$related_id . "'");
									$this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$product_id . "', related_id = '" . (int)$related_id . "'");
								}
							}
and

Code: Select all

							$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "'");
							$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE related_id = '" . (int)$product_id . "'");

							if (isset($data['product_related'])) {
								foreach ($data['product_related'] as $related_id) {
									$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "' AND related_id = '" . (int)$related_id . "'");
									$this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$product_id . "', related_id = '" . (int)$related_id . "'");
								}
							}	
I am sure that I have just missed something / deleted an incorrect line that checks if a product is linked and removes it if not reciprocated. Unfortunately my understanding of this code is limited. Any help is much appreciated.

Original Unmodified Code for Reference

part one

Code: Select all

		if (isset($data['product_related'])) {
			foreach ($data['product_related'] as $related_id) {
				$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "' AND related_id = '" . (int)$related_id . "'");
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$product_id . "', related_id = '" . (int)$related_id . "'");
				$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$related_id . "' AND related_id = '" . (int)$product_id . "'");
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$related_id . "', related_id = '" . (int)$product_id . "'");
			}
		}
part 2

Code: Select all

		$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE related_id = '" . (int)$product_id . "'");

		if (isset($data['product_related'])) {
			foreach ($data['product_related'] as $related_id) {
				$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "' AND related_id = '" . (int)$related_id . "'");
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$product_id . "', related_id = '" . (int)$related_id . "'");
				$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$related_id . "' AND related_id = '" . (int)$product_id . "'");
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$related_id . "', related_id = '" . (int)$product_id . "'");
			}
		}

Active Member

Posts

Joined
Tue Feb 23, 2010 7:33 pm

Post by speedingorange » Wed Feb 08, 2017 12:32 am

FIXED

With my limited understanding it was this line that was causing the issue.

Code: Select all

							$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE related_id = '" . (int)$product_id . "'");
Which to my layman terms brain says, Delete from the database any link where the product being edited is related product.

Active Member

Posts

Joined
Tue Feb 23, 2010 7:33 pm
Who is online

Users browsing this forum: No registered users and 21 guests