Scrivere funzioni personalizzate da usare nei vostri Designs
Clerk.js permette di scrivere funzioni javascript personalizzate, che aggiungono nuove funzionalità ai Designs.
I formattatori devono essere inseriti come configurazione per Clerk.js, nello script di tracciamento che viene inserito su tutte le pagine.
Per Magento 1 questo file contiene il tracking-script:
app->design->frontend->base->default->template->clerk->tracking.phtml
Un esempio può essere visto qui sotto. Si prega di essere consapevoli che questo è per Clerk V2 (ultima versione):
<!-- Start of Clerk.io E-commerce Personalisation tool - www.clerk.io -->
<script type="text/javascript">
(function(w,d){
var e=d.createElement('script');e.type='text/javascript';e.async=true;
e.src=(d.location.protocol=='https:'?'https':'http')+'://cdn.clerk.io/clerk.js';
var s=d.getElementsByTagName('script')[0];s.parentNode.insertBefore(e,s);
w.__clerk_q=w.__clerk_q||[];w.Clerk=w.Clerk||function(){w.__clerk_q.push(arguments)};
})(window,document);
Clerk('config', {
key: 'O7UITkJIXqXibeQF9ONobiGGKYYKtbvh',
formatters: {
form_key: function () {
return '<?php echo $this->getFormKey(); ?>';
},
price_in_currency: function(price) {
price = Math.round(price * 100) / 100;
var price_converted = Math.round((price * <?php echo (float)$rates[$currentCurrencyCode]; ?>) * 100) / 100;
var retVal = <?php echo json_encode($currentCurrencySymbol); ?> + ' ' + price_converted.toFixed(2).toString();
return retVal;
},
log_price: function(price) {
console.log(price);
}
}
});
};
(function(){
</script>
<!-- End of Clerk.io E-commerce Personalisation tool - www.clerk.io -->
Puoi scrivere qualsiasi numero di Formatters, separati da una virgola:
formatters: {
log_price: function(price) {
console.log(price);
},
calculate_discount: function(price,special_price) {
return price-special_price;
},
substring: function(text) {
var short_string = text.substring(0,20);
return short_string;
}
}
Dopo aver creato i tuoi Formatters, puoi usarli nei tuoi Designs usando questa sintassi:
{{ x formatter | attribute }} {{ x | formatter | attribute1 | attribute2 }}
Questo ti permette di creare qualsiasi funzionalità di cui hai bisogno nei Designs.