2019-11-11 17:47:48 +10:00
/* eslint-disable prefer-arrow-callback, no-var, no-tabs */
2020-02-01 17:00:05 +10:00
/* globals showNotification, numeral, feather */
2018-01-07 04:55:48 +10:00
$ ( document ) . ready ( function ( ) {
if ( $ ( window ) . width ( ) < 768 ) {
$ ( '.menu-side' ) . on ( 'click' , function ( e ) {
e . preventDefault ( ) ;
$ ( '.menu-side li:not(".active")' ) . slideToggle ( ) ;
} ) ;
$ ( '.menu-side li:not(".active")' ) . hide ( ) ;
2020-02-01 16:59:30 +10:00
$ ( '.menu-side>.active' ) . html ( '<i class="feather" data-feather="menu"></i>' ) ;
2018-01-07 04:55:48 +10:00
$ ( '.menu-side>.active' ) . addClass ( 'menu-side-mobile' ) ;
// hide menu if there are no items in it
if ( $ ( '#navbar ul li' ) . length === 0 ) {
$ ( '#navbar' ) . hide ( ) ;
}
$ ( '#offcanvasClose' ) . hide ( ) ;
}
2019-12-16 14:22:27 +10:00
$ ( '#userSetupForm' ) . validator ( ) . on ( 'submit' , function ( e ) {
if ( ! e . isDefaultPrevented ( ) ) {
e . preventDefault ( ) ;
$ . ajax ( {
method : 'POST' ,
url : '/admin/setup_action' ,
data : {
usersName : $ ( '#usersName' ) . val ( ) ,
userEmail : $ ( '#userEmail' ) . val ( ) ,
userPassword : $ ( '#userPassword' ) . val ( )
}
} )
. done ( function ( msg ) {
showNotification ( msg . message , 'success' , false , '/admin/login' ) ;
} )
. fail ( function ( msg ) {
showNotification ( msg . responseJSON . message , 'danger' ) ;
} ) ;
}
} ) ;
2019-12-21 12:46:48 +10:00
$ ( document ) . on ( 'click' , '.menu-btn' , function ( e ) {
2018-01-07 04:55:48 +10:00
e . preventDefault ( ) ;
2019-12-21 12:46:48 +10:00
$ ( 'body' ) . addClass ( 'pushy-open-right' ) ;
2018-01-07 04:55:48 +10:00
} ) ;
// add the table class to all tables
$ ( 'table' ) . each ( function ( ) {
$ ( this ) . addClass ( 'table table-hover' ) ;
} ) ;
2019-12-30 13:10:57 +10:00
if ( $ ( '#productTags' ) . length ) {
$ ( '#productTags' ) . tokenfield ( ) ;
}
2018-01-07 04:55:48 +10:00
$ ( document ) . on ( 'click' , '.dashboard_list' , function ( e ) {
window . document . location = $ ( this ) . attr ( 'href' ) ;
} ) . hover ( function ( ) {
$ ( this ) . toggleClass ( 'hover' ) ;
} ) ;
$ ( document ) . on ( 'click' , '.btn-qty-minus' , function ( e ) {
2019-12-26 13:23:21 +10:00
e . preventDefault ( ) ;
2018-01-07 04:55:48 +10:00
var qtyElement = $ ( e . target ) . parent ( ) . parent ( ) . find ( '.cart-product-quantity' ) ;
$ ( qtyElement ) . val ( parseInt ( qtyElement . val ( ) ) - 1 ) ;
cartUpdate ( qtyElement ) ;
} ) ;
$ ( document ) . on ( 'click' , '.btn-qty-add' , function ( e ) {
2019-12-26 13:23:21 +10:00
e . preventDefault ( ) ;
2018-01-07 04:55:48 +10:00
var qtyElement = $ ( e . target ) . parent ( ) . parent ( ) . find ( '.cart-product-quantity' ) ;
$ ( qtyElement ) . val ( parseInt ( qtyElement . val ( ) ) + 1 ) ;
cartUpdate ( qtyElement ) ;
} ) ;
2018-05-21 23:36:12 +10:00
$ ( document ) . on ( 'click' , '.btn-delete-from-cart' , function ( e ) {
deleteFromCart ( $ ( e . target ) ) ;
} ) ;
2018-01-07 04:55:48 +10:00
if ( $ ( '#pager' ) . length ) {
var pageNum = $ ( '#pageNum' ) . val ( ) ;
var pageLen = $ ( '#productsPerPage' ) . val ( ) ;
var productCount = $ ( '#totalProductCount' ) . val ( ) ;
var paginateUrl = $ ( '#paginateUrl' ) . val ( ) ;
var searchTerm = $ ( '#searchTerm' ) . val ( ) ;
if ( searchTerm !== '' ) {
searchTerm = searchTerm + '/' ;
}
var pagerHref = '/' + paginateUrl + '/' + searchTerm + '{{number}}' ;
var totalProducts = Math . ceil ( productCount / pageLen ) ;
if ( parseInt ( productCount ) > parseInt ( pageLen ) ) {
$ ( '#pager' ) . bootpag ( {
total : totalProducts ,
page : pageNum ,
maxVisible : 5 ,
2018-02-14 06:22:12 +10:00
href : pagerHref ,
wrapClass : 'pagination' ,
2019-12-26 13:23:21 +10:00
prevClass : 'page-item previous' ,
nextClass : 'page-item next' ,
activeClass : 'page-item active'
} ) ;
// Fix for Bootstrap 4
$ ( '#pager a' ) . each ( function ( ) {
$ ( this ) . addClass ( 'page-link' ) ;
2018-01-07 04:55:48 +10:00
} ) ;
}
}
2018-01-22 07:20:33 +10:00
$ ( '#customerLogout' ) . on ( 'click' , function ( e ) {
$ . ajax ( {
method : 'POST' ,
url : '/customer/logout' ,
data : { }
} )
. done ( function ( msg ) {
location . reload ( ) ;
} ) ;
} ) ;
2019-12-16 16:33:51 +10:00
$ ( '#customerForgotten' ) . validator ( ) . on ( 'submit' , function ( e ) {
if ( ! e . isDefaultPrevented ( ) ) {
e . preventDefault ( ) ;
$ . ajax ( {
method : 'POST' ,
url : '/customer/forgotten_action' ,
data : {
email : $ ( '#email' ) . val ( )
}
} )
. done ( function ( msg ) {
showNotification ( msg . message , 'success' ) ;
} )
. fail ( function ( msg ) {
if ( msg . message ) {
showNotification ( msg . responseJSON . message , 'danger' ) ;
return ;
}
showNotification ( msg . responseText , 'danger' ) ;
} ) ;
}
} ) ;
2019-12-21 12:46:48 +10:00
$ ( document ) . on ( 'click' , '#createAccountCheckbox' , function ( e ) {
$ ( '#newCustomerPassword' ) . prop ( 'required' , $ ( '#createAccountCheckbox' ) . prop ( 'checked' ) ) ;
} ) ;
$ ( '#checkoutInformation' ) . validator ( ) . on ( 'click' , function ( e ) {
2018-01-22 07:20:33 +10:00
e . preventDefault ( ) ;
if ( $ ( '#shipping-form' ) . validator ( 'validate' ) . has ( '.has-error' ) . length === 0 ) {
2019-12-21 12:46:48 +10:00
// Change route if customer to be saved for later
var route = '/customer/save' ;
if ( $ ( '#createAccountCheckbox' ) . prop ( 'checked' ) ) {
route = '/customer/create' ;
}
2018-01-22 07:20:33 +10:00
$ . ajax ( {
method : 'POST' ,
2019-12-21 12:46:48 +10:00
url : route ,
2018-01-22 07:20:33 +10:00
data : {
email : $ ( '#shipEmail' ) . val ( ) ,
2020-03-07 19:37:49 +10:00
company : $ ( '#shipCompany' ) . val ( ) ,
2018-01-22 07:20:33 +10:00
firstName : $ ( '#shipFirstname' ) . val ( ) ,
lastName : $ ( '#shipLastname' ) . val ( ) ,
address1 : $ ( '#shipAddr1' ) . val ( ) ,
address2 : $ ( '#shipAddr2' ) . val ( ) ,
country : $ ( '#shipCountry' ) . val ( ) ,
state : $ ( '#shipState' ) . val ( ) ,
postcode : $ ( '#shipPostcode' ) . val ( ) ,
phone : $ ( '#shipPhoneNumber' ) . val ( ) ,
2019-12-21 12:46:48 +10:00
password : $ ( '#newCustomerPassword' ) . val ( ) ,
orderComment : $ ( '#orderComment' ) . val ( )
2018-01-22 07:20:33 +10:00
}
} )
2019-12-19 15:05:48 +10:00
. done ( function ( ) {
2019-12-21 12:46:48 +10:00
window . location = '/checkout/shipping' ;
2018-01-22 07:20:33 +10:00
} )
. fail ( function ( msg ) {
2019-11-23 08:35:19 +10:00
showNotification ( msg . responseJSON . message , 'danger' ) ;
2018-01-22 07:20:33 +10:00
} ) ;
}
} ) ;
2020-01-21 17:36:46 +10:00
$ ( '#addDiscountCode' ) . on ( 'click' , function ( e ) {
e . preventDefault ( ) ;
$ . ajax ( {
method : 'POST' ,
url : '/checkout/adddiscountcode' ,
data : {
discountCode : $ ( '#discountCode' ) . val ( )
}
} )
. done ( function ( msg ) {
showNotification ( msg . message , 'success' , true ) ;
} )
. fail ( function ( msg ) {
showNotification ( msg . responseJSON . message , 'danger' ) ;
} ) ;
} ) ;
$ ( '#removeDiscountCode' ) . on ( 'click' , function ( e ) {
e . preventDefault ( ) ;
$ . ajax ( {
method : 'POST' ,
url : '/checkout/removediscountcode' ,
data : { }
} )
. done ( function ( msg ) {
showNotification ( msg . message , 'success' , true ) ;
} )
. fail ( function ( msg ) {
showNotification ( msg . responseJSON . message , 'danger' ) ;
} ) ;
} ) ;
2018-02-23 03:41:24 +10:00
$ ( '#loginForm' ) . on ( 'click' , function ( e ) {
if ( ! e . isDefaultPrevented ( ) ) {
e . preventDefault ( ) ;
$ . ajax ( {
method : 'POST' ,
url : '/admin/login_action' ,
data : {
email : $ ( '#email' ) . val ( ) ,
password : $ ( '#password' ) . val ( )
}
} )
. done ( function ( msg ) {
window . location = '/admin' ;
} )
. fail ( function ( msg ) {
showNotification ( msg . responseJSON . message , 'danger' ) ;
} ) ;
}
e . preventDefault ( ) ;
} ) ;
2018-01-22 07:20:33 +10:00
// call update settings API
$ ( '#customerLogin' ) . on ( 'click' , function ( e ) {
if ( ! e . isDefaultPrevented ( ) ) {
e . preventDefault ( ) ;
$ . ajax ( {
method : 'POST' ,
url : '/customer/login_action' ,
data : {
loginEmail : $ ( '#customerLoginEmail' ) . val ( ) ,
loginPassword : $ ( '#customerLoginPassword' ) . val ( )
}
} )
. done ( function ( msg ) {
var customer = msg . customer ;
// Fill in customer form
$ ( '#shipEmail' ) . val ( customer . email ) ;
$ ( '#shipFirstname' ) . val ( customer . firstName ) ;
$ ( '#shipLastname' ) . val ( customer . lastName ) ;
$ ( '#shipAddr1' ) . val ( customer . address1 ) ;
$ ( '#shipAddr2' ) . val ( customer . address2 ) ;
$ ( '#shipCountry' ) . val ( customer . country ) ;
$ ( '#shipState' ) . val ( customer . state ) ;
$ ( '#shipPostcode' ) . val ( customer . postcode ) ;
$ ( '#shipPhoneNumber' ) . val ( customer . phone ) ;
location . reload ( ) ;
} )
. fail ( function ( msg ) {
2018-02-24 05:28:05 +10:00
showNotification ( msg . responseJSON . message , 'danger' ) ;
2018-01-22 07:20:33 +10:00
} ) ;
}
e . preventDefault ( ) ;
} ) ;
2018-01-07 04:55:48 +10:00
$ ( document ) . on ( 'click' , '.image-next' , function ( e ) {
var thumbnails = $ ( '.thumbnail-image' ) ;
var index = 0 ;
var matchedIndex = 0 ;
// get the current src image and go to the next one
$ ( '.thumbnail-image' ) . each ( function ( ) {
if ( $ ( '#product-title-image' ) . attr ( 'src' ) === $ ( this ) . attr ( 'src' ) ) {
if ( index + 1 === thumbnails . length || index + 1 < 0 ) {
matchedIndex = 0 ;
} else {
matchedIndex = index + 1 ;
}
}
index ++ ;
} ) ;
// set the image src
$ ( '#product-title-image' ) . attr ( 'src' , $ ( thumbnails ) . eq ( matchedIndex ) . attr ( 'src' ) ) ;
} ) ;
$ ( document ) . on ( 'click' , '.image-prev' , function ( e ) {
var thumbnails = $ ( '.thumbnail-image' ) ;
var index = 0 ;
var matchedIndex = 0 ;
// get the current src image and go to the next one
$ ( '.thumbnail-image' ) . each ( function ( ) {
if ( $ ( '#product-title-image' ) . attr ( 'src' ) === $ ( this ) . attr ( 'src' ) ) {
if ( index - 1 === thumbnails . length || index - 1 < 0 ) {
matchedIndex = thumbnails . length - 1 ;
} else {
matchedIndex = index - 1 ;
}
}
index ++ ;
} ) ;
// set the image src
$ ( '#product-title-image' ) . attr ( 'src' , $ ( thumbnails ) . eq ( matchedIndex ) . attr ( 'src' ) ) ;
} ) ;
$ ( document ) . on ( 'click' , '.product-add-to-cart' , function ( e ) {
var productOptions = getSelectedOptions ( ) ;
2020-01-07 21:03:35 +10:00
if ( parseInt ( $ ( '#product_quantity' ) . val ( ) ) < 1 ) {
$ ( '#product_quantity' ) . val ( 1 ) ;
2018-05-21 23:36:12 +10:00
}
2018-01-07 04:55:48 +10:00
$ . ajax ( {
method : 'POST' ,
2018-02-04 01:23:59 +10:00
url : '/product/addtocart' ,
2018-10-05 21:54:42 +10:00
data : {
productId : $ ( '#productId' ) . val ( ) ,
productQuantity : $ ( '#product_quantity' ) . val ( ) ,
productOptions : JSON . stringify ( productOptions ) ,
productComment : $ ( '#product_comment' ) . val ( )
}
2018-01-07 04:55:48 +10:00
} )
. done ( function ( msg ) {
2020-01-04 12:37:35 +10:00
showNotification ( msg . message , 'success' ) ;
updateCartDiv ( ) ;
2018-01-07 04:55:48 +10:00
} )
. fail ( function ( msg ) {
showNotification ( msg . responseJSON . message , 'danger' ) ;
} ) ;
} ) ;
2020-01-08 19:12:32 +10:00
$ ( '#product_quantity' ) . on ( 'keyup' , function ( e ) {
checkMaxQuantity ( e , $ ( '#product_quantity' ) ) ;
} ) ;
$ ( '.cart-product-quantity' ) . on ( 'keyup' , function ( e ) {
checkMaxQuantity ( e , $ ( '.cart-product-quantity' ) ) ;
} ) ;
2019-12-30 13:10:57 +10:00
$ ( '.cart-product-quantity' ) . on ( 'focusout' , function ( e ) {
cartUpdate ( $ ( e . target ) ) ;
2018-01-07 04:55:48 +10:00
} ) ;
$ ( document ) . on ( 'click' , '.pushy-link' , function ( e ) {
$ ( 'body' ) . removeClass ( 'pushy-open-right' ) ;
} ) ;
$ ( document ) . on ( 'click' , '.add-to-cart' , function ( e ) {
var productLink = '/product/' + $ ( this ) . attr ( 'data-id' ) ;
if ( $ ( this ) . attr ( 'data-link' ) ) {
productLink = '/product/' + $ ( this ) . attr ( 'data-link' ) ;
}
if ( $ ( this ) . attr ( 'data-has-options' ) === 'true' ) {
window . location = productLink ;
} else {
$ . ajax ( {
method : 'POST' ,
2018-02-04 01:23:59 +10:00
url : '/product/addtocart' ,
2019-06-15 14:46:08 +10:00
data : { productId : $ ( this ) . attr ( 'data-id' ) }
2018-01-07 04:55:48 +10:00
} )
. done ( function ( msg ) {
2020-01-04 12:37:35 +10:00
showNotification ( msg . message , 'success' ) ;
updateCartDiv ( ) ;
2018-01-07 04:55:48 +10:00
} )
. fail ( function ( msg ) {
showNotification ( msg . responseJSON . message , 'danger' ) ;
} ) ;
}
} ) ;
2020-01-21 20:00:52 +10:00
// On empty cart click
2018-01-07 04:55:48 +10:00
$ ( document ) . on ( 'click' , '#empty-cart' , function ( e ) {
2020-01-21 20:00:52 +10:00
$ ( '#confirmModal' ) . modal ( 'show' ) ;
$ ( '#buttonConfirm' ) . attr ( 'data-func' , 'emptyCart' ) ;
} ) ;
$ ( document ) . on ( 'click' , '#buttonConfirm' , function ( e ) {
// Get the function and run it
var func = $ ( e . target ) . attr ( 'data-func' ) ;
window [ func ] ( ) ;
$ ( '#confirmModal' ) . modal ( 'hide' ) ;
2018-01-07 04:55:48 +10:00
} ) ;
$ ( '.qty-btn-minus' ) . on ( 'click' , function ( ) {
2019-02-09 13:05:41 +10:00
var number = parseInt ( $ ( '#product_quantity' ) . val ( ) ) - 1 ;
$ ( '#product_quantity' ) . val ( number > 0 ? number : 1 ) ;
2018-01-07 04:55:48 +10:00
} ) ;
$ ( '.qty-btn-plus' ) . on ( 'click' , function ( ) {
2018-02-14 06:22:12 +10:00
$ ( '#product_quantity' ) . val ( parseInt ( $ ( '#product_quantity' ) . val ( ) ) + 1 ) ;
2018-01-07 04:55:48 +10:00
} ) ;
// product thumbnail image click
$ ( '.thumbnail-image' ) . on ( 'click' , function ( ) {
$ ( '#product-title-image' ) . attr ( 'src' , $ ( this ) . attr ( 'src' ) ) ;
} ) ;
// resets the order filter
$ ( document ) . on ( 'click' , '#btn_search_reset' , function ( e ) {
window . location . replace ( '/' ) ;
} ) ;
2018-01-22 07:20:33 +10:00
// search button click event
2018-01-07 04:55:48 +10:00
$ ( document ) . on ( 'click' , '#btn_search' , function ( e ) {
e . preventDefault ( ) ;
if ( $ ( '#frm_search' ) . val ( ) . trim ( ) === '' ) {
showNotification ( 'Please enter a search value' , 'danger' ) ;
} else {
window . location . href = '/search/' + $ ( '#frm_search' ) . val ( ) ;
}
} ) ;
if ( $ ( '#input_notify_message' ) . val ( ) !== '' ) {
// save values from inputs
var messageVal = $ ( '#input_notify_message' ) . val ( ) ;
var messageTypeVal = $ ( '#input_notify_messageType' ) . val ( ) ;
// clear inputs
$ ( '#input_notify_message' ) . val ( '' ) ;
$ ( '#input_notify_messageType' ) . val ( '' ) ;
// alert
2019-12-26 13:23:21 +10:00
showNotification ( messageVal , messageTypeVal || 'danger' , false ) ;
2018-01-07 04:55:48 +10:00
}
2020-03-14 10:51:26 +10:00
// checkout-blockonomics page (blockonomics_payment route) handling START ***
if ( $ ( '#blockonomics_div' ) . length > 0 ) {
2020-03-14 10:57:09 +10:00
var orderid = $ ( '#blockonomics_div' ) . data ( 'orderid' ) || '' ;
var timestamp = $ ( '#blockonomics_div' ) . data ( 'timestamp' ) || - 1 ;
var address = $ ( '#blockonomics_div' ) . data ( 'address' ) || '' ;
var blSocket = new WebSocket ( 'wss://www.blockonomics.co/payment/' + address + '?timestamp=' + timestamp ) ;
blSocket . onopen = function ( msg ) {
} ;
var timeOutMinutes = 10 ;
setTimeout ( function ( ) {
$ ( '#blockonomics_waiting' ) . html ( '<b>Payment expired</b><br><br><b><a href=\'/checkout/payment\'>Click here</a></b> to try again.<br><br>If you already paid, your order will be processed automatically.' ) ;
showNotification ( 'Payment expired' , 'danger' ) ;
blSocket . close ( ) ;
} , 1000 * 60 * timeOutMinutes ) ;
var countdownel = $ ( '#blockonomics_timeout' ) ;
var endDatebl = new Date ( ( new Date ( ) ) . getTime ( ) + 1000 * 60 * timeOutMinutes ) ;
var blcountdown = setInterval ( function ( ) {
var now = new Date ( ) . getTime ( ) ;
var distance = endDatebl - now ;
if ( distance < 0 ) {
clearInterval ( blcountdown ) ;
return ;
}
var minutes = Math . floor ( ( distance % ( 1000 * 60 * 60 ) ) / ( 1000 * 60 ) ) ;
var seconds = Math . floor ( ( distance % ( 1000 * 60 ) ) / 1000 ) ;
countdownel . html ( minutes + 'm ' + seconds + 's' ) ;
} , 1000 ) ;
blSocket . onmessage = function ( msg ) {
var data = JSON . parse ( msg . data ) ;
if ( ( data . status === 0 ) || ( data . status === 1 ) || ( data . status === 2 ) ) {
// redirect to order confirmation page
var orderMessage = '<br>View <b><a href="/payment/' + orderid + '">Order</a></b>' ;
$ ( '#blockonomics_waiting' ) . html ( 'Payment detected (<b>' + data . value / 1e8 + ' BTC</b>).' + orderMessage ) ;
showNotification ( 'Payment detected' , 'success' ) ;
$ ( '#cart-count' ) . html ( '0' ) ;
blSocket . close ( ) ;
$ . ajax ( { method : 'POST' , url : '/product/emptycart' } ) . done ( function ( ) {
window . location . replace ( '/payment/' + orderid ) ;
} ) ;
}
} ;
2020-03-14 10:51:26 +10:00
}
// checkout-blockonomics page (blockonomics_payment route) handling *** END
2018-01-07 04:55:48 +10:00
} ) ;
2020-01-08 19:12:32 +10:00
function checkMaxQuantity ( e , element ) {
if ( $ ( '#maxQuantity' ) . length ) {
if ( e . keyCode === 46 || e . keyCode === 8 ) {
return ;
}
if ( parseInt ( $ ( e . target ) . val ( ) ) > parseInt ( $ ( '#maxQuantity' ) . val ( ) ) ) {
const qty = element . val ( ) ;
e . preventDefault ( ) ;
element . val ( qty . slice ( 0 , - 1 ) ) ;
showNotification ( ` Exceeds maximum quantity: ${ $ ( '#maxQuantity' ) . val ( ) } ` , 'warning' , false ) ;
}
}
}
2018-01-07 04:55:48 +10:00
function deleteFromCart ( element ) {
$ . ajax ( {
method : 'POST' ,
2018-02-04 01:23:59 +10:00
url : '/product/removefromcart' ,
2020-01-21 20:00:52 +10:00
data : {
cartId : element . attr ( 'data-cartid' )
}
2018-01-07 04:55:48 +10:00
} )
. done ( function ( msg ) {
2020-01-04 12:37:35 +10:00
showNotification ( msg . message , 'success' ) ;
updateCartDiv ( ) ;
2018-01-07 04:55:48 +10:00
} )
. fail ( function ( msg ) {
showNotification ( msg . responseJSON . message , 'danger' ) ;
} ) ;
}
function cartUpdate ( element ) {
2018-05-21 23:36:12 +10:00
if ( $ ( element ) . val ( ) > 0 ) {
2018-01-07 04:55:48 +10:00
if ( $ ( element ) . val ( ) !== '' ) {
2019-12-30 13:10:57 +10:00
updateCart ( element ) ;
2018-01-07 04:55:48 +10:00
}
2018-05-21 23:36:12 +10:00
} else {
$ ( element ) . val ( 1 ) ;
2018-01-07 04:55:48 +10:00
}
}
2019-12-30 13:10:57 +10:00
function updateCart ( element ) {
2018-01-07 04:55:48 +10:00
// update cart on server
$ . ajax ( {
method : 'POST' ,
2018-02-04 01:23:59 +10:00
url : '/product/updatecart' ,
2019-12-30 13:10:57 +10:00
data : {
2020-01-21 20:00:52 +10:00
cartId : element . attr ( 'data-cartid' ) ,
2019-12-30 13:10:57 +10:00
productId : element . attr ( 'data-id' ) ,
quantity : element . val ( )
}
2018-01-07 04:55:48 +10:00
} )
. done ( function ( msg ) {
2020-01-04 12:37:35 +10:00
updateCartDiv ( ) ;
2018-01-07 04:55:48 +10:00
} )
. fail ( function ( msg ) {
2019-02-09 20:46:41 +10:00
showNotification ( msg . responseJSON . message , 'danger' , true ) ;
2018-01-07 04:55:48 +10:00
} ) ;
}
function getSelectedOptions ( ) {
var options = { } ;
$ ( '.product-opt' ) . each ( function ( ) {
2019-02-10 13:46:42 +10:00
var optionValue = $ ( this ) . val ( ) . trim ( ) ;
2020-01-02 13:37:36 +10:00
var optionLabel = $ ( this ) . attr ( 'data-label' ) ;
var optionName = $ ( this ) . attr ( 'name' ) ;
var optionType = $ ( this ) . attr ( 'type' ) ;
// If select option
if ( ! optionType ) {
options [ optionName ] = {
label : optionLabel ,
name : optionName ,
value : optionValue
} ;
}
// If radio option
if ( optionType === 'radio' ) {
options [ optionName ] = {
label : optionLabel ,
name : optionName ,
value : $ ( 'input[name="' + optionName + '"]:checked' ) . val ( )
} ;
}
// If checkbox option
if ( optionType === 'checkbox' ) {
options [ optionName ] = {
label : optionLabel ,
name : optionName ,
value : $ ( 'input[name="' + $ ( this ) . attr ( 'name' ) + '"]' ) . is ( ':checked' )
} ;
2019-02-10 13:46:42 +10:00
}
2018-01-07 04:55:48 +10:00
} ) ;
return options ;
}
2020-01-04 12:37:35 +10:00
function updateCartDiv ( ) {
$ . ajax ( {
method : 'GET' ,
url : '/checkout/cartdata'
} )
. done ( function ( result ) {
// Update the cart div
var cart = result . cart ;
var session = result . session ;
var productHtml = '' ;
var totalAmount = numeral ( session . totalCartAmount ) . format ( '0.00' ) ;
// Work out the shipping
var shippingTotalAmt = numeral ( session . totalCartShipping ) . format ( '0.00' ) ;
2020-01-04 17:10:58 +10:00
var shippingTotal = ` ${ session . shippingMessage } :<strong id="shipping-amount"> ${ result . currencySymbol } ${ shippingTotalAmt } </strong> ` ;
if ( session . totalCartShipping === 0 ) {
2020-01-04 17:26:30 +10:00
shippingTotal = ` <span id="shipping-amount"> ${ session . shippingMessage } </span> ` ;
2020-01-04 12:37:35 +10:00
}
2020-01-21 17:36:46 +10:00
var discountTotalAmt = numeral ( session . totalCartDiscount ) . format ( '0.00' ) ;
var discountTotal = '' ;
if ( session . totalCartDiscount > 0 ) {
discountTotal = `
< div class = "text-right" >
Discount : < strong id = "discount-amount" > $ { result . currencySymbol } $ { discountTotalAmt } < / s t r o n g >
< / d i v > ` ;
}
2020-01-04 12:37:35 +10:00
// If the cart has contents
if ( cart ) {
$ ( '#cart-empty' ) . empty ( ) ;
Object . keys ( cart ) . forEach ( function ( productId ) {
var item = cart [ productId ] ;
// Setup the product
var productTotalAmount = numeral ( item . totalItemPrice ) . format ( '0.00' ) ;
var optionsHtml = '' ;
var optionIndex = 1 ;
Object . keys ( item . options ) . forEach ( function ( key ) {
var option = item . options [ key ] ;
if ( optionIndex === Object . keys ( item . options ) . length ) {
optionsHtml += ` <strong> ${ upperFirst ( option . name ) } </strong>: ${ option . value } ` ;
} else {
optionsHtml += ` <strong> ${ upperFirst ( option . name ) } </strong>: ${ option . value } / ` ;
}
optionIndex ++ ;
} ) ;
var productImage = ` <img class="img-fluid" src="/uploads/placeholder.png" alt=" ${ item . title } product image"></img> ` ;
if ( item . productImage ) {
productImage = ` <img class="img-fluid" src=" ${ item . productImage } " alt=" ${ item . title } product image"></img> ` ;
}
// Setup the product html
productHtml += `
< div class = "d-flex flex-row bottom-pad-15" >
2020-01-07 11:34:22 +10:00
< div class = "p-2 cart-product" >
2020-01-04 12:37:35 +10:00
< div class = "row h-200" >
2020-01-04 21:04:42 +10:00
< div class = "col-4 col-md-3 no-pad-left" >
$ { productImage }
2020-01-04 12:37:35 +10:00
< / d i v >
2020-01-04 21:04:42 +10:00
< div class = "col-8 col-md-9" >
< div class = "row" >
< div class = "col-12 no-pad-left mt-md-4" >
< h6 > < a href = "/product/${item.link}" > $ { item . title } < / a > < / h 6 >
$ { optionsHtml }
< / d i v >
< div class = "col-12 col-md-6 no-pad-left mb-2" >
< div class = "input-group" >
< div class = "input-group-prepend" >
2020-01-21 17:36:46 +10:00
< button class = "btn btn-primary btn-qty-minus" type = "button" > - < / b u t t o n >
2020-01-04 21:04:42 +10:00
< / d i v >
2020-01-21 20:00:52 +10:00
< input type = "number" class = "form-control cart-product-quantity text-center" data - cartid = "${productId}" data - id = "${item.id}" maxlength = "2" value = "${item.quantity}" >
2020-01-04 21:04:42 +10:00
< div class = "input-group-append" >
2020-01-21 17:36:46 +10:00
< button class = "btn btn-primary btn-qty-add" type = "button" > + < / b u t t o n >
2020-01-04 21:04:42 +10:00
< / d i v >
< / d i v >
2020-01-04 12:37:35 +10:00
< / d i v >
2020-01-04 21:04:42 +10:00
< div class = "col-4 col-md-2 no-pad-left" >
2020-02-01 16:59:30 +10:00
< button class = "btn btn-danger btn-delete-from-cart" data - cartid = "${productId}" type = "button" > < i class = "feather" data - feather = "trash-2" data - cartid = "${productId}" > < / i > < / b u t t o n >
2020-01-04 21:04:42 +10:00
< / d i v >
< div class = "col-8 col-md-4 align-self-center text-right" >
< strong class = "my-auto" > $ { result . currencySymbol } $ { productTotalAmount } < / s t r o n g >
2020-01-04 12:37:35 +10:00
< / d i v >
< / d i v >
< / d i v >
< / d i v >
< / d i v >
< / d i v > ` ;
} ) ;
2020-01-04 17:10:58 +10:00
$ ( '.cartBodyWrapper' ) . html ( productHtml ) ;
2020-01-04 12:37:35 +10:00
} else {
2020-01-04 17:10:58 +10:00
$ ( '.cartBodyWrapper' ) . html ( '' ) ;
2020-01-04 12:37:35 +10:00
}
2020-01-05 08:15:28 +10:00
$ ( '#cart-count' ) . text ( session . totalCartItems ) ;
2020-01-04 12:37:35 +10:00
// Set the totals section
var cartTotalsHtml = `
2020-01-07 11:34:22 +10:00
< div class = "d-flex flex-row" >
2020-01-04 12:37:35 +10:00
< div class = "cart-contents-shipping col-md-12 no-pad-right" >
< div class = "text-right" >
2020-01-04 17:10:58 +10:00
$ { shippingTotal }
2020-01-04 12:37:35 +10:00
< / d i v >
2020-01-21 17:36:46 +10:00
$ { discountTotal }
2020-01-04 12:37:35 +10:00
< div class = "text-right" >
Total :
< strong id = "total-cart-amount" > $ { result . currencySymbol } $ { totalAmount } < / s t r o n g >
< / d i v >
< / d i v >
< / d i v > ` ;
var cartTotalsEmptyHtml = `
2020-01-07 11:34:22 +10:00
< div id = "cart-empty" class = "d-flex flex-row" >
< div class = " cart - contents - shipping col - md - 12 no - pad - left >
2020-01-04 12:37:35 +10:00
Cart empty
< / d i v >
< / d i v > ` ;
// Set depending on cart contents
if ( cart ) {
2020-01-04 17:10:58 +10:00
$ ( '.cartTotalsWrapper' ) . html ( cartTotalsHtml ) ;
$ ( '.cart-buttons' ) . removeClass ( 'd-none' ) ;
2020-01-04 12:37:35 +10:00
} else {
2020-01-04 17:10:58 +10:00
$ ( '.cartTotalsWrapper' ) . html ( cartTotalsEmptyHtml ) ;
$ ( '.cart-buttons' ) . addClass ( 'd-none' ) ;
2020-01-04 12:37:35 +10:00
}
2020-02-01 16:59:30 +10:00
feather . replace ( ) ;
2020-01-04 12:37:35 +10:00
} )
. fail ( function ( result ) {
showNotification ( result . responseJSON . message , 'danger' ) ;
} ) ;
}
function upperFirst ( value ) {
return value . replace ( /^\w/ , ( chr ) => {
return chr . toUpperCase ( ) ;
} ) ;
}
2020-01-21 20:00:52 +10:00
// eslint-disable-next-line no-unused-vars
function emptyCart ( ) {
$ . ajax ( {
method : 'POST' ,
url : '/product/emptycart'
} )
. done ( function ( msg ) {
showNotification ( msg . message , 'success' , true ) ;
updateCartDiv ( ) ;
} ) ;
}