A custom cart functionality with Add / Update Items and Add Offers.
yarn add @waakif/waakif-cart or npm i @waakif/waakif-cart
yarn add sonner
After installing package you have to initialze
this package and feed some data to create / update
cart.
import {useCart} from 'waakif-cart'
const {updateCart, cartData, upsertOffer, removeOffer} = useCart({
products: [],
offers: [],
org: {_id: '123', name: 'Dummy Org'}
})
cartData: Retruns from useCart
with following object
{
"items": [
{
"appliedDiscountAmount": 10,
"discountApplied": false,
"eachPrice": 99,
"product": "123",
"quantity": 1,
"totalAmount": 99,
"notes": "Freebie / Bogo",
"totalAmountWithoutDiscount": 99,
"name": "Pizza",
"productId": "Product Object"
},
],
"gstAmount": 18,
"gstPercentage": 18,
"totalAmount": 300,
"totalAmountWithGst": 354,
"discount": 0,
"appliedOfferId": "Offer Object",
"loyaltyPointsUsed": 0,
"gstDetails": {
"CGSTAmount": 9,
"SGSTAmount": 9
}
}
Method | Parameter | Return |
---|---|---|
useCart | {products: List of Products, offers: List of Offers, org: Org Details} |
This hook will return 3 things: 1.updateCart 2.cartData 3.upsertOffer |
updateCart | {product: id of product, quantity: 3} |
This will do all the calculation of product and make new cart data |
upsertOffer | offerId | This function will check offer requirements and if all things passes for offer then it will applied to cart. |
removeOffer | null | This will remove the applicable offer from the cartData
|
Steps for build and publish package
1. yarn build or npm run build
2. npm publish (before publishing must have to login with `npm` credential)
First do below steps in cart package file
yarn build
npm link
Then go to your frontend repo and remove waakif-cart package if installed and run
npm link @waakif/waakif-cart --legacy-peer-deps
After all the testing unlink package from repo and publish the package
npm unlink @waakif/waakif-cart