Last active
October 16, 2016 17:57
-
-
Save MikeDigitize/06cfe4b5a4fa9638a94096529c59c311 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Check for presence of computing promo blocks (hidden by default) | |
| Check the cookie and search through SKUs to see if any anti-virus or MS office are in basket | |
| If no MS office display add to basket block and initiliase add to basket | |
| or if MS office show added to basket block | |
| If no antivirus display add to basket block and initiliase add to basket | |
| or if no antivirus show added to basket block | |
| */ | |
| var computingAttachmentSkus = { | |
| office : [], | |
| av : [] | |
| }; | |
| function cachedBasketPromos() { | |
| var promos = Array.from(document.querySelectorAll('.productServicepromotion')); | |
| return () => promos; | |
| } | |
| var getBasketPromos = cachedBasketPromos(); | |
| function getComputingPromos(promos) { | |
| return promos.filter(promo => promo.hasAttribute('data-computing-promo')); | |
| } | |
| function getOfficePromos(promos) { | |
| return promos.filter(promo => promo.hasAttribute('data-computing-promo-office')); | |
| } | |
| function getAVPromos(promos) { | |
| return promos.filter(promo => promo.hasAttribute('data-computing-promo-av')); | |
| } | |
| function checkForComputingAttachmentsInBasket(basketData) { | |
| var skusInBasket = basketData.CommaSeparatedListOfProductSkus.split(','); | |
| var skus = window.computingAttachmentSkusOverrides || computingAttachmentSkus; | |
| var skusFound = { | |
| office : skusInBasket.filter(sku => skus.office.indexOf(sku) > -1), | |
| av : skusInBasket.filter(sku => skus.av.indexOf(sku) > -1) | |
| }; | |
| return skusFound; | |
| } | |
| function getPromoStates() { | |
| var promos = getBasketPromos(); | |
| var officePromoBlocks = getOfficePromos(promos).slice(0,2); | |
| var avPromoBlocks = getAVPromos(promos).slice(0,2); | |
| return { officePromoBlocks, avPromoBlocks }; | |
| } | |
| function initiliaseAddToBasketButton(block) { | |
| var tagging = { | |
| location : 'attachInBasket', | |
| sku : block.getAttribute('data-promo-sku') | |
| }; | |
| window.AddProductsToBasket(block, tagging); | |
| } | |
| function setPromoStates(skusFound, blocks) { | |
| if(skusFound.length) { | |
| blocks.forEach(function(block) { | |
| if(block.hasAttribute('data-attachment-is-in-basket')) { | |
| block.style.display = 'block'; | |
| } | |
| }); | |
| } | |
| else { | |
| blocks.forEach(function(block) { | |
| if(block.hasAttribute('data-attachment-is-not-in-basket')) { | |
| block.style.display = 'block'; | |
| initiliaseAddToBasketButton(block); | |
| } | |
| }); | |
| } | |
| } | |
| function checkForComputingAttachmentSkusInBasket() { | |
| var promos = getBasketPromos(); | |
| var computingPromos = getComputingPromos(promos); | |
| if(computingPromos.length) { | |
| var basketData = getInBasketData(); | |
| if(basketData) { | |
| var skusFound = checkForComputingAttachmentsInBasket(basketData); | |
| var computingPromoBlocks = getPromoStates(skusFound); | |
| setPromoStates(skusFound.office, computingPromoBlocks.officePromoBlocks); | |
| setPromoStates(skusFound.av, computingPromoBlocks.avPromoBlocks); | |
| } | |
| } | |
| else { | |
| return false; | |
| } | |
| } | |
| function getInBasketData() { | |
| var inBasket = document.cookie.split(';').filter(cookie => cookie.match(/BasketSummary/)); | |
| if(inBasket.length) { | |
| return JSON.parse(inBasket | |
| .map(cookie => decodeURIComponent(cookie)) | |
| .pop() | |
| .replace(/(^\s+)?BasketSummary=/, '')); | |
| } | |
| else { | |
| return null; | |
| } | |
| } | |
| checkForComputingAttachmentSkusInBasket(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment