Skip to content

Instantly share code, notes, and snippets.

@yogeshdubey2006
Created June 9, 2019 03:00
Show Gist options
  • Save yogeshdubey2006/bbccda3c4cced3dfa2891ea30b4ae26b to your computer and use it in GitHub Desktop.
Save yogeshdubey2006/bbccda3c4cced3dfa2891ea30b4ae26b to your computer and use it in GitHub Desktop.
Magento 2 - Speical price countdown
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>
<?php if($_product->getSpecialPrice() && $_product->getSpecialToDate() && $_product->getSpecialFromDate()): ?>
<style type="text/css">
.deal-timer {
max-width: 270px;
}
.deal-timer .countdown {
display: flex;
margin-left: -4px;
margin-right: -4px;
}
.deal-timer .countdown > span {
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
display: block;
padding: 0 4px;
}
.deal-timer .countdown > span > b {
font-weight: 400;
background-color: #2a2a2a;
color: #ffffff;
font-size: 16px;
text-align: center;
display: block;
position: relative;
border-radius: 4px 4px 0 0;
line-height: 1;
padding-top: 14px;
}
.deal-timer .time-note {
display: flex;
margin-left: -4px;
margin-right: -4px;
}
.deal-timer .time-note span {
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
display: block;
margin: 0 4px;
text-align: center;
color: #999;
background-color: #2a2a2a;
border-radius: 0 0 4px 4px;
padding-bottom: 6px;
}
</style>
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objDate = $objectManager->create('Magento\Framework\Stdlib\DateTime\DateTime');
$date = $objDate->gmtDate();
$specialFromDate = date('m/d/y h:i:s A', strtotime($_product->getSpecialFromDate()));
$now = date('m/d/y h:i:s A',strtotime($date));
$endTime = str_replace(' 00:00:00','',$_product->getSpecialToDate()) ?>
<?php if($specialFromDate <= $now && $endTime > $now): ?>
<div class="deal-timer">
<div id="deal-timer-<?php echo $_product->getId(); ?>" class="countdown"></div>
<div class="time-note">
<span class="days"><?php echo __('Days') ?></span>
<span class="hours"><?php echo __('Hours') ?></span>
<span class="mins"><?php echo __('Mins') ?></span>
<span class='secs'><?php echo __('Secs') ?></span>
</div>
</div>
<script type="text/javascript">
require(['jquery','domReady!'],function($){
/**
* Contus Support Interactive.
*
* NOTICE OF LICENSE
*
* This source file is subject to the EULA
* that is bundled with this package in the file PRICE COUNTDOWN-LICENSE.txt.
*
* =================================================================
* MAGENTO EDITION USAGE NOTICE
* =================================================================
* This package designed for Magento 1.4.x and 1.5.x COMMUNITY edition
* Contus Support does not guarantee correct work of this package
* on any other Magento edition except Magento 1.4.x and 1.5.x COMMUNITY edition.
* =================================================================
*/
if (typeof(BackColor)=="undefined")
BackColor = "white";
if (typeof(ForeColor)=="undefined")
ForeColor= "black";
if (typeof(DisplayFormat)=="undefined")
DisplayFormat = "<span class='days'>%%D%%</span><span class='hours'>%%H%%</span><span class='mins'>%%M%%</span><span class='secs'>%%S%%</span>";
if (typeof(CountActive)=="undefined")
CountActive = true;
if (typeof(FinishMessage)=="undefined")
FinishMessage = "";
if (typeof(CountStepper)!="number")
CountStepper = -1;
if (typeof(LeadingZero)=="undefined")
LeadingZero = true;
CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
function calcage(secs, num1, num2) {
s = ((Math.floor(secs/num1)%num2)).toString();
if (LeadingZero && s.length < 2)
s = "0" + s;
return "<b>" + s + "</b>";
}
function CountBack(secs,iid,j) {
if (secs < 0) {
document.getElementById(iid).innerHTML = FinishMessage;
document.getElementById('caption'+j).style.display = "none";
document.getElementById('heading'+j).style.display = "none";
return;
}
DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));
document.getElementById(iid).innerHTML = DisplayStr;
if (CountActive)
setTimeout(function(){CountBack((secs+CountStepper),iid,j)}, SetTimeOutPeriod);
}
// logic
var dthen<?php echo $_product->getId(); ?> = new Date("<?php echo Date("m/d/y", strtotime($endTime)).' 11:59:59 PM'; ?>");
start = "<?php echo $now; ?>";
start_date = Date.parse(start);
var dnow<?php echo $_product->getId(); ?> = new Date(start_date);
ddiff = new Date((dthen<?php echo $_product->getId(); ?>)-(dnow<?php echo $_product->getId(); ?>));
gsecs<?php echo $_product->getId(); ?> = Math.floor(ddiff.valueOf()/1000);
var iid<?php echo $_product->getId(); ?> = "deal-timer-<?php echo $_product->getId(); ?>";
CountBack(gsecs<?php echo $_product->getId(); ?>,"deal-timer-<?php echo $_product->getId(); ?>", <?php echo $_product->getId(); ?>);
});
</script>
<?php endif ?>
<?php endif ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()): ?>
<div class="box-tocart">
<div class="fieldset">
<?php if ($block->shouldRenderQuantity()): ?>
<div class="field qty">
<label class="label" for="qty"><span><?php /* @escapeNotVerified */ echo __('Qty') ?></span></label>
<div class="control">
<div class="qty-minus"> <a class="btn-qty minus" href="#">-</a></div>
<div class="qty-input">
<input type="number"
name="qty"
id="qty"
maxlength="12"
value="<?php /* @escapeNotVerified */ echo $block->getProductDefaultQty() * 1 ?>"
title="<?php /* @escapeNotVerified */ echo __('Qty') ?>" class="input-text qty"
data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
/>
</div>
<div class="qty-plus"> <a class="btn-qty plus" href="#">+</a></div>
</div>
</div>
<script type="text/javascript">
require(['jquery','domReady!'],function($){
$(".btn-qty").click(function (event) {
var $button = $(this);
var oldValue = $button.closest('.control').find("input#qty").val();
var defaultValue = <?php /* @escapeNotVerified */ echo $block->getProductDefaultQty() * 1 ?>;
if ($button.hasClass('plus')) {
var newVal = parseFloat(oldValue) + 1;
} else {
if (oldValue > defaultValue) {
var newVal = parseFloat(oldValue) - 1;
} else {
newVal = defaultValue;
}
}
$button.closest('.control').find("input#qty").val(newVal);
event.preventDefault();
});
});
</script>
<?php endif; ?>
<div class="actions">
<button type="submit"
title="<?php /* @escapeNotVerified */ echo $buttonTitle ?>"
class="action primary tocart"
id="product-addtocart-button">
<span><?php /* @escapeNotVerified */ echo $buttonTitle ?></span>
</button>
<?php echo $block->getChildHtml('', true) ?>
</div>
</div>
</div>
<?php endif; ?>
<?php if ($block->isRedirectToCartEnabled()) : ?>
<script type="text/x-magento-init">
{
"#product_addtocart_form": {
"Magento_Catalog/product/view/validation": {
"radioCheckboxClosest": ".nested"
}
}
}
</script>
<?php else : ?>
<script>
require([
'jquery',
'mage/mage',
'Magento_Catalog/product/view/validation',
'Magento_Catalog/js/catalog-add-to-cart'
], function ($) {
'use strict';
$('[id="product_addtocart_form"]').mage('validation', {
radioCheckboxClosest: '.nested',
submitHandler: function (form) {
var widget = $(form).catalogAddToCart({
bindSubmit: false
});
widget.catalogAddToCart('submitForm', $(form));
return false;
}
});
});
</script>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment