I'm trying to add a new field (programmatically) in the (admin->customer->tab general) customer section of customers called customer number. Everything works as expected but I don't want to get double numbers in the database since the user can enter his own number. Every time a new user gets created from the admin the code looks for the last (highest) customer number and adds +1 to it.
I cannot find a solution for this case:
If the customer already exists (edit mode) it should show a not editable value (readonly)
if registering a new customer the last (highest) customer number should be shown (editable). If the user enters a number that is lower than the highest customer number an error should show up and the highest customer number + 1 should show.
This is the code I have so far
In the controller (getForm):
if (isset($this->error['customer_number'])) {
$data['error_customer_number'] = $this->error['customer_number'];
} else {
$data['error_customer_number'] = '';
}
$data['latest_customer'] = $this->model_sale_customer->getLatestCustomerNumber();
if (isset($this->request->post['customer_number'])) {
$data['customer_number'] = $this->request->post['customer_number'];
} elseif (!empty($customer_info)) {
$data['customer_number'] = $customer_info['customer_number'];
} else {
$data['customer_number'] = '0';
}
In the validation (validateForm):
$data['latest_customer'] = $this->model_sale_customer->getLatestCustomerNumber();
if (isset($this->request->post['customer_number']) && ($this->request->post['customer_number'] <= $data['latest_customer'])) {
$this->error['customer_number'] = $this->language->get('error_customer_number');
}
In my customer_form.tpl I have:
<div class="form-group">
<label class="col-sm-2 control-label" for="input-customer-number"><span data-toggle="tooltip" title="<?php echo $help_customer_number; ?>"> <?php echo $entry_customer_number; ?></span></label>
<div class="col-sm-10">
<?php if (!empty($customer_number) || $customer_number != 0){ ?>
<input type="text" name="customer_number" value="<?php echo $customer_number; ?>" placeholder="<?php echo $entry_customer_number; ?>" id="input-customer-number" class="form-control" />
<?php }else{ ?>
<input type="text" name="customer_number" value="<?php echo $latest_customer + 1; ?>" placeholder="<?php echo $entry_customer_number; ?>" id="input-customer-number" class="form-control" />
<?php if ($error_customer_number) { ?>
<div class="text-danger"><?php echo $error_customer_number; ?></div>
<?php } ?>
<?php } ?>
</div>
</div>
This obviously doesn't work because when editing an existing customer the customer number is always lower than the highest customer number in the system (and thus gives an error). Hope some one can help me out with this! Thanks in advance!
For other who are looking into this I solved my problem myself. Ali's comment made me think. I added a hidden input field so I could check against that. Is it an existing customer or not (has number or not). I only have to check for new customers cause that is an inputfield (existing is readonly and thus not editable). The check is to see if this is a new customer (value 0) - is the posted value < The highest customer number. That's it!
This is the code I have so In the controller (getForm):
if (isset($this->error['customer_number'])) {
$data['error_customer_number'] = $this->error['customer_number'];
} else {
$data['error_customer_number'] = '';
}
if (isset($this->request->post['customer_number']) && (($this->request->post['customer_number'] < $data['latest_customer']) && $this->request->post['existing_customer'] == '0')) {
$this->request->post['customer_number'] = '0';
$data['customer_number'] = '0';
} elseif (isset($this->request->post['customer_number'])) {
$data['customer_number'] = $this->request->post['customer_number'];
} elseif (!empty($customer_info)) {
$data['customer_number'] = $customer_info['customer_number'];
} else {
$data['customer_number'] = '0';
}
if (isset($this->request->post['existing_customer'])) {
$data['existing_customer'] = $this->request->post['existing_customer'];
} else {
$data['existing_customer'] = '0';
}
In the validation (validateForm):
$data['latest_customer'] = $this->model_customer_customer->getLatestCustomerNumber();
if (isset($this->request->post['customer_number']) && (($this->request->post['customer_number'] < $data['latest_customer']) && ($this->request->post['existing_customer'] == '0' || $data['existing_customer'] = '0'))) {
$this->error['customer_number'] = $this->language->get('error_customer_number');
}
In my customer_form.tpl I have:
<div class="form-group">
<label class="col-sm-2 control-label" for="input-customer-number"><span data-toggle="tooltip" title="<?php echo $help_customer_number; ?>"> <?php echo $entry_customer_number; ?></span></label>
<div class="col-sm-10">
<?php if (!empty($customer_number) || $customer_number != 0){ ?>
<input type="text" name="customer_number" value="<?php echo $customer_number; ?>" placeholder="<?php echo $entry_customer_number; ?>" id="input-customer-number" class="form-control" readonly />
<input type="hidden" name="existing_customer" value="1" id="input-existing-customer" class="form-control" />
<?php }else{ ?>
<input type="text" name="customer_number" value="<?php echo $latest_customer + 1; ?>" placeholder="<?php echo $entry_customer_number; ?>" id="input-customer-number" class="form-control" />
<input type="hidden" name="existing_customer" value="0" id="input-existing-customer" class="form-control" />
<?php } ?>
<?php if ($error_customer_number) { ?>
<div class="text-danger"><?php echo $error_customer_number; ?></div>
<?php } ?>
</div>
</div>
Related
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'; } ?> />
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...?
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.
hope all is well. I am trying to install Opencart (2.0.3) into a subdirectory (Ie. www.website.com/shop/) I want the root URL www.website.com to go to a Login page, where once a user logs in. It will redirect them to the /shop/ portion of the site and allow them to continue their business. I was wondering what the easiest way was to accomplish this. Would I install everything in the root folder, and then modify the .htaccess file along with the config files? Then how would i Make the login files work in the root folder? I tried installing everything first into the subdirectory /shop/... but then I get issues trying to figure out how to get files in the root folder to work.
Thanks in advance!
Yes, need to work with ajax functionality as below. In the index.php insert the following code and replace URL_WITH_SHOP with your urlshop. Then I have taken "shop" as sub-folder installation if it is different then replace "shop" with your sub-folder name:
<script src="shop/catalog/view/javascript/jquery/jquery-2.1.1.min.js" type="text/javascript"></script>
<script src="shop/catalog/view/javascript/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div id="content" class="col-sm-12 ">
<div class="row ">
<div class="col-sm-6" style="margin: 0px auto; float: none;">
<div class="well">
<h2>Returning Customer</h2>
<p><strong>I am a returning customer</strong></p>
<form method="post" enctype="multipart/form-data">
<div class="error"></div>
<div class="form-group">
<label class="control-label" for="input-email">E-Mail Address</label>
<input type="text" name="email" value="" placeholder="E-Mail Address" id="input-email" class="form-control" />
</div>
<div class="form-group">
<label class="control-label" for="input-password">Password</label>
<input type="password" name="password" value="" placeholder="Password" id="input-password" class="form-control" />
</div>
<button type="button" id="button-cart" data-loading-text="Checking login" class="btn btn-primary ">Login</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript"><!--
$('#button-cart').on('click', function() {
$.ajax({
url: 'shop/index.php?route=account/loginajax',
type: 'post',
data: $('input[type=\'text\'], input[type=\'password\']'),
dataType: 'json',
beforeSend: function() {
$('#button-cart').button('loading');
},
complete: function() {
$('#button-cart').button('reset');
},
success: function(json) {
$('.alert, .text-danger').remove();
$('.form-group').removeClass('has-error');
if (json['error']) {
$('.error').after('<div class="alert alert-danger has-error">' + json['error'] + '</div>');
}
if (json['success']) {
$('.error').after('<div class="alert alert-success">' + json['success'] + '</div>');
window.location = "URL_WITH_SHOP";
}
}
});
});
//--></script>
Above is the presentation layer, now let's make the logical layer, go to shop/catalog/controller/account and create loginajax.php and paste following code:
<?php
class ControllerAccountLoginAjax extends Controller
{
private $error = array();
public function index()
{
$this->load->model('account/customer');
$json = array();
// Login override for admin users
if (!empty($this->request->get['token'])) {
$this->event->trigger('pre.customer.login');
$this->customer->logout();
$this->cart->clear();
unset($this->session->data['wishlist']);
unset($this->session->data['payment_address']);
unset($this->session->data['payment_method']);
unset($this->session->data['payment_methods']);
unset($this->session->data['shipping_address']);
unset($this->session->data['shipping_method']);
unset($this->session->data['shipping_methods']);
unset($this->session->data['comment']);
unset($this->session->data['order_id']);
unset($this->session->data['coupon']);
unset($this->session->data['reward']);
unset($this->session->data['voucher']);
unset($this->session->data['vouchers']);
$customer_info = $this->model_account_customer->getCustomerByToken($this->request->get['token']);
if ($customer_info && $this->customer->login($customer_info['email'], '', true)) {
// Default Addresses
$this->load->model('account/address');
if ($this->config->get('config_tax_customer') == 'payment') {
$this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
}
if ($this->config->get('config_tax_customer') == 'shipping') {
$this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
}
$this->event->trigger('post.customer.login');
$this->response->redirect($this->url->link('account/account', '', 'SSL'));
}
}
if ($this->customer->isLogged()) {
$this->response->redirect($this->url->link('account/account', '', 'SSL'));
}
if (!$json) {
$this->load->language('account/login');
$this->document->setTitle($this->language->get('heading_title'));
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$json['success'] = "Successfully logging in! ";
unset($this->session->data['guest']);
// Default Shipping Address
$this->load->model('account/address');
if ($this->config->get('config_tax_customer') == 'payment') {
$this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
}
if ($this->config->get('config_tax_customer') == 'shipping') {
$this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
}
// Add to activity log
$this->load->model('account/activity');
$activity_data = array(
'customer_id' => $this->customer->getId(),
'name' => $this->customer->getFirstName() . ' ' . $this->customer->getLastName()
);
$this->model_account_activity->addActivity('login', $activity_data);
}
else{
$json['error'] = $this->language->get('error_login');
}
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_account'),
'href' => $this->url->link('account/account', '', 'SSL')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_login'),
'href' => $this->url->link('account/login', '', 'SSL')
);
$data['heading_title'] = $this->language->get('heading_title');
$data['text_new_customer'] = $this->language->get('text_new_customer');
$data['text_register'] = $this->language->get('text_register');
$data['text_register_account'] = $this->language->get('text_register_account');
$data['text_returning_customer'] = $this->language->get('text_returning_customer');
$data['text_i_am_returning_customer'] = $this->language->get('text_i_am_returning_customer');
$data['text_forgotten'] = $this->language->get('text_forgotten');
$data['entry_email'] = $this->language->get('entry_email');
$data['entry_password'] = $this->language->get('entry_password');
$data['button_continue'] = $this->language->get('button_continue');
$data['button_login'] = $this->language->get('button_login');
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$data['action'] = $this->url->link('account/login', '', 'SSL');
$data['register'] = $this->url->link('account/register', '', 'SSL');
$data['forgotten'] = $this->url->link('account/forgotten', '', 'SSL');
if (isset($this->session->data['success'])) {
$data['success'] = $this->session->data['success'];
unset($this->session->data['success']);
} else {
$data['success'] = '';
}
if (isset($this->request->post['email'])) {
$data['email'] = $this->request->post['email'];
} else {
$data['email'] = '';
}
if (isset($this->request->post['password'])) {
$data['password'] = $this->request->post['password'];
} else {
$data['password'] = '';
}
} else {
$json['error'] = $this->language->get('error_login');
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
protected function validate() {
$this->event->trigger('pre.customer.login');
// Check how many login attempts have been made.
$login_info = $this->model_account_customer->getLoginAttempts($this->request->post['email']);
if ($login_info && ($login_info['total'] >= $this->config->get('config_login_attempts')) && strtotime('-1 hour') < strtotime($login_info['date_modified'])) {
$this->error['warning'] = $this->language->get('error_attempts');
}
// Check if customer has been approved.
$customer_info = $this->model_account_customer->getCustomerByEmail($this->request->post['email']);
if ($customer_info && !$customer_info['approved']) {
$this->error['warning'] = $this->language->get('error_approved');
}
if (!$this->error) {
if (!$this->customer->login($this->request->post['email'], $this->request->post['password'])) {
$this->error['warning'] = $this->language->get('error_login');
$this->model_account_customer->addLoginAttempt($this->request->post['email']);
} else {
$this->model_account_customer->deleteLoginAttempts($this->request->post['email']);
$this->event->trigger('post.customer.login');
}
}
return !$this->error;
}
}
This will help you.
Download files and folders for above codes
Hope it also help you
If you just want the login page then here is the tricks, create index.php or index.html at root folder then paste the following code and change URL_WITH_SHOP in the code with your url with shop like "http://www.example.com/shop" :
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div id="content" class="col-sm-12 ">
<div class="row ">
<div class="col-sm-6" style="margin: 0px auto; float: none;">
<div class="well">
<h2>Returning Customer</h2>
<p><strong>I am a returning customer</strong></p>
<form action="URL_WITH_SHOP/index.php?route=account/login" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label" for="input-email">E-Mail Address</label>
<input type="text" name="email" value="" placeholder="E-Mail Address" id="input-email" class="form-control" />
</div>
<div class="form-group">
<label class="control-label" for="input-password">Password</label>
<input type="password" name="password" value="" placeholder="Password" id="input-password" class="form-control" />
</div>
<input type="submit" value="Login" class="btn btn-primary" />
</form>
</div>
</div>
</div>
</div>
</div>
</div>
The issue will be if the customer enters wrong username and password then it redirects to the actual login page.
I download example of autocomplate search list
The Problem is the list of people always appear even i
click outside of list area, i must delete the data from input field to disappear.
What i want when i click any where this list hide or disappear when i
click or enter any data appear again inside input field .
About source code
index.php
js.js (java)
db.php (Database)
index code is :
<body bgcolor="#F1f1f1">
<div id='val'>
<div>
<form>
<input type="text" id ="test" name="n" onblur="message()" onclick="showHint(this.value)" onkeyup="showHint(this.value)" placeholder="Search here" autocomplete="off" autocorrect="off" autocapitalize="off" />
<img src="Black_Search.png" class="search" width="20" height="20">
</form>
</div>
</div>
<div id='val2'>
<div id="result"></div>
<div id='more'> <a href='#' class='search_profile'>see more result</a></div>
</div>
java js.js Code is:
function showHint(str)
{
if (str.length==0)
{
document.getElementById("result").innerHTML="";
document.getElementById("val2").style.display = "none";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("result").innerHTML=xmlhttp.responseText;
document.getElementById("val2").style.display="block";
}
}
xmlhttp.open("GET","db.php?n="+str,true);
xmlhttp.send();
}
Database db.php code :
if(#mysql_real_escape_string(strip_tags($_GET['n']))){
$_dblocal='localhost';
$_dbrott='root';
$_dbpassword='';
$_dbname='search';
#mysql_connect($_dblocal,$_dbrott,$_dbpassword) or die (mysql_error());
#mysql_select_db($_dbname) or die (mysql_error());
$sele="SELECT * FROM `users` WHERE name LIKE '" .$_GET['n']. "%' ORDER BY id DESC LIMIT 5";
$que=#mysql_query($sele);
while($rows=mysql_fetch_assoc($que)){
$id= $rows['id'];
$name= $rows['name'];
$images = $rows['image'];
$desc= $rows['desc'];
echo "<a href='profile.php?id=$id' class='search_profile' >
<div class='comment'>
<img src='images/$images' width='40' height='40' align='top' class='Square' >
<b>$name</b>
<div class='dec'>$desc</div>
</div> </a>";
}
}else{
echo "Enter Name";
}