Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OP Lasku calculator #201

Merged
merged 12 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions assets/img/icon_oplasku_admin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions assets/op-lasku-assets/OP_logo.e0c695c0.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions assets/op-lasku-assets/OP_tunnus.24771aea.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions assets/op-lasku-assets/op-lasku-helper-blocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { useEffect } from "react";
import { select, subscribe } from "@wordpress/data";
import { CART_STORE_KEY } from "@woocommerce/block-data";
import { registerPlugin } from "@wordpress/plugins";
import { ExperimentalOrderMeta } from "@woocommerce/blocks-checkout";

const render = () => {
if (typeof ExperimentalOrderMeta === "undefined" || typeof window.op_lasku_data === "undefined") {
return null;
}

// Initialize calculator options if not available
if (!window.__opLaskuOpts) {
window.__opLaskuOpts = [];
}

// Subscribe to cart changes
const onCartChange = () => {
const cart = select(CART_STORE_KEY).getCartData();
if (!cart) {
return;
}

const cartTotal = cart.totals.total_price || 0;

if (window.__opLaskuCalcWidget?.lasku) {
window.__opLaskuCalcWidget.lasku.setAmount(cartTotal);
}

window.__opLaskuOpts = {
amount: cartTotal,
lang: window.op_lasku_data.language || "fi",
type: "lasku",
};
};

useEffect(() => {
const unsubscribe = subscribe(onCartChange);

return () => {
unsubscribe();
};
}, []);

return (
<ExperimentalOrderMeta>
<div className="wc-block-components-totals-wrapper">
<div id="op-lasku--init"></div>
</div>
</ExperimentalOrderMeta>
);
};

registerPlugin("paytrail-oplasku-cart-blocks", {
render,
scope: "woocommerce-checkout",
});
46 changes: 46 additions & 0 deletions assets/op-lasku-assets/op-lasku-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
if (!window.__opLaskuOpts) {
window.__opLaskuOpts = [];
}

if (typeof window.op_lasku_data !== 'undefined') {
//Get the language
const lang = op_lasku_data.language || "fi";

// Initialize options based on page type
if (op_lasku_data?.product_price) {
// Product page
window.__opLaskuOpts = {
amount: Math.round(op_lasku_data.product_price * 100),
lang: lang,
type: "lasku",
};
} else if (op_lasku_data?.cart_total) {
// Cart page
window.__opLaskuOpts = {
amount: Math.round(op_lasku_data.cart_total * 100),
lang: lang,
type: "lasku",
};

// Attach cart update listener on shortcode cart page
jQuery(document.body).on("updated_cart_totals", function () {
const totalElement = document.querySelector(".order-total .amount");
if (!totalElement) return;

const cartTotal = totalElement.textContent
.replace(/[^0-9.,]/g, "")
.replace(",", ".");

window.__opLaskuOpts = {
amount: Math.round(parseFloat(cartTotal) * 100),
lang: lang,
type: "lasku",
};

// Call the OP Lasku initialization function
if (typeof window.opLaskuInit === "function") {
window.opLaskuInit();
}
});
}
}
Loading