Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function toggleCountryPayment() {
var isPayPal = function(el) {
return el.querySelectorAll(".radio__label__primary")[0] && el.querySelectorAll(".radio__label__primary")[0].textContent.trim() === "PayPal";
}
var shipToElement = document.querySelector("address.address") && document.querySelector("address.address").firstChild;
if (shipToElement && shipToElement.data.indexOf("Malaysia") > -1) {
var els = document.querySelectorAll(".section--payment-method .radio-wrapper.content-box__row"),
radioButtons = [];
for (var i = 0; i < els.length; i++) {
var el = els[i],
radioButton = el.querySelector(radioButtonSelector);
if (el.dataset.selectGateway && isPayPal(el)) {
el.classList.add("visually-hidden");
if (document.getElementById("payment-gateway-subfields-" + el.dataset.selectGateway)) {
document.getElementById("payment-gateway-subfields-" + el.dataset.selectGateway).classList.add("visually-hidden");
}
if (radioButton.checked) {
radioButton.checked = false;
}
} else if (radioButton && el.dataset.selectGateway) {
radioButtons.push(radioButton);
}
}
if (!radioButtons.reduce(function(checked, el) { return checked || el.checked }, false)) {
radioButtons[0].click();
}
} else if (shipToElement) {
var els = document.querySelectorAll(".section--payment-method .radio-wrapper.content-box__row");
for (var i = 0; i < els.length; i++) {
var el = els[i],
radioButton = el.querySelector(radioButtonSelector);
if (el.dataset.selectGateway && !isPayPal(el)) {
el.classList.add("visually-hidden");
if (document.getElementById("payment-gateway-subfields-" + el.dataset.selectGateway)) {
document.getElementById("payment-gateway-subfields-" + el.dataset.selectGateway).classList.add("visually-hidden");
}
if (radioButton.checked) {
radioButton.checked = false;
}
} else if (el.dataset.selectGateway && isPayPal(el)) {
console.log(el)
radioButton.click();
}
}
}
}
function initObserver(shippingElement) {
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.target == document.querySelector(".main__content")) {
toggleCODPayment(shippingElement);
toggleCountryPayment();
}
});
});
observer.observe(document.querySelector(".main"), {childList: true, subtree: true});
}
(function(fn){var d=document;(d.readyState=='loading')?d.addEventListener('DOMContentLoaded',fn):fn();})(function(){
var shippingElement = document.querySelector("div.review-block:nth-child(3) > div:nth-child(1) > div:nth-child(2)");
if (shippingElement) {
initObserver(shippingElement);
toggleCODPayment(shippingElement);
toggleCountryPayment();
}
});
!function(){var e=document.createElement("style");e.appendChild(document.createTextNode("")),document.head.appendChild(e),e.sheet.insertRule(".alt-payment-list__item--paypal, .alt-payment-list__item--paypalv4 {display: none !important;}",0),e.sheet.insertRule(".alternative-payment-separator{display: none !important;}",0),e.sheet.insertRule(".dynamic-checkout{display: none !important;}",0)}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.