Skip to content

Commit

Permalink
Merge pull request #201 from kotivuori/op-lasku-calculator
Browse files Browse the repository at this point in the history
OP Lasku calculator
  • Loading branch information
kotivuori authored Feb 10, 2025
2 parents de8c9c1 + a5fdd0d commit 60c5e96
Show file tree
Hide file tree
Showing 40 changed files with 9,149 additions and 210 deletions.
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

0 comments on commit 60c5e96

Please sign in to comment.