Vue Storefront 2 Reference
- Install Magento 2 module as described in Magento Reference
- Insert the following Nuxt's
head()
method to a page where you want to display the integratio (recommended -CartPreview.vue
orPayment.vue
)
CartPreview.vue
head() {
return {
title: "Donmo Roundup", // Other meta information
script: [
{
hid: "donmo",
src: "https://static.donmo.org/integration.js",
defer: true,
},
],
};
}
-
Add
DONMO_PUBLIC_KEY
toprocess.env
-
Follow the next steps to initialize and build integration.
Get the required VSF variables
CartPreview.vueimport cartGetters from '~/modules/checkout/getters/cartGetters' import {onMounted, useContext, watch} from "@nuxtjs/composition-api"; setup { // ... const { cart, removeItem, updateItemQty, load } = useCart() const { mutate } = useApi() const { app: { i18n }, } = useContext() // ... }
Declare
addDonationAction
insidesetup()
CartPreview.vue// Add donation to the cart using GraphQl mutation from Donmo Magento module const addDonationAction = async ({ donationAmount }) => { const ADD_DONATION_MUTATION = ` mutation ADD_DONATION_MUTATION($donationAmount: Float!, $cartId: String!) { addDonationToQuote(donationAmount: $donationAmount, cartId: $cartId){ message } } ` await mutate(ADD_DONATION_MUTATION, { donationAmount, cartId: cart.value.id, }) // refresh cart load() }
Declare
removeDonationAction
insidesetup()
CartPreview.vue// Remove donation from the cart using GraphQl mutation from Donmo Magento module const removeDonationAction = async () => { const REMOVE_DONATION_MUTATION = ` mutation REMOVE_DONATION_MUTATION($cartId: String!) { removeDonationFromQuote(cartId: $cartId) { message } } ` await mutate(REMOVE_DONATION_MUTATION, { cartId: cart.value.id }) // refresh cart load() }
Declare
getExistingDonation
andgetGrandTotal
insidesetup()
CartPreview.vue// Get current donation from the cart const getExistingDonation = () => cart.value?.prices?.['donmo_donation']?.value // Get current cart grand total const getGrandTotal = () => cartGetters.getTotals(cart.value).total
Call
onMounted
with initalization scripts insidesetup()
CartPreview.vueonMounted(async () => { await load(); // All the details on possible parameters are given in the JavaScript integration docs const donmo = (window as any).DonmoRoundup({ publicKey: process.env.DONMO_PUBLIC_KEY, isBackendBased: true, language: i18n.locale, orderId: cart.value.id, addDonationAction, removeDonationAction, getExistingDonation, getGrandTotal, }); donmo.build(); watch( () => cartGetters.getTotals(cart.value).total, () => load().then(() => donmo.refresh()) ); });