Post by straightlight » Thu Mar 08, 2012 5:13 am

Most merchants has requested to have the previous product related added into the v1.5.x releases of OpenCart. I have tested the below modifications with v1.5.2.1 release as it only took me less than 5 minutes to bring it back. This is very easy.

// Step 1

In your admin/controller/catalog/product.php file,

find:

Code: Select all

public function autocomplete() {
add above:

Code: Select all

public function category() {
		$json = array();
		
		$this->load->model('catalog/product');

		if (isset($this->request->get['category_id'])) {
			$category_id = $this->request->get['category_id'];
		} else {
			$category_id = 0;
		}

		$results = $this->model_catalog_product->getProductsByCategoryId($category_id);

		foreach ($results as $result) {
			$json[] = array(
				'product_id' => $result['product_id'],
				'name'       => $result['name'],
				'model'      => $result['model']
			);
		}

		$this->response->setOutput(json_encode($json));
	}

	public function related() {
		$json = array();
		
		$this->load->model('catalog/product');

		if (isset($this->request->post['product_related'])) {
			$products = $this->request->post['product_related'];
		} else {
			$products = array();
		}

		foreach ($products as $product_id) {
			$product_info = $this->model_catalog_product->getProduct($product_id);

			if ($product_info) {
				$json[] = array(
					'product_id' => $product_info['product_id'],
					'name'       => $product_info['name'],
					'model'      => $product_info['model']
				);
			}
		}
		
		$this->response->setOutput(json_encode($json));
	}
// Step 2

In your admin/view/template/catalog/product_form.tpl file,

find:

Code: Select all

<tr>
              <td><?php echo $entry_related; ?></td>
              <td><input type="text" name="related" value="" /></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td><div id="product-related" class="scrollbox">
                  <?php $class = 'odd'; ?>
                  <?php foreach ($product_related as $product_related) { ?>
                  <?php $class = ($class == 'even' ? 'odd' : 'even'); ?>
                  <div id="product-related<?php echo $product_related['product_id']; ?>" class="<?php echo $class; ?>"> <?php echo $product_related['name']; ?><img src="view/image/delete.png" />
                    <input type="hidden" name="product_related[]" value="<?php echo $product_related['product_id']; ?>" />
                  </div>
                  <?php } ?>
                </div></td>
            </tr>
replace with:

Code: Select all

<tr>
            <td><?php echo $entry_related; ?></td>
            <td><table>
                <tr>
                  <td style="padding: 0;" colspan="3"><select id="category" style="margin-bottom: 5px;" onchange="getProducts();">
                      <?php foreach ($categories as $category) { ?>
                      <?php if (in_array((int)$category['category_id'], $product_category)) { ?>
                          <option value="<?php echo (int)$category['category_id']; ?>" selected="selected"><?php echo $category['name']; ?></option>
                      <?php } else { ?>
                          <option value="<?php echo (int)$category['category_id']; ?>"><?php echo $category['name']; ?></option>
                      <?php } ?>
                      <?php } ?>
                    </select></td>
                </tr>
                <tr>
                  <td style="padding: 0;"><select multiple="multiple" id="product" size="10" style="width: 350px;">
                    </select></td>
                  <td style="vertical-align: middle;"><input type="button" value="-->" onclick="addRelated();" />
                    <br />
                    <input type="button" value="<--" onclick="removeRelated();" /></td>
                  <td style="padding: 0;"><select multiple="multiple" id="related" size="10" style="width: 350px;">
                    </select></td>
                </tr>
              </table>
              <div id="product_related">
                <?php foreach ($product_related as $related_id) { ?>
                <input type="hidden" name="product_related[]" value="<?php echo $related_id; ?>" />
                <?php } ?>
              </div></td>
          </tr>
Then, above:

Code: Select all

<?php echo $footer; ?>
add:

Code: Select all

<script type="text/javascript"><!--
function addRelated() {
	$('#product :selected').each(function() {
		$(this).remove();
		
		$('#related option[value=\'' + $(this).attr('value') + '\']').remove();
		
		$('#related').append('<option value="' + $(this).attr('value') + '">' + $(this).text() + '</option>');
		
		$('#product_related input[value=\'' + $(this).attr('value') + '\']').remove();
		
		$('#product_related').append('<input type="hidden" name="product_related[]" value="' + $(this).attr('value') + '" />');
	});
}

function removeRelated() {
	$('#related :selected').each(function() {
		$(this).remove();
		
		$('#product_related input[value=\'' + $(this).attr('value') + '\']').remove();
	});
}

function getProducts() {
	$('#product option').remove();

	<?php if (isset($this->request->get['product_id'])) {?>
	var product_id = '<?php echo $this->request->get['product_id'] ?>';
	<?php } else { ?>
	var product_id = 0;
	<?php } ?>

	$.ajax({
		url: 'index.php?route=catalog/product/category&token=<?php echo $token; ?>&category_id=' + $('#category').attr('value'),
		dataType: 'json',
		success: function(data) {
			for (i = 0; i < data.length; i++) {
				if (data[i]['product_id'] == product_id) { continue; }
	 			$('#product').append('<option value="' + data[i]['product_id'] + '">' + data[i]['name'] + ' (' + data[i]['model'] + ') </option>');
			}
		}
	});
}

function getRelated() {
	$('#related option').remove();
	
	$.ajax({
		url: 'index.php?route=catalog/product/related&token=<?php echo $token; ?>',
		type: 'POST',
		dataType: 'json',
		data: $('#product_related input'),
		success: function(data) {
			$('#product_related input').remove();
			
			for (i = 0; i < data.length; i++) {
	 			$('#related').append('<option value="' + data[i]['product_id'] + '">' + data[i]['name'] + ' (' + data[i]['model'] + ') </option>');
				
				$('#product_related').append('<input type="hidden" name="product_related[]" value="' + data[i]['product_id'] + '" />');
			} 
		}
	});
}

getProducts();
getRelated();
//--></script>
// Step 3

Go to your admin - > catalog - > products - > Insert or Edit and go to the Links table. Scroll down and you will see the old and amazing product related used in v1.4.9.6 release and you're done !

Attachments

opencart_product_related_v1496.PNG

v1.4.9.6 -> v1.5.x releases. - opencart_product_related_v1496.PNG (8.58 KiB) Viewed 9165 times

Last edited by straightlight on Thu Mar 08, 2012 11:05 pm, edited 2 times in total.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by Brook » Thu Mar 08, 2012 5:43 am

Thank you for posting your Related Products Solution. I will try it out.

Active Member

Posts

Joined
Wed Feb 24, 2010 12:15 am

Post by straightlight » Thu Mar 08, 2012 11:05 pm

Fixed a little problem on the product_form.tpl file. To all those who installed this modification before, simply redo the template step to maintain your values in the form.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by straightlight » Tue Mar 20, 2012 8:13 pm

You don't seem to be missing anything as I'm starting to think it may of been one of the reasons why this feature was removed after OC v1.4.9.6 release.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by Brook » Sat Mar 24, 2012 1:08 pm

I have OpenCart 1.5.2.1 installed. The above Admin Related Products functionality worked perfect in OpenCart 1.4.9.6. What code needs to be added to OpenCart 1.5.2.1 to get the Admin Related Products functionality working correctly?

Can someone please post a solution for Saving and then showing the Saved Values on the Related Products section of the Admin->Product Form?

Given the above solution, using the Admin website I can assign Related Products to a Product on the Product Form and click the Save Button. If I go back and look at the same product and view the assigned Related Products, no Related Products are shown on the Admin Product Form. If I go to the catalog website the assigned Related Products display correctly on the Related Products Tab of the given Product.

What code needs to be added to the Admin website to display the assigned Related Products for a given Product?

Active Member

Posts

Joined
Wed Feb 24, 2010 12:15 am

Post by Datura » Sat Apr 21, 2012 6:11 pm

Brook wrote: What code needs to be added to the Admin website to display the assigned Related Products for a given Product?
Try this for the template part admin/view/template/catalog/product_form.tpl file, it worked for me in 1.5.2.2.

Code: Select all

            <!-- Former amazing product related -->
            <tr>
                <td><?php echo $entry_related; ?></td>
                <td><table>
                    <tr>
                      <td style="padding: 0;" colspan="3"><select id="category" style="margin-bottom: 5px;" onchange="getProducts();">
                          <?php foreach ($categories as $category) { ?>
                          <?php if (in_array((int)$category['category_id'], $product_category)) { ?>
                              <option value="<?php echo (int)$category['category_id']; ?>" selected="selected"><?php echo $category['name']; ?></option>
                          <?php } else { ?>
                              <option value="<?php echo (int)$category['category_id']; ?>"><?php echo $category['name']; ?></option>
                          <?php } ?>
                          <?php } ?>
                        </select></td>
                    </tr>
                    <tr>
                      <td style="padding: 0;"><select multiple="multiple" id="product" size="10" style="width: 350px;"></select></td>
                      <td style="vertical-align: middle;"><input type="button" value="-->" onclick="addRelated();" />
                      <br /><input type="button" value="<--" onclick="removeRelated();" /></td>
                      <td style="padding: 0;"><select multiple="multiple" id="related" size="10" style="width: 350px;"></select></td>
                    </tr>
                  </table>
                  <div id="product_related">
                  <?php foreach ($product_related as $product_related) { ?>
                    <input type="hidden" name="product_related[]" value="<?php echo $product_related['product_id']; ?>" />
                    <?php } ?>
                  </div></td>
              </tr>
             <!-- Former amazing product related -->

Attachments

2012-04-21_121047.png

2012-04-21_121047.png (89.17 KiB) Viewed 8638 times


My very own OpenCart Shop


User avatar
Newbie

Posts

Joined
Sat Apr 21, 2012 6:05 pm
Location - Netherlands

Post by xkenx » Sun Jun 10, 2012 7:49 pm

How to apply for Featured product ? Featured product also uses auto-complete to search the product name only !

Newbie

Posts

Joined
Wed May 30, 2012 8:25 pm

Post by dengine » Mon Aug 13, 2012 3:59 am

I dont know why Daniel changed the way Related Products work. Auto complete is e nice function but doesn't really work for this. To make the solution work change the following code in the product controller (admin > controller > catalog > product.php > scroll down to the getForm() function):

if (isset($this->request->post['product_related'])) {
$products = $this->request->post['product_related'];
} elseif (isset($this->request->get['product_id'])) {
$products = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);
} else {
$products = array();
}

$this->data['product_related'] = array();

foreach ($products as $product_id) {
$related_info = $this->model_catalog_product->getProduct($product_id);

if ($related_info) {
$this->data['product_related'][] = array(
'product_id' => $related_info['product_id'],
'name' => $related_info['name']
);
}
}


Should be:


if (isset($this->request->post['product_related'])) {
$this->data['product_related'] = $this->request->post['product_related'];
} elseif (isset($product_info)) {
$this->data['product_related'] = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);
} else {
$this->data['product_related'] = array();
}


Hope this will help all those getting back to the old way of adding related products.

OpenCart Developer sinds versie 1.4.9.3. Inmiddels volledige CMS geïntegreerd binnen OC 1.5. Sta open voor uitdagende custom uitbreidingen en custom themes, inclusief responsive design.

GEMAAKT MET OPENCART
http://www.worldofbessy.com
http://www.kiddycolors.com
http://www.ke-works.com
http://www.trendyard.nl
http://www.bigliftshipping.nl
http://www.kosterklokken.nl


New member

Posts

Joined
Sun Mar 06, 2011 6:52 pm
Location - Colijnsplaat / Den Haag

Post by xkenx » Tue Apr 23, 2013 11:49 am

Thanks for your Help !

Newbie

Posts

Joined
Wed May 30, 2012 8:25 pm
Who is online

Users browsing this forum: No registered users and 105 guests