get third level category in opencart 2 - opencart

Im editing template for 2.0.x open cart, and found out that the Menu one the top side - shows only upto second sub category..
and i'v been looking up controller files, template files, to edit it as I want - show just one more level (or all levels..as long as I can get more deeper level of sub sub cats..) of sub categories on the navigation on the left --- for couple weeks..and could not find the way.
header.tpl
<ul>
<?php foreach ($categories as $category_1) { ?>
<li class="sub-menu"><div><?php echo $category_1['name']; ?></div>
<?php if ($category_1['children']) { ?>
<div class="mega-menu-content style-2 col-4 clearfix">
<?php foreach($category_1['children'] as $category_2) { ?>
<ul id="m2">
<li class="mega-menu-title"><div><?php echo $category_2['name']; ?></div>
<?php if ($category_2['children']) { ?>
<ul id="m3">
<?php foreach($category_2['children'] as $category_3) { ?>
<li><div><?php echo $category_3['name']; ?></div></li>
<?php } ?>
</ul>
<?php } ?>
</li>
</ul>
<?php } ?>
</div>
<?php } ?>
</li><!-- .mega-menu end -->
<?php } ?>
</ul>
controller - header.php
//below is written to enable 3rd level sub-categories
$categories_1 = $this->model_catalog_category->getCategories(0);
foreach ($categories_1 as $category_1) {
$level_2_data = array();
$categories_2 = $this->model_catalog_category->getCategories($category_1['category_id']);
foreach ($categories_2 as $category_2) {
$level_3_data = array();
$categories_3 = $this->model_catalog_category->getCategories($category_2['category_id']);
foreach ($categories_3 as $category_3) {
$level_3_data[] = array(
'name' => $category_3['name'],
'column' => $category_3['column'] ? $category_3['column'] : 1,
'href' => $this->url->link('product/category', 'path=' . $category_1['category_id'] . '_' . $category_2['category_id'] . '_' . $category_3['category_id'])
);
}
$level_2_data[] = array(
'name' => $category_2['name'],
'children' => $level_3_data,
'href' => $this->url->link('product/category', 'path=' . $category_1['category_id'] . '_' . $category_2['category_id'])
);
}
$this->data['categories'][] = array(
'name' => $category_1['name'],
'children' => $level_2_data,
'column' => $category_1['column'] ? $category_1['column'] : 1,
'href' => $this->url->link('product/category', 'path=' . $category_1['category_id'])
);
}
// End of the written addition
can anyone help this out please?

controller file
$categories = $this->model_catalog_category->getCategories(0);
foreach ($categories as $category) {
if ($category['top']) {
// Level 2
$children_data = array();
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach ($children as $child) {
// Level 3
$children_data2 = array();
$children2 = $this->model_catalog_category->getCategories($child['category_id']);
foreach ($children2 as $child2) {
$children_data2[] = array(
'name' => $child2['name'],
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'].'_'.$child2['category_id'])
);
}
$filter_data = array(
'filter_category_id' => $child['category_id'],
'filter_sub_category' => true
);
$children_data[] = array(
'children'=>$children_data2,
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
}
// Level 1
$data['categories'][] = array(
'name' => $category['name'],
'children' => $children_data,
'column' => $category['column'] ? $category['column'] : 1,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
}
}
In view i.e tpl file
<ul class="nav navbar-nav">
<?php foreach ($categories as $category) { ?>
<?php if ($category['children']) { ?>
<li class="dropdown"><?php echo $category['name']; ?>
<div class="dropdown-menu">
<div class="dropdown-inner">
<?php foreach (array_chunk($category['children'], ceil(count($category['children']) / $category['column'])) as $children) { ?>
<ul class="list-unstyled">
<?php foreach ($children as $child) { ?>
<li><?php echo $child['name']; ?></li>
<?php if($child['children']) { ?>
<?php foreach($child['children'] as $child2) { ?>
<ul>
<li><?php echo $child2['name']; ?></li>
</ul>
<?php } } ?>
<?php } ?>
</ul>
<?php } ?>
</div>
<?php echo $text_all; ?> <?php echo $category['name']; ?> </div>
</li>
<?php } else { ?>
<li><?php echo $category['name']; ?></li>
<?php } ?>
<?php } ?>
</ul>
You need to adjust some css code to look it good.
It is tested on OpenCart version 2.0.1.1.

Related

How to add custom pagination in Woocommerce product page template?

How to add custom pagination in Woocommerce product page template?
Here is my code:
But pagination is not working.
Any suggestion?
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 6,
'paged' => $paged,
'product_cat' => 'trekking,rafting',
'orderby' => 'ASC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
and for displaying pagination:
<?php endwhile; ?>
<!-- End of the main loop -->
<!-- Add the pagination functions here. -->
<div class="cu-pagi">
<?php get_next_posts_link('Older', $loop->max_num_pages) ?>
<?php get_previous_posts_link('Newer', $loop->max_num_pages) ?>
</div>
<?php wp_reset_query(); ?>
</div><!--/.products-->
Got an answer:
Working Code:
<div class="cu-pagi">
<?php
$total_pages = $loop->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
?>
</div>

Opencart - Options disabled when quantity 0

I have a code, but it does not work properly. When the quantity is 0 and Subtract Stock is specified as Yes, the option(s) disappear.
The option(s) remains only when Subtract Stock is specified as No.
Code:
In catalog/controller/product/product.php
First:
Replace: if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
OG: if ($option_value['subtract']) {
Second:
After: $option_value['name'],
Add: 'quantity' => $option_value['quantity'],
In catalog/view/theme/*/template/product/product.tpl
Replace:
<input type="checkbox" name="option[<?php echo $option['product_option_id']; ?>][]" value="<?php echo $option_value['product_option_value_id']; ?>" />
OG:
<input type="checkbox" name="option[<?php echo $option['product_option_id']; ?>][]" value="<?php echo $option_value['product_option_value_id']; ?>" <?php if ($option_value['quantity'] == 0) { ?> disabled <?php } ?> />
In catalog/controller/product/product.php
remove:
if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) { and the closing bracket } a couple lines further down.
Now,
add:
'quantity' => $option_value['quantity'],
after:
$product_option_value_data[] = array(
In product.tpl
<input type="checkbox" name="option[<?php echo $option['product_option_id']; ?>][]" value="<?php echo $option_value['product_option_value_id']; ?>" <?php if ($option_value['quantity'] == 0) { echo 'disabled'; } ?> />

How to update customer TIN Number in Opencart

Am new in OpenCart MVC, I have page called my_account.tpl. In this page am displaying customer name and tin_number.
If customers want to change their tin_number just fill the TIN Number field and click save, it will update oc_customer tabel.
I tried but data's not update.
Following view, model, and controller codes
View: my_account.tpl
<form action="<?php echo $action;?>" method="POST" enctype="multipart/form-data" class="form-horizontal">
<div class="col-md-9 col-lg-6 col-sm-12">
<?php foreach($customerData as $customer) { ?>
<div class="form-group">
<label>Name:</label>
<input type="text" style="display: none" placeholder="ID" name="id" value="<?php echo $result['tin_number'];?>">
<input type="text" name="name" value="<?php echo $customer['name']; ?>" placeholder="Name" class="form-control" disabled>
</div>
<div class="form-group">
<label>TIN Number:</label>
<input type="text" name="tin_number" value="<?php echo $customer['tin_number']; ?>" placeholder="TIN Number" class="form-control">
</div>
<?php } ?>
<div class="form-group">
<input type="submit" name="save" value="<?php echo 'Save'; ?>" class="btn btn-primary">
</div>
</div>
</form>
Controller: my_account.php
public function edit() {
/*form edit my_account*/
if(isset($_GET['tin_number']))
{
$tin_number=$_GET['tin_number'];
}
$this->load->model('account/customer');
$data['delivery_point']=$this->model_account_customer->getcustomerId($tin_number);
$data['action'] = $this->url->link('account/my_account', '', 'SSL');
if ($this->request->server['REQUEST_METHOD'] == 'POST')
{
$this->model_account_customer->editCustomerTin($this->request->post);
$this->response->redirect($this->url->link('account/home', '', 'SSL'));
echo "<script>javascript: alert('test msgbox')></script>";
}
/*form edit my_account*/
}
Model: my_account.php
public function editCustomerTin($data)
{
$query = $this->db->query("UPDATE " . DB_PREFIX . "customer set tin_number='".$data['tin_number']."' where customer_id=".$data['id']);
}
I got the answer it working in another function see below the coding
controller my_account.php
public function index() {
$this->load->language('account/account');
$this->document->setTitle($this->language->get('heading_title'));
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('Home'),
'href' => $this->url->link('common/home')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('Previous'),
'href' => $this->url->link('common/home', '', 'SSL')
);
if (isset($this->session->data['success'])) {
$data['success'] = $this->session->data['success'];
unset($this->session->data['success']);
} else {
$data['success'] = '';
}
$data['heading_title'] = $this->language->get('heading_title');
$url = '';
$this->load->model('account/customer');
//$data['customerData']=array();
$results=$this->model_account_customer->getCustomerByTin();
foreach($results as $result)
{
$query1 = $this->db->query("SELECT concat(firstname,' ',lastname) as name,tin_number,invoice_no_overwrite FROM " . DB_PREFIX . "customer where customer_id='".$_COOKIE['customerId']."'");
$customer_name= $query1->row['name'];
$tin_number= $query1->row['tin_number'];
$invoice_no_overwrite= $query1->row['invoice_no_overwrite'];
$data['customerData'][0] = array(
'name' => $customer_name,
'invoice_no_overwrite'=> $invoice_no_overwrite,
'tin_number'=>$tin_number
);
}
//var_dump($data['CustomerData']);
/*form edit my_account*/
if ($this->request->server['REQUEST_METHOD'] == 'POST')
{
$this->model_account_customer->editCustomerTin($this->request->post);
$this->response->redirect($this->url->link('account/my_account', '', 'SSL'));
}
/*form edit my_account*/
$data['column_left'] = $this->load->controller('common/column_left');
$data['column_right'] = $this->load->controller('common/column_right');
$data['content_top'] = $this->load->controller('common/content_top');
$data['content_bottom'] = $this->load->controller('common/content_bottom');
$data['footer'] = $this->load->controller('common/footer');
$data['header'] = $this->load->controller('common/header');
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/my_account.tpl')) {
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/account/my_account.tpl', $data));
} else {
$this->response->setOutput($this->load->view('default/template/account/my_account.tpl', $data));
}
}
I don't know how its happen can any one teach me...?

compare product functionality not allowed for non-registered user in magento

When non-registred user clicking on add to compare then it shows the alert for registration (with message "registration is required"). Please help. I am using magento 1.9. anybody knows answer please share.
this is my list.phtml file
<?php
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
//var_dump(Mage::app()->getRequest()->getControllerName());
//get list layout
$currentCat = Mage::getSingleton('catalog/layer')->getCurrentCategory()->getName();
//echo $currentCat;exit;
if ('Retail POS Software' == $currentCat && !$this->getRequest()->isAjax()) {
$displayMode = 'list';
} else {
$displayMode = $this->getMode();
}
//get list layout end
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<?php if (!$this->getRequest()->isAjax()): ?>
<div id="ajax-errors" style="display: none;">
<ul class="messages">
<li class="error-msg">
<ul>
<li><span><?php echo $this->__('An error occurred, please try again later.'); ?></span></li>
</ul>
</li>
</ul>
</div>
<div id="loading" style="display: none; margin-bottom: 10px; text-align: center;">
<img class="v-middle" alt="" src="<?php echo $this->getSkinUrl('images/loader
shopby.gif'); ?>"> <?php echo $this->__('Loading, please wait...'); ?>
</div>
<div id="catalog-listing">
<?php endif; ?>
<div class="category-products">
<div class="toolbar-top">
<?php echo $this->getToolbarHtml() ?>
<?php // List mode
//change $this->getMode() to $displayMode
?>
<div class="yt-products-container clearfix">
<?php if($displayMode!='grid'): ?>
<?php $_iterator = 0; ?>
<ol class="products-list" >
<?php
$count_input_qty = 0;
foreach ($_productCollection as $_product):
$count_input_qty++;
$now = date("Y-m-d");
$newsFrom= substr($_product->getData('news_from_date'),0,10);
$newsTo= substr($_product->getData('news_to_date'),0,10);
$specialprice = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialPrice();
$price = Mage::getModel('catalog/product')->load($_product->getId())->getPrice();
$saleoff= round(($price - $specialprice)/$price*100) ;
?>
<li class="item <?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
<div class="item-inner">
<div class="product-list-left col-lg-4 col-md-4 col-sm-5 col-xs-12">
<div class="product-image">
<a href="<?php echo $_product->getProductUrl() ?>" class="product-img" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>">
<img id="product-collection-image-<?php echo $_product->getId(); ?>"
src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(300,300); ?>"
alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
</a>
<?php if ( $now>=$newsFrom && $now<=$newsTo ){ ?>
<span class="new-product have-ico"><?php echo $this->__('New'); ?></span>
<?php }
if ( $specialprice ){ ?>
<span class="sale-product have-ico"><?php echo $this->__('Sale'); ?></span>
<?php } ?>
</div>
</div>
<div class="product-info col-lg-8 col-md-8 col-sm-7 col-xs-12">
<div class="product-name">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>">
<?php if( strlen($_helper->productAttribute($_product, $_product->getName(), 'name')) > 100 ){
echo substr($_helper->productAttribute($_product, $_product->getName(), 'name'), 0, 100);
} else {
echo $_helper->productAttribute($_product, $_product->getName(), 'name');
}?>
</a>
</div>
<div class="product-review">
<?php echo $this->getReviewsSummaryHtml($_product, "short", true); ?>
</div>
<div class="product-price">
<?php echo $this->getPriceHtml($_product, true) ?>
</div>
<?php
// Provides extra blocks on which to hang some features for products in the list
// Features providing UI elements targeting this block will display directly below the product name
if ($this->getChild('name.after')) {
$_nameAfterChildren = $this->getChild('name.after')->getSortedChildren();
foreach ($_nameAfterChildren as $_nameAfterChildName) {
$_nameAfterChild = $this->getChild('name.after')->getChild($_nameAfterChildName);
$_nameAfterChild->setProduct($_product);
echo $_nameAfterChild->toHtml();
}
}
?>
<?php if($_product->getshort_description()) { ?>
<div class="product-desciption">
<?php echo $_product->getshort_description();?>
</div>
<?php } ?>
<?php /* if($_product->isSaleable()): ?>
<p class="availability in-stock"><?php echo $this->__('Availability:') ?> <span><?php echo $this->__('In stock') ?></span></p>
<?php else: ?>
<p class="availability out-of-stock"><?php echo $this->__('Availability:') ?> <span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; */?>
<div class="product-addto-wrap">
<div class="product-addcart">
<?php if($_product->isSaleable()): ?>
<a class="btn-cart" title="<?php echo $this->__('Add to cart') ?>" href="javascript:void(0);" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')">
<?php echo $this->__('Add to cart') ?>
</a>
<?php endif; ?>
</div>
<div class="wishlist-compare">
<?php if ( $this->helper('wishlist')->isAllow() ) : ?>
<a class="link-wishlist" href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" title="<?php echo $this->__('Add to Wishlist') ?>">
<?php //echo $this->__('Add to Wishlist') ?>
</a>
<?php endif; ?>
<?php if( $_compareUrl=$this->getAddToCompareUrl($_product) ): ?>
<a class="link-compare" href="<?php echo $_compareUrl ?>" title="<?php echo $this->__('Add to Compare'); ?>">
<?php //echo $this->__('Add to Compare') ?>
</a>
<?php endif;?>
</div>
</div>
</div>
</div>
</li>
<?php endforeach; ?>
</ol>
<script type="text/javascript">decorateList('products-list', 'none-recursive')</script>
<?php else: ?>
<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $this->getColumnCount();?>
<?php $i=0; foreach ($_productCollection as $_product):?>
<?php
$now = date("Y-m-d H:m:s");
$newsFrom= $_product->getNewsFromDate();
$newsTo= $_product->getNewsToDate();
$specialprice = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialPrice();
$price = Mage::getModel('catalog/product')->load($_product->getId())->getPrice();
$special_from_date = $_product->getSpecialFromDate();
$special_to_date = $_product->getSpecialToDate();
?>
<?php if ( $i++ == 0 ){ ?>
<div class="products-grid">
<div class="row">
<?php } ?>
<div class="item col-lg-4 col-md-4 col-sm-6 col-xs-12">
<div class="item-inner">
<div class="product-image">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
<img id="product-collection-image-<?php echo $_product->getId(); ?>"
src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(300,300); ?>"
alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
</a>
<?php
// Provides extra blocks on which to hang some features for products in the list
// Features providing UI elements targeting this block will display directly below the product name
if ($this->getChild('name.after')) {
$_nameAfterChildren = $this->getChild('name.after')->getSortedChildren();
foreach ($_nameAfterChildren as $_nameAfterChildName) {
$_nameAfterChild = $this->getChild('name.after')->getChild($_nameAfterChildName);
$_nameAfterChild->setProduct($_product);
echo $_nameAfterChild->toHtml();
}
}
?>
<?php if ( $now>=$newsFrom && $now<=$newsTo ){ ?>
<span class="new-product have-ico"><?php echo $this->__('New'); ?></span>
<?php }
if ( $specialprice ){ ?>
<span class="sale-product have-ico"><?php echo $this->__('Sale'); ?></span>
<?php } ?>
</div>
<div class="product-info">
<div class="product-name">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>">
<?php if( strlen($_helper->productAttribute($_product, $_product->getName(), 'name')) > 60 ){
echo substr($_helper->productAttribute($_product, $_product->getName(), 'name'), 0, 60).'...more';
} else {
echo $_helper->productAttribute($_product, $_product->getName(), 'name');
}?>
</a>
</div>
<div class="product-review">
<?php echo $this->getReviewsSummaryHtml($_product, "short", true); ?>
</div>
<div class="product-price">
<?php echo $this->getPriceHtml($_product, true) ?>
</div>
<div class="product-addto-wrap">
<div class="product-addcart">
<?php if($_product->isSaleable()): ?>
<a class="btn-cart" title="<?php echo $this->__('Add to cart') ?>" href="javascript:void(0);" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')">
<?php echo $this->__('Add to Cart') ?>
</a>
<?php else: ?>
<p class="availability out-of-stock">
<span><?php echo $this->__('Out of stock') ?> </span>
</p>
<?php endif; ?>
</div>
<div class="wishlist-compare">
<?php if ( $this->helper('wishlist')->isAllow() ) : ?>
<a class="link-wishlist" href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" title="<?php echo $this->__('Add to Wishlist') ?>">
<?php echo $this->__('Add to Wishlist') ?>
</a>
<?php endif; ?>
<?php if( $_compareUrl=$this->getAddToCompareUrl($_product) ): ?>
<a class="link-compare" href="<?php echo $_compareUrl ?>" title="<?php echo $this->__('Add to Compare'); ?>">
<?php echo $this->__('Add to Compare') ?>
</a>
<?php endif;?>
</div>
</div>
</div>
</div>
</div>
<?php if ( $i == $_collectionSize ){ ?>
</div>
</div>
<?php } ?>
<?php endforeach ?>
<script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
<?php endif; ?>
</div>
<div class="toolbar-bottom">
<?php echo $this->getToolbarHtml() ?>
</div>
</div>
<?php if (!$this->getRequest()->isAjax()): ?>
</div>
<?php endif; ?>
<?php endif; ?>
<?phpif($this->helper('sm_shopby')->isAjaxEnabled()&&!$this->getRequest()->isAjax()):?>
<script type="text/javascript">
//<![CDATA[
function pushState(data, link, replace) {
var History = window.History;
if ( !History.enabled ) {
return false;
}
if (replace) {
History.replaceState(data, document.title, link);
} else {
History.pushState(data, document.title, link);
}
}
function handleEvent(el, event) {
var url, fullUrl;
if (typeof el === 'string') {
url = el;
} else if (el.tagName.toLowerCase() === 'a') {
url = $(el).readAttribute('href');
} else if (el.tagName.toLowerCase() === 'select') {
url = $(el).getValue();
}
<?php // Add this to query string for full page caching systems ?>
if (url.indexOf('?') != -1) {
fullUrl = url + '&isLayerAjax=1';
} else {
fullUrl = url + '?isLayerAjax=1';
}
$('loading').show();
$('ajax-errors').hide();
pushState(null, url, false);
new Ajax.Request(fullUrl, {
method: 'get',
onSuccess: function(transport) {
if (transport.responseJSON) {
$('catalog-listing').update(transport.responseJSON.listing);
$('layered-navigation').update(transport.responseJSON.layer);
pushState({
listing: transport.responseJSON.listing,
layer: transport.responseJSON.layer
}, url, true);
ajaxListener();
} else {
$('ajax-errors').show();
}
$('loading').hide();
}
});
if (event) {
event.preventDefault();
}
}
function ajaxListener() {
var els;
els = $$('div.pager-wrapper a').concat(
$$('div.sort-by-wrap a'),
$$('div.view-mode-wrap a'),
// $$('div.pager select'),
$$('div.sorter select'),
$$('div.block-layered-nav a')
);
els.each(function(el) {
if (el.tagName.toLowerCase() === 'a') {
$(el).observe('click', function(event) {
handleEvent(this, event);
});
} else if (el.tagName.toLowerCase() === 'select') {
$(el).setAttribute('onchange', '');
$(el).observe('change', function(event) {
handleEvent(this, event);
});
}
});
}
document.observe("dom:loaded", function() {
ajaxListener();
(function(History) {
if ( !History.enabled ) {
return false;
}
pushState({
listing: $('catalog-listing').innerHTML,
layer: $('layered-navigation').innerHTML
}, document.location.href, true);
// Bind to StateChange Event
History.Adapter.bind(window, 'popstate', function(event) {
if (event.type == 'popstate') {
var State = History.getState();
$('catalog-listing').update(State.data.listing);
$('layered-navigation').update(State.data.layer);
ajaxListener();
}
});
})(window.History);
});
//]]>
</script>
<?php endif; ?>
<?php if ($this->getRequest()->isAjax()){ ?>
<?php } ?>
<?php
// Provides a block where additional page components may be attached,primarily good for in-page JavaScript
if ($this->getChild('after')) {
$_afterChildren = $this->getChild('after')->getSortedChildren();
foreach ($_afterChildren as $_afterChildName) {
$_afterChild = $this->getChild('after')->getChild($_afterChildName);
//set product collection on after blocks
$_afterChild->setProductCollection($_productCollection);
echo $_afterChild->toHtml();
}
}
?>
Updated
I have made changes in add to compare button like this,
<?php
if(Mage::getSingleton('customer/session')->isLoggedIn()){
<?php if( $_compareUrl=$this->getAddToCompareUrl($_product) ): ?>
<a class="link-compare" href="<?php echo $_compareUrl ?>" title="<?php echo $this->__('Add to Compare'); ?>">
<?php //echo $this->__('Add to Compare') ?>
</a>
<?php endif;?>
}
else
{
<?php echo '<script type="text/javascript"> alert("Registration is required for compare product"); </script>?>
}
?>
Got solution after long time.
I have added following code at the place of compare button
<?php if( $_compareUrl=$this->getAddToCompareUrl($_product) ): ?>
<?php if(Mage::getSingleton('customer/session')->isLoggedIn()) { ?>
<a class="link-compare" href="<?php echo $_compareUrl ?>" title="<?php echo $this->__('Add to Compare'); ?>"></a>
<?php } else { ?>
<a class="link-compare loginToCompare" title="<?php echo $this->__('Add to Compare'); ?>"></a>
<?php } ?>
<?php endif;?>
and added following jquery at the bottom of page for redirection of login page or create account
jQuery(document).ready(function($){
$(".loginToCompare").click(function(){
var r = confirm("You can not compare product without registration please click ok to login or create account!");
if (r == true) {
window.location.href="/customer/account/login/";
}
});
});
this is really working fine for me.

how to display product option on featured module or featured product of opencart

I am using the featured module to list products to my homepage. But while the product option works fine in the product details page, it doesnt work well in the homepage(featured module). The option values for the last product is getting repeated to all the product listings. Also add to cart has some problem. Can anyone provide a proper solution to this?
Code is as follows :
controller:
$this->data['products'] = array();
$products = explode(',', $this->config->get('featured_product'));
if (empty($setting['limit'])) {
$setting['limit'] = 5;
}
$products = array_slice($products, 0, (int)$setting['limit']);
foreach ($products as $product_id) {
$product_info = $this->model_catalog_product->getProduct($product_id);
if ($product_info) {
if ($product_info['image']) {
$image = $this->model_tool_image->resize($product_info['image'], $setting['image_width'], $setting['image_height']);
} else {
$image = false;
}
if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
$price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
} else {
$price = false;
}
if ((float)$product_info['special']) {
$special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
} else {
$special = false;
}
if ($this->config->get('config_review_status')) {
$rating = $product_info['rating'];
} else {
$rating = false;
}
$this->data['products'][] = array(
'product_id' => $product_info['product_id'],
'thumb' => $image,
'name' => $product_info['name'],
'price' => $price,
'product_description' => $product_info['product_description'],
'special' => $special,
'rating' => $rating,
'reviews' => sprintf($this->language->get('text_reviews'), (int)$product_info['reviews']),
'href' => $this->url->link('product/product', 'product_id=' . $product_info['product_id']),
);
$this->data['options'] = array();
//print_r($this->model_catalog_product->getProductOptions($this->request->get['product_id']));
//die("");
$option=$this->model_catalog_product->getProductOptions($product_id);
//foreach ($this->model_catalog_product->getProductOptions($product_id) as $option) //{
$option_value_data = array();
foreach ($option['option_value'] as $option_value) {
if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
$price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
} else {
$price = false;
}
$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' => $price,
'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']
);
//}
}
}
view:
<?php foreach ($products as $product) { ?>
<img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" />
<?php if ($options) { ?>
<div class="options" style="color: #457A33; float: left;line-height: 23px; margin-right: 20px; width:300px;">
<?php foreach ($options as $option) { ?>
<?php if ($option['type'] == 'select') { ?>
<div id="option-<?php echo $option['product_option_id']; ?>" class="option" style="float:left; width:150px;">
<?php if ($option['required']) { ?>
<span class="required">*</span>
<?php } ?>
<b><?php echo $option['name']; ?>:</b><br />
<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>
<br />
What's this
</div>
<?php } ?>
<?php } ?>
The options has to be assigned to the concrete product - so populated and processed before $this->data['products'][] = array(...);:
$this->data['products'] = array();
$products = explode(',', $this->config->get('featured_product'));
if (empty($setting['limit'])) {
$setting['limit'] = 5;
}
$products = array_slice($products, 0, (int)$setting['limit']);
foreach ($products as $product_id) {
$product_info = $this->model_catalog_product->getProduct($product_id);
if ($product_info) {
if ($product_info['image']) {
$image = $this->model_tool_image->resize($product_info['image'], $setting['image_width'], $setting['image_height']);
} else {
$image = false;
}
if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
$price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
} else {
$price = false;
}
if ((float)$product_info['special']) {
$special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
} else {
$special = false;
}
if ($this->config->get('config_review_status')) {
$rating = $product_info['rating'];
} else {
$rating = false;
}
$options = array();
foreach ($this->model_catalog_product->getProductOptions($product_id) as $option) {
$option_value_data = array();
foreach ($option['option_value'] as $option_value) {
if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
$price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
} else {
$price = false;
}
$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' => $price,
'price_prefix' => $option_value['price_prefix']
);
}
}
$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']
);
}
$this->data['products'][] = array(
'product_id' => $product_info['product_id'],
'thumb' => $image,
'name' => $product_info['name'],
'price' => $price,
'product_description' => $product_info['product_description'],
'special' => $special,
'rating' => $rating,
'reviews' => sprintf($this->language->get('text_reviews'), (int)$product_info['reviews']),
'href' => $this->url->link('product/product', 'product_id=' . $product_info['product_id']),
'options' => $options,
);
}
Now You should have the options loaded in each product respectively. Just modify your template accordingly:
<?php foreach ($products as $product) { ?>
<img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" />
<?php if ($product['options']) { ?>
<div class="options" style="color: #457A33; float: left;line-height: 23px; margin-right: 20px; width:300px;">
<?php foreach ($product['options'] as $option) { ?>
<?php if ($option['type'] == 'select') { ?>
<div id="option-<?php echo $option['product_option_id']; ?>" class="option" style="float:left; width:150px;">
<?php if ($option['required']) { ?>
<span class="required">*</span>
<?php } ?>
<b><?php echo $option['name']; ?>:</b><br />
<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>
<br />
What's this
</div>
<?php } ?>
<?php } ?>