feat(telecart): add vouchers and coupons (#9)

This commit is contained in:
2025-10-20 20:00:31 +03:00
parent 78ca4fd309
commit ac24f0376b
10 changed files with 149 additions and 14 deletions

View File

@@ -1,6 +1,6 @@
import {defineStore} from "pinia";
import {isNotEmpty} from "@/helpers.js";
import {addToCart, cartEditItem, cartRemoveItem, getCart} from "@/utils/ftch.js";
import {addToCart, cartEditItem, cartRemoveItem, getCart, setCoupon, setVoucher} from "@/utils/ftch.js";
export const useCartStore = defineStore('cart', {
state: () => ({
@@ -12,6 +12,8 @@ export const useCartStore = defineStore('cart', {
error_warning: '',
attention: '',
success: '',
coupon: '',
voucher: '',
}),
getters: {
@@ -104,5 +106,43 @@ export const useCartStore = defineStore('cart', {
this.isLoading = false;
}
},
async applyCoupon() {
try {
this.isLoading = true;
this.error_warning = '';
const response = await setCoupon(this.coupon);
if (response.error) {
this.error_warning = response.error;
} else {
await this.getProducts();
}
} catch (error) {
console.log(error);
this.error_warning = 'Возникла ошибка';
} finally {
this.isLoading = false;
}
},
async applyVoucher() {
try {
this.isLoading = true;
this.error_warning = '';
const response = await setVoucher(this.voucher);
if (response.error) {
this.error_warning = response.error;
} else {
await this.getProducts();
}
} catch (error) {
console.log(error);
this.error_warning = 'Возникла ошибка';
} finally {
this.isLoading = false;
}
},
},
});

View File

@@ -14,10 +14,10 @@ export const useSettingsStore = defineStore('settings', {
manifest_url: null,
night_auto: true,
ya_metrika_enabled: false,
feature_coupons: false,
feature_vouchers: false,
theme: {
light: 'light',
dark: 'dark',
variables: {
light: 'light', dark: 'dark', variables: {
'--product_list_title_max_lines': 2,
}
},
@@ -40,6 +40,8 @@ export const useSettingsStore = defineStore('settings', {
this.ya_metrika_enabled = settings.ya_metrika_enabled;
this.app_enabled = settings.app_enabled;
this.store_enabled = settings.store_enabled;
this.feature_coupons = settings.feature_coupons;
this.feature_vouchers = settings.feature_vouchers;
}
}
});

View File

@@ -72,4 +72,24 @@ export async function getFiltersForMainPage() {
return await ftch('filtersForMainPage');
}
export async function setCoupon(coupon) {
const formData = new FormData();
formData.append('coupon', coupon);
return await apiFetch(`${BASE_URL}index.php?route=extension/total/coupon/coupon`, {
method: 'POST',
body: formData,
});
}
export async function setVoucher(voucher) {
const formData = new FormData();
formData.append('voucher', voucher);
return await apiFetch(`${BASE_URL}index.php?route=extension/total/voucher/voucher`, {
method: 'POST',
body: formData,
});
}
export default ftch;

View File

@@ -71,7 +71,7 @@
</div>
</div>
<div class="card card-border bg-base-100">
<div class="card card-border bg-base-100 mb-3">
<div class="card-body">
<h2 class="card-title">Ваша корзина</h2>
<div v-for="total in cart.totals">
@@ -84,6 +84,31 @@
</div>
</div>
<div
v-if="settings.feature_coupons || settings.feature_vouchers"
class="card card-border bg-base-100 mb-3"
>
<div class="card-body">
<div v-if="settings.feature_coupons" class="join">
<input v-model="cart.coupon" type="text" class="input" placeholder="Промокод"/>
<button
class="btn"
:disabled="!cart.coupon"
@click="cart.applyCoupon"
>Применить</button>
</div>
<div v-if="settings.feature_vouchers" class="join">
<input v-model="cart.voucher" type="text" class="input" placeholder="Подарочный сертификат"/>
<button
class="btn"
:disabled="!cart.voucher"
@click="cart.applyVoucher"
>Применить</button>
</div>
</div>
</div>
<div class="fixed px-4 pb-10 pt-4 bottom-0 left-0 w-full bg-base-200 z-50 flex justify-between items-center gap-2 border-t-1 border-t-base-300">
<div>
<div v-if="lastTotal">
@@ -122,9 +147,11 @@ import OptionCheckbox from "@/components/ProductOptions/Cart/OptionCheckbox.vue"
import OptionText from "@/components/ProductOptions/Cart/OptionText.vue";
import {computed} from "vue";
import {useRouter} from "vue-router";
import {useSettingsStore} from "@/stores/SettingsStore.js";
const cart = useCartStore();
const router = useRouter();
const settings = useSettingsStore();
// const componentMap = {
// radio: OptionRadio,