Deprecated.
This function will continue to work, but a newer version is available at
wix-ecom-backend.currentCart.updateCurrentCart()
.
If this function is already in your code, it will continue to work.
To stay compatible with future changes, migrate to
wix-ecom-backend.currentCart.updateCurrentCart()
.
To migrate to the new function:
Add the new import statement:
import { currentCart } from "wix-ecom-backend";
Look for any code that uses cart.applyCoupon()
,
and replace it with currentCart.updateCurrentCart()
.
Update your code to work with the new currentCart.updateCurrentCart()
call and response properties.
Test your changes to make sure your code behaves as expected.
Adds and applies a coupon to the cart.
The applyCoupon()
function returns a Promise that resolves to the current
site visitor's cart when the specified coupon has been applied.
The cart can only hold one coupon. If a coupon is already applied to the cart, passing a different couponCode
to applyCoupon()
will replace the existing one.
Note: When editing a site as a contributor, applyCoupon()
will only work when viewing the live site.
function applyCoupon(couponCode: string): Promise<CartObj>;
The code of the coupon to be applied to the cart.
This example uses a deprecated function.
import { cart } from "wix-stores-frontend";
const couponCode = "SummerSale";
cart
.applyCoupon(couponCode)
.then((updatedCart) => {
const couponDiscount = updatedCart.appliedCoupon.discountValue;
})
.catch((error) => {
console.error(error);
});
/* Example of returned updatedCart object:
*
* {
* "_id": "b36eb035-635a-450e-b74d-acf86ee4dfcc",
* "appliedCoupon": {
* "couponId": "e81e9c48-f954-4044-ba64-ccfe5c103c8f",
* "name": "Summer Sale",
* "code": "SummerSale",
* "discountValue": "$10.00",
* "couponType": "MoneyOff"
* },
* "billingAddress": {
* "firstName": "John",
* "lastName": "Doe",
* "email":"john.doe@somedomain.com",
* "phone":"5555555",
* "address":"235 West 23rd Street\nNew York, New York 10011\nUnited States"
* },
* "buyerNote": "This is a note from the buyer.",
* "buyerInfo":{
* "firstName": "John",
* "lastName": "Doe",
* "email": "john.doe@somedomain.com",
* "phone": "5555555555",
* "identityType": "CONTACT"
* },
* "status": "INCOMPLETE",
* "currency": {
* "code": "USD",
* "symbol": "$"
* },
* "shippingInfo": {
* "deliveryOption": "Free Shipping",
* "shippingAddress": {
* "firstName": "John",
* "lastName": "Doe",
* "email":"john.doe@somedomain.com",
* "phone":"5555555",
* "address":"235 West 23rd Street\nNew York, New York 10011\nUnited States"
* },
* "pickupDetails":null
* },
* "lineItems":[
* {
* "quantity": 1,
* "price": 120,
* "name": "A product",
* "productId": "a668ef33-f5b8-6569-d04c-1d123be68441",
* "totalPrice": 120,
* "lineItemType": "PHYSICAL",
* "customTextFields": [
* "title": "Custom Field",
* "value": "Custom value"
* ],
* "mediaItem": {
* "src": "wix:image://v1/a9ff3b_ed3b544c319b4fad9c222c791a997832.jpg/file.jpg#originWidth=1000&originHeight=1000",
* "type": "IMAGE"
* },
* "sku": "21554345656",
* "options": [ ],
* "weight": 3,
* "id": 1
* },
* {
* "quantity": 1,
* "price": 25,
* "name": "Another product",
* "productId": "1a2d7e83-4bef-31d5-09e1-3326ee271c09",
* "totalPrice": 25,
* "lineItemType": "PHYSICAL",
* "mediaItem": {
* "src": "wix:image://v1/a9ff3b_c6158b4d41784ae8b08337a331e1de7f.jpg/file.jpg#originWidth=1000&originHeight=1000",
* "type": "IMAGE"
* },
* "sku": "217537123517253",
* "options": [
* {
* "option": "Size",
* "selection": "Medium"
* },
* {
* "option": "Color",
* "selection": "Black"
* }
* ],
* "weight": 2,
* "id": 2
* }
* ],
* "totals": {
* "discount": 10,
* "quantity": 2,
* "shipping": 0,
* "subtotal": 145,
* "tax": 0,
* "total": 135,
* "weight": 5
* },
* "weightUnit": "LB"
* }
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.