Appearance
Manage Discounts
Use getDiscount() to retrieve discount details and validateDiscount() to verify codes at checkout.
Get Discount
Retrieve the affiliate's discount code and details:
html
<script>
AffiliateSDK.getDiscount().then((discount) => {
if (discount) {
console.log("Code:", discount.code);
console.log("Type:", discount.type); // "percentage" or "flat"
console.log("Value:", discount.value);
// Display discount banner
document.getElementById("discount-banner").innerHTML =
`Use code <strong>${discount.code}</strong> for ${discount.value}% off!`;
}
});
</script>php
<script>
AffiliateSDK.getDiscount().then(discount => {
if (discount) {
// Auto-apply discount to cart
jQuery("#coupon_code").val(discount.code);
// Show message
alert(`Use code ${discount.code} for ${discount.value}% off!`);
}
});
</script>liquid
<script>
AffiliateSDK.getDiscount().then(discount => {
if (discount) {
// Display discount
document.querySelector('.discount-code').textContent = discount.code;
document.querySelector('.discount-value').textContent =
`${discount.value}${discount.type === 'percentage' ? '%' : '$'} off`;
}
});
</script>jsx
import { useEffect, useState } from "react";
export default function DiscountBanner() {
const [discount, setDiscount] = useState(null);
useEffect(() => {
if (window.AffiliateSDK) {
window.AffiliateSDK.getDiscount().then((d) => setDiscount(d));
}
}, []);
if (!discount) return null;
return (
<div>
Use code <strong>{discount.code}</strong> for {discount.value}
{discount.type === "percentage" ? "%" : "$"} off!
</div>
);
}vue
<script setup lang="ts">
const discount = ref(null);
onMounted(async () => {
if (window.AffiliateSDK) {
discount.value = await window.AffiliateSDK.getDiscount();
}
});
</script>
<template>
<div v-if="discount">
Use code <strong>{{ discount.code }}</strong> for {{ discount.value
}}{{ discount.type === "percentage" ? "%" : "$" }} off!
</div>
</template>Validate Discount
Verify a discount code at checkout:
html
<script>
const code = "SAVE10";
const cartTotal = 100.0;
AffiliateSDK.validateDiscount(code, cartTotal).then((result) => {
if (result.isValid) {
console.log("Discount amount:", result.discountAmount);
// Update cart total
const newTotal = cartTotal - result.discountAmount;
document.getElementById("total").innerText = `$${newTotal.toFixed(2)}`;
} else {
alert("Invalid discount code");
}
});
</script>php
<script>
function applyDiscount(code) {
const cartTotal = <?php echo WC()->cart->get_total(''); ?>;
AffiliateSDK.validateDiscount(code, cartTotal).then(result => {
if (result.isValid) {
// Apply discount via WooCommerce
jQuery.post('/wp-admin/admin-ajax.php', {
action: 'apply_coupon',
coupon_code: code
});
} else {
alert("Invalid discount code");
}
});
}
</script>liquid
<script>
function validateCode(code) {
const cartTotal = {{ cart.total_price | money_without_currency }};
AffiliateSDK.validateDiscount(code, cartTotal).then(result => {
if (result.isValid) {
// Redirect to checkout with discount
window.location.href = `/checkout?discount=${code}`;
} else {
alert("Invalid discount code");
}
});
}
</script>jsx
import { useState } from "react";
export default function Checkout({ cartTotal }) {
const [discount, setDiscount] = useState(0);
const applyDiscount = async (code) => {
const result = await window.AffiliateSDK.validateDiscount(code, cartTotal);
if (result.isValid) {
setDiscount(result.discountAmount);
} else {
alert("Invalid discount code");
}
};
return (
<div>
<p>Total: ${(cartTotal - discount).toFixed(2)}</p>
</div>
);
}vue
<script setup lang="ts">
const cartTotal = ref(100.0);
const discountAmount = ref(0);
const applyDiscount = async (code: string) => {
const result = await window.AffiliateSDK.validateDiscount(
code,
cartTotal.value,
);
if (result.isValid) {
discountAmount.value = result.discountAmount;
} else {
alert("Invalid discount code");
}
};
</script>
<template>
<div>
<p>Total: ${{ (cartTotal - discountAmount).toFixed(2) }}</p>
</div>
</template>