Skip to content

Commit

Permalink
Add view files
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjaap committed Aug 27, 2024
1 parent de71386 commit e06a2f9
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
27 changes: 27 additions & 0 deletions view/frontend/layout/hyva_checkout_components.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="head.additional">
<block name="paazl-js"
template="Elgentos_HyvaCheckoutPaazl::paazl-scripts.phtml" after="-"/>
</referenceBlock>

<referenceBlock name="checkout.shipping.methods">
<arguments>
<argument name="magewire" xsi:type="object">
Elgentos\HyvaCheckoutPaazl\Magewire\Shipping\MethodList\MethodListExtend
</argument>
</arguments>
<block name="checkout.shipping.method.paazl" template="Elgentos_HyvaCheckoutPaazl::paazl.phtml"
as="paazlshipping_paazlshipping">
<arguments>
<argument name="magewire" xsi:type="object">
Elgentos\HyvaCheckoutPaazl\Magewire\Shipping\Method\Paazl
</argument>
</arguments>
</block>
</referenceBlock>

</body>
</page>
74 changes: 74 additions & 0 deletions view/frontend/templates/paazl-scripts.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/*
* Copyright © elgentos. All rights reserved.
* https://elgentos.nl
*/

use Elgentos\HyvaCheckoutPaazl\ViewModel\Shipping\Method\Paazl as PaazlViewModel;
use Hyva\Theme\Model\ViewModelRegistry;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;

/** @var Escaper $escaper */
/** @var ViewModelRegistry $viewModels */
/** @var Template $block */

$paazlViewModel = $viewModels->require(PaazlViewModel::class);
$paazlConfig = $paazlViewModel->getPaazlConfig();

?>


<?php if ($paazlViewModel->getPaazlMode() === 'live') : ?>
<script preload src="<?= $escaper->escapeUrl($block->getViewFileUrl('Paazl_CheckoutWidget::js/checkout/lib/checkout_local_live.js')); ?>"></script>
<?php else : ?>
<script preload src="<?= $escaper->escapeUrl($block->getViewFileUrl('Paazl_CheckoutWidget::js/checkout/lib/checkout_local_test.js')); ?>"></script>
<?php endif; ?>


<script>
window.addEventListener('postcode:updated', (event) => {
if (event.detail.postcode === '') {
return;
}

PaazlCheckout.setConsigneeCountryCode(event.detail.country);
PaazlCheckout.setConsigneePostalCode(event.detail.postcode);
});

function isCheckoutUrl(url) {
const checkoutApiUrl = '<?= $escaper->escapeUrl($paazlConfig['paazlshipping']['checkoutApiUrl']) ?>';
return checkoutApiUrl && (checkoutApiUrl.indexOf(
url) === 0);
}

function isLocationUrl(url) {
let locationsUrl = '<?= $escaper->escapeUrl($paazlConfig['paazlshipping']['baseApiUrl']) ?>';
locationsUrl += 'pickuplocations';
return (locationsUrl.indexOf(url) === 0);
}

let openOrig = window.XMLHttpRequest.prototype.open;
let sendOrig = window.XMLHttpRequest.prototype.send;

window.XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
return openOrig.apply(this, arguments);
};

window.XMLHttpRequest.prototype.send = function (body) {
this.removeEventListener('load', onLocationSelect);
this.addEventListener('load', onLocationSelect.bind(this, body));
return sendOrig.apply(this, arguments);
};

function onLocationSelect(body, event) {
var ready =
(this.readyState === 4)
&& event.target
&& this.responseURL
&& (event.target.status === 200);
if (ready && isCheckoutUrl(this.responseURL)) {
Magewire.emit('paazle_updated', body?.shippingOption?.identifier);
}
}
</script>
29 changes: 29 additions & 0 deletions view/frontend/templates/paazl.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/*
* Copyright © elgentos. All rights reserved.
* https://elgentos.nl
*/

use Elgentos\HyvaCheckoutPaazl\ViewModel\Shipping\Method\Paazl as PaazlViewModel;
use Hyva\Theme\Model\ViewModelRegistry;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;

/** @var Escaper $escaper */
/** @var ViewModelRegistry $viewModels */
/** @var Template $block */

$paazlViewModel = $viewModels->require(PaazlViewModel::class);

$widgetConfig = $paazlViewModel->getWidgetConfig();
$paazlConfig = $paazlViewModel->getPaazlConfig();

?>

<div>
<script>
PaazlCheckout.init(<?= /** @noEscape */ json_encode($widgetConfig); ?>);
</script>

<div id="widget_paazlshipping_paazlshipping"></div>
</div>

0 comments on commit e06a2f9

Please sign in to comment.