rootKID wrote: ↑Fri May 24, 2019 5:47 am
straightlight wrote: ↑Mon Oct 15, 2018 7:38 am
Setting an automated closing time session template may affect your site reputation with Search Engines. Rather disable the add-to-cart buttons during closing times than disabling the views from the categories and products page themselves.
Good idea but how to do this? I am not the best in doing JS :/
Untested but, idealistically, to start the idea. Ensure to create a backup of the existing files below before proceeding. This has been coded for OC v3.0.3.2 release.
In your catalog/controller/product folder, create the file:
product_maintenance.php .
Then, add the following:
Code: Select all
<?php
class ControllerProductProductMaintenance extends Controller {
public function index() {
}
public function validateMaintenance() {
$json = array();
if (isset($this->request->post['product_id'])) {
$this->load->language('product/product_maintenance');
if (!$this->storeIsOpen()) {
$json['error']['maintenance'] = $this->language->get('error_maintenance');
}
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
// Source: https://stackoverflow.com/questions/14904864/determine-if-business-is-open-closed-based-on-business-hours
protected function storeIsOpen() {
$status = false;
$storeSchedule = array('Mon' => ['08:00 AM' => '05:00 PM'],
'Tue' => ['08:00 AM' => '05:00 PM'],
'Wed' => ['08:00 AM' => '05:00 PM'],
'Thu' => ['08:00 AM' => '05:00 PM'],
'Fri' => ['08:00 AM' => '05:00 PM']
);
// Get current East Coast US time
$timeObject = new DateTime('America/New_York');
$timestamp = $timeObject->getTimeStamp();
$currentTime = $timeObject->setTimestamp($timestamp)->format('H:i A');
// Loop through time ranges for current day
foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) {
// Create time objects from start/end times and format as string (24hr AM/PM)
$startTime = DateTime::createFromFormat('h:i A', $startTime)->format('H:i A');
$endTime = DateTime::createFromFormat('h:i A', $endTime)->format('H:i A');
// Check if current time is within the range
if (($startTime < $currentTime) && ($currentTime < $endTime)) {
$status = true;
break;
}
}
return $status;
}
}
From the: $storeSchedule array, you can modify your time periods for each days of the week.
Followed is based on the default theme. You'll have to make the adjustment in your custom theme.
In catalog/view/theme/<your_theme>/template/product/product.twig file,
find:
Code: Select all
<script type="text/javascript"><!--
$('#button-cart').on('click', function() {
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
data: $('#product input[type=\'text\'], #product input[type=\'hidden\'], #product input[type=\'radio\']:checked, #product input[type=\'checkbox\']:checked, #product select, #product textarea'),
dataType: 'json',
beforeSend: function() {
$('#button-cart').button('loading');
},
complete: function() {
$('#button-cart').button('reset');
},
success: function(json) {
$('.alert-dismissible, .text-danger').remove();
$('.form-group').removeClass('has-error');
if (json['error']) {
if (json['error']['option']) {
for (i in json['error']['option']) {
var element = $('#input-option' + i.replace('_', '-'));
if (element.parent().hasClass('input-group')) {
element.parent().after('<div class="text-danger">' + json['error']['option'][i] + '</div>');
} else {
element.after('<div class="text-danger">' + json['error']['option'][i] + '</div>');
}
}
}
if (json['error']['recurring']) {
$('select[name=\'recurring_id\']').after('<div class="text-danger">' + json['error']['recurring'] + '</div>');
}
// Highlight any found errors
$('.text-danger').parent().addClass('has-error');
}
if (json['success']) {
$('.breadcrumb').after('<div class="alert alert-success alert-dismissible">' + json['success'] + '<button type="button" class="close" data-dismiss="alert">×</button></div>');
$('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>');
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('#cart > ul').load('index.php?route=common/cart/info ul li');
}
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
});
//--></script>
Remove it.
Then, find:
add after:
Code: Select all
$.ajax({
url: 'index.php?route=product/product_maintenance/validateMaintenance',
type: 'post',
dataType: 'json',
data: 'product_id=' + {{ product_id }}',
beforeSend: function() {
$('#button-cart').button('loading');
},
complete: function() {
$('#button-cart').button('reset');
},
success: function(json) {
$('.alert-dismissible').remove();
if (json['error']['maintenance']) {
$('#button-cart').prop('disabled', true);
$('.breadcrumb').after('<div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> ' + json['error']['maintenance'] + '</div>');
} else {
$('#button-cart').on('click', function() {
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
data: $('#product input[type=\'text\'], #product input[type=\'hidden\'], #product input[type=\'radio\']:checked, #product input[type=\'checkbox\']:checked, #product select, #product textarea'),
dataType: 'json',
beforeSend: function() {
$('#button-cart').button('loading');
},
complete: function() {
$('#button-cart').button('reset');
},
success: function(json) {
$('.alert-dismissible, .text-danger').remove();
$('.form-group').removeClass('has-error');
if (json['error']) {
if (json['error']['option']) {
for (i in json['error']['option']) {
var element = $('#input-option' + i.replace('_', '-'));
if (element.parent().hasClass('input-group')) {
element.parent().after('<div class="text-danger">' + json['error']['option'][i] + '</div>');
} else {
element.after('<div class="text-danger">' + json['error']['option'][i] + '</div>');
}
}
}
if (json['error']['recurring']) {
$('select[name=\'recurring_id\']').after('<div class="text-danger">' + json['error']['recurring'] + '</div>');
}
// Highlight any found errors
$('.text-danger').parent().addClass('has-error');
}
if (json['success']) {
$('.breadcrumb').after('<div class="alert alert-success alert-dismissible">' + json['success'] + '<button type="button" class="close" data-dismiss="alert">×</button></div>');
$('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>');
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('#cart > ul').load('index.php?route=common/cart/info ul li');
}
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
});
}
}
});
Then, in your catalog/language/en-gb/product folder, create a new language file:
product_maintenance.php .
Add:
Code: Select all
<?php
$_['error_maintenance'] = 'Our store is currently closed for the day!';
See if that solves the issue.