AFTER: paid shipping pre-selected by default (with snippet)
Same checkout, same two shipping options. A small JavaScript snippet pre-selects Verzending (standaard) ($8.75) as soon as the shipping options appear, and the shipping fee is added to the total. A customer who really wants to collect can still click Afhalen in Zoelen. That manual choice is respected and is not overridden again.
Pick Netherlands as the country (any street/postcode/city) and watch the Verzending section. Payment runs on the Test Processor, so a test order can be completed without any charge.
See the default behaviour without the snippet: https://sure.ivacode.com/default-shipping-before-1542097/?line_items[0][price_id]=fc109de8-8cae-40a1-b0a4-384d9b6eadac&line_items[0][quantity]=1
The snippet (copy and paste)
Add it as a site-wide JavaScript snippet with WPCode or Code Snippets, loaded in the site footer. Change PREFERRED_METHOD_NAME if you rename the shipping method.
/**
* SureCart - pre-select a PAID shipping method by default instead of the
* cheapest / free option, so "Verzending (standaard)" is selected rather than
* the free "Afhalen in Zoelen". Customers can still switch to pickup - their
* own choice is respected and stays selected.
* Add this as a site-wide JavaScript snippet that loads in the site footer.
*/
(function () {
'use strict';
// The shipping method to pre-select (case-insensitive, partial match is fine).
var PREFERRED_METHOD_NAME = 'Verzending';
var STORE_KEY = 'scDefaultShipping:';
var inFlight = {};
function applied(id){ try { return localStorage.getItem(STORE_KEY + id) === '1'; } catch(e){ return false; } }
function markApplied(id){ try { localStorage.setItem(STORE_KEY + id, '1'); } catch(e){} }
function choicesOf(co){ return (co && co.shipping_choices && co.shipping_choices.data) || []; }
function pickTarget(list){
if (!list.length) return null;
if (PREFERRED_METHOD_NAME) {
var byName = list.filter(function(c){
var n = (c.shipping_method && c.shipping_method.name) || '';
return n.toLowerCase().indexOf(PREFERRED_METHOD_NAME.toLowerCase()) !== -1;
});
if (byName.length) return byName[0];
}
// Fallback: the cheapest PAID option.
var paid = list.filter(function(c){ return (c.amount || 0) > 0; })
.sort(function(a,b){ return a.amount - b.amount; });
return paid[0] || null;
}
function currentChoice(co, list){
for (var i=0;i<list.length;i++){ if (list[i].id === co.selected_shipping_choice) return list[i]; }
return null;
}
function selectChoice(id, done){
var tries = 0;
(function attempt(){
var host = document.querySelector('sc-shipping-choices');
var root = host && host.shadowRoot;
if (root) {
var radios = root.querySelectorAll('sc-radio');
for (var i=0;i<radios.length;i++){
if (radios[i].value === id) {
if (radios[i].checked) { done(true); return; }
if (typeof radios[i].ceClick === 'function') radios[i].ceClick();
else radios[i].checked = true;
done(true); return;
}
}
}
if (++tries < 25) setTimeout(attempt, 120); else done(false);
})();
}
function apply(co){
if (!co || !co.id) return;
if (applied(co.id) || inFlight[co.id]) return;
var list = choicesOf(co);
if (!list.length) return;
var target = pickTarget(list);
if (!target) return;
var cur = currentChoice(co, list);
if (cur && (cur.amount || 0) > 0) { markApplied(co.id); return; } // a paid option is already selected
if (co.selected_shipping_choice === target.id) { markApplied(co.id); return; }
inFlight[co.id] = true;
selectChoice(target.id, function(ok){ inFlight[co.id] = false; if (ok) markApplied(co.id); });
}
document.addEventListener('scShippingInfoAdded', function(e){ apply(e.detail); });
document.addEventListener('scCheckoutUpdated', function(e){ apply(e.detail && e.detail.currentCheckout); });
document.addEventListener('scCheckoutInitiated', function(e){ apply(e.detail); });
})();