Page 1 of 1

Add Product Options to Categories

Posted: Wed Sep 21, 2011 3:09 am
by zebdev
Hi all,

I have been trying to solve this issue for a few days and have previously posted on the forum.

I have been looking in to the issue more and think I am close to solving the issue but am also new to open cart.

I have used the category wall to show all my categories on the home page of my store. When the user clicks on a category all products from that category are listed.
Each product has a set of options and I want these options to display on this page.

I am using opencart 1.5.1

I added a line of code to this file:- catalog/controller/product/category.php

Code: Select all

$this->data['action'] = $this->url->link('index.php?route=checkout/cart'); 
Then just before the

Code: Select all

this->data['products'][] = array(
I added this:-

Code: Select all

$options = $this->model_catalog_product->getProductOptions($result['product_id']);
Then added to the product array so it looks like this:-

Code: Select all

$this->data['products'][] = array(
'id' => $result['product_id'],
'options' => $options, 
'name'        => $result['name'],
'thumb'       => $image,
'description' => substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
'price'       => $price,
'special'     => $special,
'tax'         => $tax,
'rating'      => $result['rating'],
'reviews'     => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
'href'        => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'])	
				);
Then in the /mytheme/template/product/category.tpl file I added this:-

Code: Select all

<form action="<?php echo $action; ?>;" method="post" enctype="multipart/form-data" id="product_<?php echo $j;?>">
<?php if ($products['options']) { ?>
<?php foreach ($products['options'] as $option) { ?>
<div class="content"> <?php echo $option['name']; ?>: <select name="option[<?php echo $option['product_option_id']; ?>]" id="option[<?php echo $option['product_option_id']; ?>]"  onchange="document.getElementById('price<?php echo $v ?>').selectedIndex=this.selectedIndex;
alert(document.getElementById('price<?php echo $v; ?>').value);">
<?php foreach ($option['option_value'] as $option_value) { ?>
<option value="<?php echo $option_value['product_option_value_id']; ?>"><?php echo $option_value['name']; ?></option>
<?php } ?>
</select></div>

<?php } ?>   
</div>
<input type="hidden" name="quantity" size="3" value="1" />
</div>
<?php } else {  ?>
<div class="content" style="height:25px"> QTY: <input type="text" name="quantity" size="3" value="" /> </div>   
<?php } ?>
<input type="hidden" name="product_id" value="<?php echo $products[$j]['id']; ?>" />
<a onclick="alert('<?php echo $products[$j]['name']; ?> added to the cart')" id="add_to_cart_<?php echo $j;?>" rel="<?php echo $j;?>" class="button add_to_cart"><span>Add to Cart</span></a>
</form>
So I refreshed my store and had the following error:
Notice: Undefined index: options in C:\wamp\www\mystore\catalog\view\theme\mytheme\template\product\category.tpl on line 60

Any ideas how I can solve this error?

Re: Add Product Options to Categories

Posted: Thu Oct 20, 2011 2:19 am
by kandarp
Hey its solved.

Instead of

$options = $this->model_catalog_product->getProductOptions($result['product_id']);

Use

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

foreach ($this->model_catalog_product->getProductOptions($result['product_id']) as $option) {
if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'image') {
$option_value_data = array();

foreach ($option['option_value'] as $option_value) {
if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
$option_value_data[] = array(
'product_option_value_id' => $option_value['product_option_value_id'],
'option_value_id' => $option_value['option_value_id'],
'name' => $option_value['name'],
'image' => $this->model_tool_image->resize($option_value['image'], 50, 50),
'price' => (float)$option_value['price'] ? $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax'))) : false,
'price_prefix' => $option_value['price_prefix']
);
}
}

$this->data['options'][] = array(
'product_option_id' => $option['product_option_id'],
'option_id' => $option['option_id'],
'name' => $option['name'],
'type' => $option['type'],
'option_value' => $option_value_data,
'required' => $option['required']
);
} elseif ($option['type'] == 'text' || $option['type'] == 'textarea' || $option['type'] == 'file' || $option['type'] == 'date' || $option['type'] == 'datetime' || $option['type'] == 'time') {
$this->data['options'][] = array(
'product_option_id' => $option['product_option_id'],
'option_id' => $option['option_id'],
'name' => $option['name'],
'type' => $option['type'],
'option_value' => $option['option_value'],
'required' => $option['required']
);
}
}

$options['product_id'] = $this->data['options'];


And on data['products'] add options parameter

$this->data['products'][] = array(
'product_id' => $result['product_id'],
'options' => $options['product_id'],
'thumb' => $image,
'name' => $result['name'],
'description' => mb_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
'price' => $price,
'special' => $special,
'tax' => $tax,
'rating' => $result['rating'],
'reviews' => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
'href' => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'])
);



On the category view page

<div class="info">
<h3><?php echo $product['name']; ?></h3>
<p><?php echo $product['description']; ?></p>

<?php if ($product['options']) { ?>
<div class="options">
<?php foreach ($product['options'] as $option) { ?>
<?php if ($option['type'] == 'select') { ?>
<div id="option-<?php echo $option['product_option_id']; ?>" class="option"><b>
<?php if ($option['required']) { ?>
<span class="required">*</span>
<?php } ?>
<?php echo $option['name']; ?>:</b>
<select name="option[<?php echo $option['product_option_id']; ?>]">
<option value=""><?php echo $text_select; ?></option>
<?php foreach ($option['option_value'] as $option_value) { ?>
<option value="<?php echo $option_value['product_option_value_id']; ?>"><?php echo $option_value['name']; ?>
<?php if ($option_value['price']) { ?>
(<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>)
<?php } ?>
</option>
<?php } ?>
</select>
</div>
<br />
<?php } }
echo "</div>";
} ?>

<a class="add_to_cart_small" onclick="addToCart('<?php echo $product['product_id']; ?>');"><?php echo $button_cart; ?></a>
<a class="wishlist_small" onclick="addToWishList('<?php echo $product['product_id']; ?>');"><?php echo $text_wish; ?></a>
<a class="compare_small" onclick="addToCompare('<?php echo $product['product_id']; ?>');"><?php echo $text_compare; ?></a>
</div>



You can add more options as per your requirements. I have just simply made it for select options.

I hope it helps.

Re: Add Product Options to Categories

Posted: Sun Dec 11, 2011 5:04 am
by gujjar
Hello, guys

I have been looking for this is it possible to see the demo how does it looks like ... and that would be really great if you guys can upload the working files so that I can use them...

I would really appreciate that..

thanks,

Re: Add Product Options to Categories

Posted: Mon Dec 12, 2011 4:25 am
by gujjar
anyone who can help me with this .. I tried to implement this code and received this error

Notice: Undefined index: product_id in C:\Users\CH-UMAR\xampp\htdocs\pizza\catalog\controller\product\category.php on line 147

and can you be bit more clear about the lines that where do I have to place them e.g before/after which line I have to place this piece of code.

Code: Select all

$this->data['action'] = $this->url->link('index.php?route=checkout/cart'); 
thanks..

Re: Add Product Options to Categories

Posted: Mon Dec 12, 2011 5:42 am
by Jeremy Fisk
Thanks for the post

Re: Add Product Options to Categories

Posted: Tue Dec 13, 2011 2:58 am
by gujjar
Anyone .... No one wants to help..

Re: Add Product Options to Categories

Posted: Fri Dec 16, 2011 1:50 am
by kandarp
Hey Gujjar,

Did You Try My Code ?

Anyone still getting any errors or notices than please post here with details so we can help you out.
Let me know in details if i can help you out to solve your issue.

Re: Add Product Options to Categories

Posted: Fri Dec 16, 2011 1:51 am
by kandarp
gujjar wrote:Anyone .... No one wants to help..
Did you try my code ??

Re: Add Product Options to Categories

Posted: Sat Dec 17, 2011 5:59 am
by gujjar
kandarp wrote:
gujjar wrote:Anyone .... No one wants to help..
Did you try my code ??

Hello , kandarp

you can check this link http://umarstudio.com/shop/index.php?ro ... ry&path=20 I am using your code..

thanks.

Re: Add Product Options to Categories

Posted: Tue Dec 20, 2011 5:45 am
by kandarp
$options_data=false;

$options_data[] = array();

foreach ($this->model_catalog_product->getProductOptions($result['product_id']) as $option) {
if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'image') {
$option_value_data = array();

foreach ($option['option_value'] as $option_value) {
if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
$option_value_data[] = array(
'product_option_value_id' => $option_value['product_option_value_id'],
'option_value_id' => $option_value['option_value_id'],
'name' => $option_value['name'],
'image' => $this->model_tool_image->resize($option_value['image'], 50, 50),
'price' => (float)$option_value['price'] ? $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax'))) : false,
'price_prefix' => $option_value['price_prefix']
);
}
}

$options_data[] = array(
'product_option_id' => $option['product_option_id'],
'option_id' => $option['option_id'],
'name' => $option['name'],
'type' => $option['type'],
'option_value' => $option_value_data,
'required' => $option['required']
);
}
}


//var_dump($options_data);echo '<br>';

$this->data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
'description' => mb_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
'price' => $price,
'special' => $special,
'tax' => $tax,
'rating' => $result['rating'],
'reviews' => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
'href' => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id']),
'options' => $options_data
);
}


Try it now. And please debug and verify each variables and its value.

Re: Add Product Options to Categories

Posted: Mon Mar 26, 2012 12:19 am
by danicmyk
I followed these steps but all I get is the same options appear on each product! Any ideas why this is happening? Could really do with some help on this. the site is clients.danimartin.co.uk

Re: Add Product Options to Categories

Posted: Fri Sep 21, 2012 2:51 am
by Alliance
Can someone break this down step by step.. Im trying on 1.5.4 ... I need help. I keep getting errors. :(

Re: Add Product Options to Categories

Posted: Thu Oct 18, 2012 4:40 am
by deeve
@kandarp: I'm using 1.5.2.1 & only need to pull one text value from the options array: $option['option_value']. Could this be done with something more like the original concept as I noticed my template contains a loop on the $Products array? I've tried the following but it seems to pull the full array of options. Seem to think I should be emulating an SQL Query where $Products['product_id'] = $option['option_id']. Any suggestions appreciated..

Code: Select all

$option_value = $this->model_catalog_product->getProductOptions($result['product_id']);	
							
				$this->data['products'][] = array(
					'product_id'  => $result['product_id'],
					'option_value'=> $option_value,
					'thumb'       => $image,
					'name'        => $result['name'],
					'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
					'price'       => $price,
					'special'     => $special,
					'tax'         => $tax,
					'rating'      => $result['rating'],
					'reviews'     => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
					'href'        => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'])
				);

Re: Add Product Options to Categories

Posted: Sun Oct 21, 2012 4:28 pm
by deeve
Okay, I've tried to adapt your 2nd example & include the single option per item into the $products array so I juat have the one php loop within the template. This does successfully pull in all the variables but for some reason, they're not being displayed on my page [yet are in the source code]. Thais makes me think maybe I've got something wrong in the wrong place in one of the loops in the template file. If anyone gets a chance, could you please let me know where I'm going wrong. Thanks.

category.php

Code: Select all

$options_data=false;

$options_data[] = array();

foreach ($this->model_catalog_product->getProductOptions($result['product_id']) as $option);

var_dump($option['option_value']);echo '<br>';

$this->data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
'description' => mb_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
'price' => $price,
'special' => $special,
'tax' => $tax,
'rating' => $result['rating'],
'reviews' => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
'href' => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id']),
'options' => $option['option_value']
);
}
category.tpl

Code: Select all

<div class="product-list">
    <?php foreach ($products as $product) { ?>
    <div class="detail">
	  <?php if ($product['thumb']) { ?>
      <div class="image"><a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" title="<?php echo $product['name']; ?>" alt="<?php echo $product['name']; ?>" /></a></div>
      <?php } ?>
      <div class="name"><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></div>
	  <div class="model"><?php echo $product['model']; ?></div>
	  <div class="rating"><img src="catalog/view/theme/supermarket/image/stars-<?php echo $product['rating']; ?>.png" alt="<?php echo $product['reviews']; ?>" /></div>
           
      <div class="description"><?php echo $product['description']; ?></div>
      <?php if ($product['price']) { ?>
      <div class="price">
        <?php if (!$product['special']) { ?>
        <?php echo $product['price']; ?>
        <?php } else { ?>
        <span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span>
        <?php } ?> <br/>
        <?php if ($product['tax']) { ?>

        <span class="price-tax"><?php echo $text_tax; ?> <?php echo $product['tax']; ?></span>
        <?php } ?>
      </div>
     <!--options loop-->
     <?php if ($product['options']) { ?>
     <div id="options">
     <div id="option-<?php echo $product['product_id']; ?>" class="option">       
        <div id="jquery_jplayer_<?php echo $product['product_id']; ?>" class="cp-jplayer"></div>
<script type="text/javascript">

					var myCirclePlayer = new CirclePlayer("#jquery_jplayer_<?php echo $product['product_id']; ?>",
			{
				m4a: "http://www.mywebsite.co.uk/sample/<?php echo $product['options']; ?>"
				
			}, {
				cssSelectorAncestor: "#cp_container_<?php echo $product['product_id']; ?>"
			});			
		
		</script>		
        </div>
        <br />
        <div id="cp_container_<?php echo $product['product_id']; ?>" class="cp-container_sm">
				<div class="cp-buffer-holder"> <!-- .cp-gt50 only needed when buffer is > than 50% -->
					<div class="cp-buffer-1"></div>
					<div class="cp-buffer-2"></div>
				</div>
				<div class="cp-progress-holder"> <!-- .cp-gt50 only needed when progress is > than 50% -->
					<div class="cp-progress-1"></div>
					<div class="cp-progress-2"></div>
				</div>
				<div class="cp-circle-control"></div>
				<ul class="cp-controls">
					<li><a href="#" class="cp-play" tabindex="1">play</a></li>
					<li><a href="#" class="cp-pause" style="display:none;" tabindex="1">pause</a></li> <!-- Needs the inline style here, or jQuery.show() uses display:inline instead of display:block -->
				</ul>
			</div> 
            </div>
            <?php } ?>            
            
            <!--end of options loop-->
            
            
      <div class="cart">
         <span class="input_but"><input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button" /></span>
      </div>
      <div class="wishlist"><a onclick="addToWishList('<?php echo $product['product_id']; ?>');"><?php echo $button_wishlist; ?></a></div>
      <div class="compare"><a onclick="addToCompare('<?php echo $product['product_id']; ?>');"><?php echo $button_compare; ?></a></div>            
      
 <?php } ?>     
 </div><!-- end detail -->
    <!--end of product as product loop-->
    <?php } ?>
    
	</div>

Re: Add Product Options to Categories

Posted: Mon Oct 22, 2012 8:28 am
by deeve
I've just tried achieving the same result with different code & again, the HTML is generated correctly but nothing shows on the page! I imagine this is because the new values have not been declared properly within category.php but not understanding the protocol I'm unsure what I have to add! If someone could advise I'd be really grateful. Here's my new code:

category.php

Code: Select all

$this->data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
'description' => mb_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
'price' => $price,
'special' => $special,
'tax' => $tax,
'rating' => $result['rating'],
'reviews' => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
'href' => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id']),
'options' => $this->model_catalog_product->getProductOptions($result['product_id'])
);
}
category.tpl

Code: Select all

<?php if ($product['options']) { ?>
<?php foreach ($product['options'] as $option) { ?>
<div id="options">
     <div id="option-<?php echo $product['product_id']; ?>" class="option">       
        <div id="jquery_jplayer_<?php echo $product['product_id']; ?>" class="cp-jplayer"></div>
<script type="text/javascript">

					var myCirclePlayer = new CirclePlayer("#jquery_jplayer_<?php echo $product['product_id']; ?>",
			{
				m4a: "http://www.mywebsite.co.uk/sample/<?php echo $option['option_value']; ?>"
				
			}, {
				cssSelectorAncestor: "#cp_container_<?php echo $product['product_id']; ?>"
			});			
		
		</script>		
        </div>
        <br />
        <div id="cp_container_<?php echo $product['product_id']; ?>" class="cp-container_sm">
				<div class="cp-buffer-holder"> <!-- .cp-gt50 only needed when buffer is > than 50% -->
					<div class="cp-buffer-1"></div>
					<div class="cp-buffer-2"></div>
				</div>
				<div class="cp-progress-holder"> <!-- .cp-gt50 only needed when progress is > than 50% -->
					<div class="cp-progress-1"></div>
					<div class="cp-progress-2"></div>
				</div>
				<div class="cp-circle-control"></div>
				<ul class="cp-controls">
					<li><a href="#" class="cp-play" tabindex="1">play</a></li>
					<li><a href="#" class="cp-pause" style="display:none;" tabindex="1">pause</a></li> <!-- Needs the inline style here, or jQuery.show() uses display:inline instead of display:block -->
				</ul>
			</div> 
            </div>

<?php } ?>
<?php } ?> 

Re: Add Product Options to Categories

Posted: Sat Mar 09, 2013 4:16 am
by pooks
Looking ages for this thank you!

Unfortunately only thing that i got working was the last post. My intention is however to display sizes (options) we have left in stock only or hightlight the ones that are sold with a class. Is there any way how I'd add this thing or similar to the code above?

foreach ($option['option_value'] as $option_value) {
if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {

Re: Add Product Options to Categories

Posted: Mon Feb 24, 2014 4:02 pm
by jaspaljass
hey im using opencart 1.5.6

i want to display product options on category page

can u help me..!!!!!

Re: Add Product Options to Categories

Posted: Sun May 25, 2014 3:37 am
by ianhaney
Has anyone got this working for 1.5.6.4