-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from kotivuori/op-lasku-calculator
OP Lasku calculator
- Loading branch information
Showing
40 changed files
with
9,149 additions
and
210 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.