Community Forums

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

Community created contributions & mods for OpenCart 1.x

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

Postby straightlight » Wed Mar 07, 2012 9:13 pm

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="--&gt;" onclick="addRelated();" />
                    <br />
                    <input type="button" value="&lt;--" 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 2719 times
Last edited by straightlight on Thu Mar 08, 2012 3:05 pm, edited 2 times in total.
Regards,
Straightlight
straightlight
 
Posts: 1932
Joined: Mon Nov 14, 2011 3:38 pm
Location: Canada, ON

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

Postby Brook » Wed Mar 07, 2012 9:43 pm

Thank you for posting your Related Products Solution. I will try it out.
Brook
 
Posts: 437
Joined: Tue Feb 23, 2010 4:15 pm

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

Postby straightlight » Thu Mar 08, 2012 3: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.
Regards,
Straightlight
straightlight
 
Posts: 1932
Joined: Mon Nov 14, 2011 3:38 pm
Location: Canada, ON

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

Postby yomariyoshop » Tue Mar 20, 2012 10:13 am

thank you for this modification....
but still can not maintain values in the form, am i missing something...?

regards,
http://www.yomariyo.com
yomariyoshop
 
Posts: 10
Joined: Tue Mar 20, 2012 2:35 am

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

Postby straightlight » Tue Mar 20, 2012 12: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.
Regards,
Straightlight
straightlight
 
Posts: 1932
Joined: Mon Nov 14, 2011 3:38 pm
Location: Canada, ON

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

Postby yomariyoshop » Thu Mar 22, 2012 2:25 am

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
yomariyoshop
 
Posts: 10
Joined: Tue Mar 20, 2012 2:35 am

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

Postby Brook » Sat Mar 24, 2012 5:08 am

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?
Brook
 
Posts: 437
Joined: Tue Feb 23, 2010 4:15 pm

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

Postby Datura » Sat Apr 21, 2012 10:11 am

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="--&gt;" onclick="addRelated();" />
                      <br /><input type="button" value="&lt;--" 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 2192 times
User avatar
Datura
 
Posts: 4
Joined: Sat Apr 21, 2012 10:05 am
Location: Netherlands

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

Postby xkenx » Sun Jun 10, 2012 11:49 am

How to apply for Featured product ? Featured product also uses auto-complete to search the product name only !
xkenx
 
Posts: 7
Joined: Wed May 30, 2012 12:25 pm

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

Postby dengine » Sun Aug 12, 2012 7:59 pm

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.
dengine
 
Posts: 19
Joined: Sun Mar 06, 2011 10:52 am

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

Postby xkenx » Tue Apr 23, 2013 3:49 am

Thanks for your Help !
xkenx
 
Posts: 7
Joined: Wed May 30, 2012 12:25 pm


Return to Free Contributions

Who is online

Users browsing this forum: No registered users and 5 guests

Hosted by Arvixe Web Hosting