Page 1 of 1

[RELEASED] Product related v1.4.9.6 -> 1.5.x releases

Posted: Thu Mar 08, 2012 5:13 am
by straightlight
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 !

Re: [RELEASED] Product related v1.4.9.6 -> 1.5.x releases

Posted: Thu Mar 08, 2012 5:43 am
by Brook
Thank you for posting your Related Products Solution. I will try it out.

Re: [RELEASED] Product related v1.4.9.6 -> 1.5.x releases

Posted: Thu Mar 08, 2012 11:05 pm
by straightlight
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.

Re: [RELEASED] Product related v1.4.9.6 -> 1.5.x releases

Posted: Tue Mar 20, 2012 6:13 pm
by yomariyoshop
thank you for this modification....
but still can not maintain values in the form, am i missing something...?

regards,
http://www.yomariyo.com

Re: [RELEASED] Product related v1.4.9.6 -> 1.5.x releases

Posted: Tue Mar 20, 2012 8:13 pm
by straightlight
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.

Re: [RELEASED] Product related v1.4.9.6 -> 1.5.x releases

Posted: Thu Mar 22, 2012 10:25 am
by yomariyoshop
Hello Straightlight have you found the solution?, i need this feature for my web site, if you solve it, please reply it here...thank you..

regards,
http://www.yomariyo.com

Re: [RELEASED] Product related v1.4.9.6 -> 1.5.x releases

Posted: Sat Mar 24, 2012 1:08 pm
by Brook
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?

Re: [RELEASED] Product related v1.4.9.6 -> 1.5.x releases

Posted: Sat Apr 21, 2012 6:11 pm
by Datura
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 -->

Re: [RELEASED] Product related v1.4.9.6 -> 1.5.x releases

Posted: Sun Jun 10, 2012 7:49 pm
by xkenx
How to apply for Featured product ? Featured product also uses auto-complete to search the product name only !

Re: [RELEASED] Product related v1.4.9.6 -> 1.5.x releases

Posted: Mon Aug 13, 2012 3:59 am
by dengine
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.

Re: [RELEASED] Product related v1.4.9.6 -> 1.5.x releases

Posted: Tue Apr 23, 2013 11:49 am
by xkenx
Thanks for your Help !