Javascript toevoegen in Magento (Checkout) met PHP variabelen
Om PHP variabelen te gebruiken in JS in de Magento Checkout (of elders in je shop), kun je het beste zo de code organiseren.
PHP Code
php
Plaats dit in je .phtml file die je via XML inlaadt in de checkout
om de php variablen op te halen -> dit is de JS file waar het gebruikt word -> Gladior_CurrencySymbol/js/gladior-currency
/**
* Gladior Currency Symbol Config
* Outputs configuration as JavaScript variable
*
* @var $block \Gladior\CurrencySymbol\Block\JsConfig
*/
?>
<?php if ($block->isEnabled() && $block->getCustomSymbol()): ?>
<script type="text/x-magento-init">
{
"*": {
"Gladior_CurrencySymbol/js/gladior-currency": {
"enabled": true,
"currencyCode": "<?php echo $block->escapeJs($block->getCurrencyCode()); ?>",
"defaultSymbol": "<?php echo $block->escapeJs($block->getDefaultSymbol()); ?>",
"customSymbol": "<?php echo $block->escapeJs($block->getCustomSymbol()); ?>"
}
}
}
</script>
<?php endif; ?>
JAVASCRIPT Code
javascript
define(['jquery'], function ($) {
'use strict';
return function (config) {
if (!config || !config.enabled || !config.customSymbol) return;config
console.log(config.enabled);
}
}