wip: shopping cart, product options
This commit is contained in:
@@ -5,39 +5,28 @@ namespace App\Handlers;
|
|||||||
use Cart\Cart;
|
use Cart\Cart;
|
||||||
use Openguru\OpenCartFramework\Http\JsonResponse;
|
use Openguru\OpenCartFramework\Http\JsonResponse;
|
||||||
use Openguru\OpenCartFramework\Http\Request;
|
use Openguru\OpenCartFramework\Http\Request;
|
||||||
use Openguru\OpenCartFramework\QueryBuilder\Connections\ConnectionInterface;
|
use Openguru\OpenCartFramework\ImageTool\ImageToolInterface;
|
||||||
|
|
||||||
class CheckoutHandler
|
class CartHandler
|
||||||
{
|
{
|
||||||
private Cart $cart;
|
private Cart $cart;
|
||||||
private \DB $connection;
|
private ImageToolInterface $imageTool;
|
||||||
|
|
||||||
public function __construct(Cart $cart, \DB $database)
|
public function __construct(Cart $cart, \DB $database, ImageToolInterface $imageTool)
|
||||||
{
|
{
|
||||||
$this->cart = $cart;
|
$this->cart = $cart;
|
||||||
$this->database = $database;
|
$this->database = $database;
|
||||||
|
$this->imageTool = $imageTool;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addToCart(Request $request): JsonResponse
|
public function index(Request $request): JsonResponse
|
||||||
{
|
{
|
||||||
$item = $request->json();
|
|
||||||
|
|
||||||
$options = [];
|
|
||||||
|
|
||||||
foreach ($item['options'] as $option) {
|
|
||||||
if (! empty($option['value']) && ! empty($option['value']['product_option_value_id'])) {
|
|
||||||
$options[$option['product_option_id']] = $option['value']['product_option_value_id'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->cart->add(
|
|
||||||
$item['productId'],
|
|
||||||
$item['quantity'],
|
|
||||||
$options,
|
|
||||||
);
|
|
||||||
|
|
||||||
return new JsonResponse([
|
return new JsonResponse([
|
||||||
'data' => $this->cart->getProducts(),
|
'data' => [
|
||||||
|
'products' => $this->getProducts(),
|
||||||
|
'count' => $this->cart->countProducts(),
|
||||||
|
'total' => $this->cart->getTotal(),
|
||||||
|
]
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,4 +54,15 @@ class CheckoutHandler
|
|||||||
'data' => $items,
|
'data' => $items,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getProducts(): array
|
||||||
|
{
|
||||||
|
$products = $this->cart->getProducts();
|
||||||
|
|
||||||
|
foreach ($products as &$product) {
|
||||||
|
$product['thumb'] = $this->imageTool->resize($product['image'], 100, 100, 'placeholder.png');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $products;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Handlers\CategoriesHandler;
|
use App\Handlers\CategoriesHandler;
|
||||||
use App\Handlers\CheckoutHandler;
|
use App\Handlers\CartHandler;
|
||||||
use App\Handlers\HelloWorldHandler;
|
use App\Handlers\HelloWorldHandler;
|
||||||
use App\Handlers\OrderCreateHandler;
|
use App\Handlers\OrderCreateHandler;
|
||||||
use App\Handlers\ProductsHandler;
|
use App\Handlers\ProductsHandler;
|
||||||
@@ -13,6 +13,6 @@ return [
|
|||||||
|
|
||||||
'categoriesList' => [CategoriesHandler::class, 'index'],
|
'categoriesList' => [CategoriesHandler::class, 'index'],
|
||||||
|
|
||||||
'checkout' => [CheckoutHandler::class, 'checkout'],
|
'checkout' => [CartHandler::class, 'checkout'],
|
||||||
'addToCart' => [CheckoutHandler::class, 'addToCart'],
|
'cart' => [CartHandler::class, 'index'],
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
<div class="app-container">
|
<div class="app-container">
|
||||||
<FullscreenViewport v-if="platform === 'ios' || platform === 'android'"/>
|
<FullscreenViewport v-if="platform === 'ios' || platform === 'android'"/>
|
||||||
<router-view/>
|
<router-view/>
|
||||||
|
<CartButton/>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -10,6 +11,7 @@ import {onMounted, ref, watch} from "vue";
|
|||||||
import {useWebAppViewport, useBackButton} from 'vue-tg';
|
import {useWebAppViewport, useBackButton} from 'vue-tg';
|
||||||
import {useMiniApp, FullscreenViewport} from 'vue-tg';
|
import {useMiniApp, FullscreenViewport} from 'vue-tg';
|
||||||
import {useRoute, useRouter} from "vue-router";
|
import {useRoute, useRouter} from "vue-router";
|
||||||
|
import CartButton from "@/components/CartButton.vue";
|
||||||
|
|
||||||
const tg = useMiniApp();
|
const tg = useMiniApp();
|
||||||
const platform = ref();
|
const platform = ref();
|
||||||
|
|||||||
40
spa/src/components/CartButton.vue
Normal file
40
spa/src/components/CartButton.vue
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<template>
|
||||||
|
<div v-if="route.name !== 'cart.show'" class="fixed right-2 bottom-30 z-50 opacity-90">
|
||||||
|
<div class="indicator">
|
||||||
|
<span class="indicator-item indicator-top indicator-start badge badge-secondary">{{ cart.productsCount }}</span>
|
||||||
|
<button class="btn btn-primary btn-lg btn-circle" @click="openCart">
|
||||||
|
<span v-if="cart.isLoading" class="loading loading-spinner"></span>
|
||||||
|
<template v-else>
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
|
||||||
|
<path stroke-linecap="round" stroke-linejoin="round" d="M2.25 3h1.386c.51 0 .955.343 1.087.835l.383 1.437M7.5 14.25a3 3 0 0 0-3 3h15.75m-12.75-3h11.218c1.121-2.3 2.1-4.684 2.924-7.138a60.114 60.114 0 0 0-16.536-1.84M7.5 14.25 5.106 5.272M6 20.25a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0Zm12.75 0a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0Z" />
|
||||||
|
</svg>
|
||||||
|
</template>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import {onMounted} from "vue";
|
||||||
|
import {useCartStore} from "@/stores/CartStore.js";
|
||||||
|
import {useRoute, useRouter} from "vue-router";
|
||||||
|
|
||||||
|
const cart = useCartStore();
|
||||||
|
const router = useRouter();
|
||||||
|
const route = useRoute();
|
||||||
|
|
||||||
|
function openCart() {
|
||||||
|
window.Telegram.WebApp.HapticFeedback.selectionChanged();
|
||||||
|
router.push({name: 'cart.show'});
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
await cart.getProducts();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
|
||||||
|
</style>
|
||||||
13
spa/src/components/Price.vue
Normal file
13
spa/src/components/Price.vue
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<template>
|
||||||
|
<span>{{ formatPrice(value) }} ₽</span>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import {formatPrice} from "@/helpers.js";
|
||||||
|
|
||||||
|
const props = defineProps({
|
||||||
|
value: {
|
||||||
|
default: 0,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
</script>
|
||||||
18
spa/src/components/ProductOptions/Cart/OptionCheckbox.vue
Normal file
18
spa/src/components/ProductOptions/Cart/OptionCheckbox.vue
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<template>
|
||||||
|
<p>
|
||||||
|
<span class="text-xs font-medium">
|
||||||
|
{{ option.name }}: {{ option.value }} <span v-if="option.price"> ({{ option.price_prefix }}<Price :value="option.price"/>)</span>
|
||||||
|
</span>
|
||||||
|
</p>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import Price from "@/components/Price.vue";
|
||||||
|
|
||||||
|
const props = defineProps({
|
||||||
|
option: {
|
||||||
|
type: Object,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
</script>
|
||||||
18
spa/src/components/ProductOptions/Cart/OptionRadio.vue
Normal file
18
spa/src/components/ProductOptions/Cart/OptionRadio.vue
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<template>
|
||||||
|
<p>
|
||||||
|
<span class="text-xs font-medium">
|
||||||
|
{{ option.name }}: {{ option.value }} <span v-if="option.price"> ({{ option.price_prefix }}<Price :value="option.price"/>)</span>
|
||||||
|
</span>
|
||||||
|
</p>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import Price from "@/components/Price.vue";
|
||||||
|
|
||||||
|
const props = defineProps({
|
||||||
|
option: {
|
||||||
|
type: Object,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
</script>
|
||||||
18
spa/src/components/ProductOptions/Cart/OptionText.vue
Normal file
18
spa/src/components/ProductOptions/Cart/OptionText.vue
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<template>
|
||||||
|
<p>
|
||||||
|
<span class="text-xs font-medium">
|
||||||
|
{{ option.name }}: {{ option.value }} <span v-if="option.price"> ({{ option.price_prefix }}<Price :value="option.price"/>)</span>
|
||||||
|
</span>
|
||||||
|
</p>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import Price from "@/components/Price.vue";
|
||||||
|
|
||||||
|
const props = defineProps({
|
||||||
|
option: {
|
||||||
|
type: Object,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
</script>
|
||||||
@@ -1,10 +1,13 @@
|
|||||||
<template>
|
<template>
|
||||||
<div v-for="option in options" :key="option.product_option_id" class="mt-3">
|
<div v-for="option in options" :key="option.product_option_id" class="mt-3">
|
||||||
<OptionRadio v-if="option.type === 'radio'" :modelValue="option"/>
|
<component
|
||||||
<OptionCheckbox v-else-if="option.type === 'checkbox'" :modelValue="option"/>
|
v-if="SUPPORTED_OPTION_TYPES.includes(option.type) && componentMap[option.type]"
|
||||||
<OptionText v-else-if="option.type === 'text'" :modelValue="option"/>
|
:is="componentMap[option.type]"
|
||||||
<OptionTextarea v-else-if="option.type === 'textarea'" :modelValue="option"/>
|
:modelValue="option"
|
||||||
<OptionSelect v-else-if="option.type === 'select'" :modelValue="option"/>
|
/>
|
||||||
|
<div v-else class="text-sm text-error">
|
||||||
|
Тип опции "{{ option.type }}" не поддерживается.
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -14,6 +17,15 @@ import OptionCheckbox from "./Types/OptionCheckbox.vue";
|
|||||||
import OptionText from "./Types/OptionText.vue";
|
import OptionText from "./Types/OptionText.vue";
|
||||||
import OptionTextarea from "./Types/OptionTextarea.vue";
|
import OptionTextarea from "./Types/OptionTextarea.vue";
|
||||||
import OptionSelect from "./Types/OptionSelect.vue";
|
import OptionSelect from "./Types/OptionSelect.vue";
|
||||||
|
import {SUPPORTED_OPTION_TYPES} from "@/constants/options.js";
|
||||||
|
|
||||||
|
const componentMap = {
|
||||||
|
radio: OptionRadio,
|
||||||
|
checkbox: OptionCheckbox,
|
||||||
|
text: OptionText,
|
||||||
|
textarea: OptionTextarea,
|
||||||
|
select: OptionSelect,
|
||||||
|
};
|
||||||
|
|
||||||
const options = defineModel();
|
const options = defineModel();
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
class="select"
|
class="select"
|
||||||
@change="onChange"
|
@change="onChange"
|
||||||
>
|
>
|
||||||
<option value="" disabled>Выберите значение</option>
|
<option value="" disabled selected>Выберите значение</option>
|
||||||
<option
|
<option
|
||||||
v-for="value in model.values"
|
v-for="value in model.values"
|
||||||
:key="value.product_option_value_id"
|
:key="value.product_option_value_id"
|
||||||
|
|||||||
@@ -34,7 +34,6 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import {ref} from "vue";
|
import {ref} from "vue";
|
||||||
import {useHapticFeedback} from 'vue-tg';
|
import {useHapticFeedback} from 'vue-tg';
|
||||||
import { useRouter, useRoute } from 'vue-router';
|
|
||||||
import NoProducts from "../components/NoProducts.vue";
|
import NoProducts from "../components/NoProducts.vue";
|
||||||
import ProductImageSwiper from "../components/ProductImageSwiper.vue";
|
import ProductImageSwiper from "../components/ProductImageSwiper.vue";
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="flex items-center text-center">
|
<div class="flex items-center text-center">
|
||||||
<button class="btn" :class="btnClassList" @click="inc">-</button>
|
<button class="btn" :class="btnClassList" @click="dec" :disabled="disabled">-</button>
|
||||||
<div class="w-10 h-10 flex items-center justify-center font-bold">{{ model }}</div>
|
<div class="w-10 h-10 flex items-center justify-center font-bold">{{ model }}</div>
|
||||||
<button class="btn" :class="btnClassList" @click="dec">+</button>
|
<button class="btn" :class="btnClassList" @click="inc" :disabled="disabled">+</button>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -15,6 +15,10 @@ const props = defineProps({
|
|||||||
size: {
|
size: {
|
||||||
type: String,
|
type: String,
|
||||||
default: '',
|
default: '',
|
||||||
|
},
|
||||||
|
disabled: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -27,12 +31,8 @@ const btnClassList = computed(() => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
function inc() {
|
function inc() {
|
||||||
if (model.value - 1 >= 0) {
|
if (props.disabled) return;
|
||||||
model.value--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function dec() {
|
|
||||||
if (props.max && model.value + 1 > props.max) {
|
if (props.max && model.value + 1 > props.max) {
|
||||||
model.value = props.max;
|
model.value = props.max;
|
||||||
return;
|
return;
|
||||||
@@ -40,4 +40,12 @@ function dec() {
|
|||||||
|
|
||||||
model.value++;
|
model.value++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function dec() {
|
||||||
|
if (props.disabled) return;
|
||||||
|
|
||||||
|
if (model.value - 1 >= 1) {
|
||||||
|
model.value--;
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
7
spa/src/constants/options.js
Normal file
7
spa/src/constants/options.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export const SUPPORTED_OPTION_TYPES = [
|
||||||
|
'checkbox',
|
||||||
|
'radio',
|
||||||
|
'select',
|
||||||
|
'text',
|
||||||
|
'textarea',
|
||||||
|
];
|
||||||
30
spa/src/helpers.js
Normal file
30
spa/src/helpers.js
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
export function isNotEmpty(value) {
|
||||||
|
if (value === null || value === undefined) return false;
|
||||||
|
|
||||||
|
if (Array.isArray(value)) return value.length > 0;
|
||||||
|
|
||||||
|
if (typeof value === 'object') return Object.keys(value).length > 0;
|
||||||
|
|
||||||
|
if (typeof value === 'string') return value.trim() !== '';
|
||||||
|
|
||||||
|
return true; // для чисел, булевых и т.п.
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatPrice(raw) {
|
||||||
|
if (raw === null || raw === undefined) return '';
|
||||||
|
|
||||||
|
const str = String(raw).trim();
|
||||||
|
const match = str.match(/^([+-]?)(\d+(?:\.\d+)?)/);
|
||||||
|
if (!match) return '';
|
||||||
|
|
||||||
|
const sign = match[1] || '';
|
||||||
|
const num = parseFloat(match[2]);
|
||||||
|
|
||||||
|
if (isNaN(num) || num === 0) return '';
|
||||||
|
|
||||||
|
const formatted = Math.round(num)
|
||||||
|
.toString()
|
||||||
|
.replace(/\B(?=(\d{3})+(?!\d))/g, ' ');
|
||||||
|
|
||||||
|
return `${sign}${formatted}`;
|
||||||
|
}
|
||||||
@@ -6,9 +6,9 @@ import { router } from './router';
|
|||||||
import { createPinia } from 'pinia';
|
import { createPinia } from 'pinia';
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
night_auto: false,
|
night_auto: true,
|
||||||
theme: {
|
theme: {
|
||||||
light: 'fantasy',
|
light: 'light',
|
||||||
dark: 'dark',
|
dark: 'dark',
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,14 +2,14 @@ import {createMemoryHistory, createRouter} from 'vue-router';
|
|||||||
import Home from './views/Home.vue';
|
import Home from './views/Home.vue';
|
||||||
import Product from './views/Product.vue';
|
import Product from './views/Product.vue';
|
||||||
import CategoriesList from "./views/CategoriesList.vue";
|
import CategoriesList from "./views/CategoriesList.vue";
|
||||||
import ProductsList from "@/components/ProductsList.vue";
|
|
||||||
import Cart from "./views/Cart.vue";
|
import Cart from "./views/Cart.vue";
|
||||||
|
import Products from "@/views/Products.vue";
|
||||||
|
|
||||||
const routes = [
|
const routes = [
|
||||||
{path: '/', name: 'home', component: Home},
|
{path: '/', name: 'home', component: Home},
|
||||||
{path: '/product/:id', name: 'product.show', component: Product},
|
{path: '/product/:id', name: 'product.show', component: Product},
|
||||||
{path: '/categories', name: 'categories', component: CategoriesList},
|
{path: '/categories', name: 'categories', component: CategoriesList},
|
||||||
{path: '/category/:id', name: 'category.show', component: ProductsList},
|
{path: '/category/:id', name: 'category.show', component: Products},
|
||||||
{path: '/cart', name: 'cart.show', component: Cart},
|
{path: '/cart', name: 'cart.show', component: Cart},
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -20,7 +20,7 @@ export const router = createRouter({
|
|||||||
if (savedPosition) {
|
if (savedPosition) {
|
||||||
return savedPosition; // Восстановить позицию прокрутки
|
return savedPosition; // Восстановить позицию прокрутки
|
||||||
} else {
|
} else {
|
||||||
return { top: 0 }; // Или оставить на старте
|
return {top: 0}; // Или оставить на старте
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,66 +1,102 @@
|
|||||||
import {defineStore} from "pinia";
|
import {defineStore} from "pinia";
|
||||||
import md5 from 'crypto-js/md5';
|
|
||||||
import ftch from "@/utils/ftch.js";
|
import ftch from "@/utils/ftch.js";
|
||||||
|
import {$fetch} from "ofetch";
|
||||||
|
import {isNotEmpty} from "@/helpers.js";
|
||||||
|
|
||||||
export const useCartStore = defineStore('cart', {
|
export const useCartStore = defineStore('cart', {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
items: [],
|
items: [],
|
||||||
|
products: [],
|
||||||
|
productsCount: 0,
|
||||||
|
total: 0,
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
}),
|
}),
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
getItem(rowId) {
|
async getProducts() {
|
||||||
return this.items.find(item => item.rowId === rowId) ?? null;
|
try {
|
||||||
},
|
this.isLoading = true;
|
||||||
|
const {data} = await ftch('cart');
|
||||||
hasItem(rowId) {
|
this.products = data.products;
|
||||||
return this.getItem(rowId) !== null;
|
this.productsCount = data.count;
|
||||||
|
this.total = data.total;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
} finally {
|
||||||
|
this.isLoading = false;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async addProduct(productId, productName, price, quantity = 1, options = []) {
|
async addProduct(productId, productName, price, quantity = 1, options = []) {
|
||||||
const rowId = this.generateRowId(productId, options);
|
|
||||||
|
|
||||||
const item = {
|
|
||||||
rowId: rowId,
|
|
||||||
productId: productId,
|
|
||||||
productName: productName,
|
|
||||||
price: price,
|
|
||||||
quantity: quantity,
|
|
||||||
options: JSON.parse(JSON.stringify(options)), // ← 💥 глубокая копия!
|
|
||||||
};
|
|
||||||
|
|
||||||
this.items.push(item);
|
|
||||||
|
|
||||||
return rowId;
|
|
||||||
},
|
|
||||||
|
|
||||||
removeItem(rowId) {
|
|
||||||
this.items.splice(this.items.indexOf(rowId), 1);
|
|
||||||
},
|
|
||||||
|
|
||||||
getQuantity(rowId) {
|
|
||||||
if (this.hasItem(rowId)) {
|
|
||||||
return this.getItem(rowId).quantity;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
},
|
|
||||||
|
|
||||||
setQuantity(rowId, quantity) {
|
|
||||||
this.getItem(rowId).quantity = quantity;
|
|
||||||
},
|
|
||||||
|
|
||||||
generateRowId(productId, options) {
|
|
||||||
return md5(productId + JSON.stringify(options)).toString();
|
|
||||||
},
|
|
||||||
|
|
||||||
async checkout() {
|
|
||||||
try {
|
try {
|
||||||
this.isLoading = true;
|
this.isLoading = true;
|
||||||
const {data} = await ftch('checkout', null, this.items);
|
const formData = new FormData();
|
||||||
this.items = data;
|
formData.append("product_id", productId);
|
||||||
} catch (e) {
|
formData.append("quantity", quantity);
|
||||||
console.error(e);
|
|
||||||
|
// TODO: Add support different types of options
|
||||||
|
options.forEach((option) => {
|
||||||
|
if (option.type === "checkbox" && Array.isArray(option.value)) {
|
||||||
|
option.value.forEach(item => {
|
||||||
|
formData.append(`option[${option.product_option_id}][]`, item.product_option_value_id);
|
||||||
|
});
|
||||||
|
} else if (option.type === "radio" && isNotEmpty(option.value)) {
|
||||||
|
formData.append(`option[${option.product_option_id}]`, option.value.product_option_value_id);
|
||||||
|
} else if (option.type === "select" && isNotEmpty(option.value)) {
|
||||||
|
formData.append(`option[${option.product_option_id}]`, option.value.product_option_value_id);
|
||||||
|
} else if ((option.type === "text" || option.type === 'textarea') && isNotEmpty(option.value)) {
|
||||||
|
formData.append(`option[${option.product_option_id}]`, option.value);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const response = await $fetch('/index.php?route=checkout/cart/add', {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.error) {
|
||||||
|
throw new Error(JSON.stringify(response.error));
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.getProducts();
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
throw error;
|
||||||
|
} finally {
|
||||||
|
this.isLoading = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async removeItem(rowId) {
|
||||||
|
try {
|
||||||
|
this.isLoading = true;
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('key', rowId);
|
||||||
|
await $fetch('/index.php?route=checkout/cart/remove', {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData,
|
||||||
|
});
|
||||||
|
await this.getProducts();
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
} finally {
|
||||||
|
this.isLoading = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async setQuantity(cartId, quantity) {
|
||||||
|
try {
|
||||||
|
this.isLoading = true;
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append(`quantity[${cartId}]`, quantity);
|
||||||
|
await $fetch('/index.php?route=checkout/cart/edit', {
|
||||||
|
redirect: 'manual',
|
||||||
|
method: 'POST',
|
||||||
|
body: formData,
|
||||||
|
});
|
||||||
|
await this.getProducts();
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
} finally {
|
} finally {
|
||||||
this.isLoading = false;
|
this.isLoading = false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ export const useProductsStore = defineStore('products', {
|
|||||||
data: [],
|
data: [],
|
||||||
meta: {},
|
meta: {},
|
||||||
},
|
},
|
||||||
|
products: {
|
||||||
|
data: [],
|
||||||
|
meta: {},
|
||||||
|
},
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
}),
|
}),
|
||||||
|
|
||||||
@@ -21,5 +25,18 @@ export const useProductsStore = defineStore('products', {
|
|||||||
this.isLoading = false;
|
this.isLoading = false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async fetchProducts(categoryId = null) {
|
||||||
|
try {
|
||||||
|
this.isLoading = true;
|
||||||
|
this.products = await ftch('products', {
|
||||||
|
categoryId: categoryId,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
} finally {
|
||||||
|
this.isLoading = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,46 +1,48 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="max-w-3xl mx-auto p-4 space-y-6">
|
<div class="max-w-3xl mx-auto p-4 space-y-6 pb-30">
|
||||||
<h2 class="text-2xl">
|
<h2 class="text-2xl">
|
||||||
Корзина
|
Корзина
|
||||||
<span v-if="cart.isLoading" class="loading loading-spinner loading-md"></span>
|
<span v-if="cart.isLoading" class="loading loading-spinner loading-md"></span>
|
||||||
</h2>
|
</h2>
|
||||||
|
|
||||||
<div>
|
<div v-if="cart.products.length > 0">
|
||||||
<button class="btn" @click="cart.checkout()" :disabled="cart.isLoading">
|
|
||||||
Checkout
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div v-if="cart.items.length > 0">
|
|
||||||
<div
|
<div
|
||||||
v-for="item in cart.items"
|
v-for="item in cart.products"
|
||||||
:key="item.rowId"
|
:key="item.cart_id"
|
||||||
class="card w-96 bg-base-100 card-sm shadow-sm"
|
class="card card-border bg-base-100 card-sm mb-3"
|
||||||
|
:class="item.stock === false ? 'border-error' : ''"
|
||||||
>
|
>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h2 class="card-title">{{ item.productName }}</h2>
|
<div class="avatar">
|
||||||
<p class="text-sm mt-1">{{ item.price }}</p>
|
<div class="w-16 rounded">
|
||||||
<div v-if="item.options.length">
|
<img :src="item.thumb"/>
|
||||||
<p v-for="option in item.options.filter(i => ['checkbox', 'radio', 'select', 'text', 'textarea'].indexOf(i.type) !== -1)">
|
</div>
|
||||||
<span v-if="option.type === 'radio'" class="text-xs font-medium">
|
</div>
|
||||||
{{ option.value.name }}<span v-if="option.value.price"> ({{ option.value.price_prefix }}{{ option.value.price }})</span>
|
|
||||||
</span>
|
<p v-if="! item.stock" class="text-error font-bold">Товар отсутствует на складе в нужном количестве.</p>
|
||||||
<span v-else-if="option.type === 'checkbox'" class="text-xs font-medium">
|
|
||||||
<span v-for="check in option.value" class="text-xs font-medium">
|
<h2 class="card-title">{{ item.name }}</h2>
|
||||||
{{ check.name }}<span v-if="check.price"> ({{ check.price_prefix }}{{ check.price }})</span>
|
<p class="text-sm font-bold">{{ formatPrice(item.total) }} ₽</p>
|
||||||
</span>
|
<p>{{ formatPrice(item.price) }} ₽/ед</p>
|
||||||
</span>
|
<div>
|
||||||
<span v-else-if="option.type === 'select'" class="text-xs font-medium">
|
<div v-for="option in item.option">
|
||||||
{{ option.value.name }}<span v-if="option.value.price"> ({{ option.value.price_prefix }}{{ option.value.price }})</span>
|
<component
|
||||||
</span>
|
v-if="SUPPORTED_OPTION_TYPES.includes(option.type) && componentMap[option.type]"
|
||||||
<span v-else-if="option.type === 'text' || option.type === 'textarea'" class="text-xs font-medium">
|
:is="componentMap[option.type]"
|
||||||
{{ option.value }}
|
:option="option"
|
||||||
</span>
|
/>
|
||||||
</p>
|
<div v-else class="text-sm text-error">
|
||||||
|
Тип опции "{{ option.type }}" не поддерживается.
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-actions justify-between">
|
<div class="card-actions justify-between">
|
||||||
<Quantity v-model="item.quantity" @update:modelValue="onQuantityUpdate(item.rowId, $event)"/>
|
<Quantity
|
||||||
<button class="btn btn-error" @click="cart.removeItem(item.rowId)">
|
:disabled="cart.isLoading"
|
||||||
|
v-model="item.quantity"
|
||||||
|
@update:modelValue="cart.setQuantity(item.cart_id, $event)"
|
||||||
|
/>
|
||||||
|
<button class="btn btn-error" @click="cart.removeItem(item.cart_id)" :disabled="cart.isLoading">
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
|
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
|
||||||
<path stroke-linecap="round" stroke-linejoin="round" d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
|
<path stroke-linecap="round" stroke-linejoin="round" d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
|
||||||
</svg>
|
</svg>
|
||||||
@@ -48,11 +50,21 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</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>
|
||||||
|
|
||||||
|
<span class="text-xs text-base-content mr-2">Всего:</span>
|
||||||
|
<span v-if="cart.isLoading" class="loading loading-spinner loading-xs"></span>
|
||||||
|
<span v-else class="text-accent font-bold">{{ formatPrice(cart.total) }} ₽</span>
|
||||||
|
</div>
|
||||||
|
<button class="btn btn-primary" :disabled="cart.isLoading">Перейти к оформлению</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
v-else
|
v-else
|
||||||
class="text-center text-gray-500 py-12 border border-dashed border-gray-300 rounded-2xl bg-white"
|
class="text-center rounded-2xl"
|
||||||
>
|
>
|
||||||
<p class="text-lg">Ваша корзина пуста</p>
|
<p class="text-lg">Ваша корзина пуста</p>
|
||||||
</div>
|
</div>
|
||||||
@@ -62,12 +74,19 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { useCartStore } from '../stores/CartStore.js'
|
import { useCartStore } from '../stores/CartStore.js'
|
||||||
import Quantity from "@/components/Quantity.vue";
|
import Quantity from "@/components/Quantity.vue";
|
||||||
|
import {SUPPORTED_OPTION_TYPES} from "@/constants/options.js";
|
||||||
|
import OptionRadio from "@/components/ProductOptions/Cart/OptionRadio.vue";
|
||||||
|
import OptionCheckbox from "@/components/ProductOptions/Cart/OptionCheckbox.vue";
|
||||||
|
import OptionText from "@/components/ProductOptions/Cart/OptionText.vue";
|
||||||
|
import {formatPrice} from "../helpers.js";
|
||||||
|
|
||||||
const cart = useCartStore()
|
const cart = useCartStore();
|
||||||
|
|
||||||
function onQuantityUpdate(rowId, newQuantity) {
|
const componentMap = {
|
||||||
if (newQuantity === 0) {
|
radio: OptionRadio,
|
||||||
cart.removeItem(rowId)
|
select: OptionRadio,
|
||||||
}
|
checkbox: OptionCheckbox,
|
||||||
}
|
text: OptionText,
|
||||||
|
textarea: OptionText,
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
@@ -34,57 +34,52 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div v-if="product.id" class="px-4 pb-10 pt-4 fixed bottom-0 left-0 w-full bg-base-200 z-50 flex justify-between gap-2 border-t-1 border-t-base-300">
|
<div v-if="product.id" class="fixed px-4 pb-10 pt-4 bottom-0 left-0 w-full bg-base-200 z-50 flex flex-col gap-2 border-t-1 border-t-base-300">
|
||||||
<div class="flex-1">
|
<div class="text-error">
|
||||||
<button
|
{{ error }}
|
||||||
class="btn btn-lg w-full"
|
|
||||||
:class="isInCartNow ? 'btn-success' : 'btn-primary'"
|
|
||||||
:disabled="canAddToCart === false"
|
|
||||||
@click="actionBtnClick"
|
|
||||||
>
|
|
||||||
<span>{{ buttonText }}</span><br>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
<div v-if="canAddToCart === false" class="text-error text-center text-xs mt-1">
|
|
||||||
Выберите обязательные опции
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Quantity
|
<div v-if="canAddToCart === false" class="text-error text-center text-xs mt-1">
|
||||||
v-if="quantity > 0"
|
Выберите обязательные опции
|
||||||
:modelValue="quantity"
|
</div>
|
||||||
@update:modelValue="setQuantity"
|
|
||||||
:max="10"
|
<div class="flex gap-2">
|
||||||
size="lg"
|
<div class="flex-1">
|
||||||
/>
|
<button
|
||||||
|
class="btn btn-primary btn-lg w-full"
|
||||||
|
:disabled="canAddToCart === false"
|
||||||
|
@click="actionBtnClick"
|
||||||
|
>
|
||||||
|
Купить
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Quantity
|
||||||
|
:modelValue="quantity"
|
||||||
|
@update:modelValue="setQuantity"
|
||||||
|
size="lg"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import {computed, onMounted, onUnmounted, ref, watch, watchEffect} from "vue";
|
import {computed, onMounted, ref} from "vue";
|
||||||
import {$fetch} from "ofetch";
|
import {$fetch} from "ofetch";
|
||||||
import {useRoute} from 'vue-router'
|
import {useRoute} from 'vue-router'
|
||||||
import {useRouter} from 'vue-router'
|
|
||||||
import ProductOptions from "../components/ProductOptions/ProductOptions.vue";
|
import ProductOptions from "../components/ProductOptions/ProductOptions.vue";
|
||||||
import {useCartStore} from "../stores/CartStore.js";
|
import {useCartStore} from "../stores/CartStore.js";
|
||||||
import ProductImageSwiper from "../components/ProductImageSwiper.vue";
|
import ProductImageSwiper from "../components/ProductImageSwiper.vue";
|
||||||
import Quantity from "../components/Quantity.vue";
|
import Quantity from "../components/Quantity.vue";
|
||||||
|
import {SUPPORTED_OPTION_TYPES} from "@/constants/options.js";
|
||||||
|
|
||||||
const route = useRoute();
|
const route = useRoute();
|
||||||
const router = useRouter();
|
|
||||||
const productId = computed(() => route.params.id);
|
const productId = computed(() => route.params.id);
|
||||||
const product = ref({});
|
const product = ref({});
|
||||||
const cart = useCartStore();
|
const cart = useCartStore();
|
||||||
|
const quantity = ref(1);
|
||||||
const rowId = computed(() => cart.generateRowId(productId.value, product.value.options));
|
const error = ref('');
|
||||||
|
|
||||||
const buttonText = computed(() => {
|
|
||||||
const item = cart.getItem(rowId.value);
|
|
||||||
return item && item.quantity > 0
|
|
||||||
? `В корзине`
|
|
||||||
: 'Добавить в корзину'
|
|
||||||
});
|
|
||||||
|
|
||||||
const canAddToCart = computed(() => {
|
const canAddToCart = computed(() => {
|
||||||
if (!product.value || product.value.options === undefined || product.value.options?.length === 0) {
|
if (!product.value || product.value.options === undefined || product.value.options?.length === 0) {
|
||||||
@@ -92,7 +87,7 @@ const canAddToCart = computed(() => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const required = product.value.options.filter(item => {
|
const required = product.value.options.filter(item => {
|
||||||
return ['checkbox', 'radio', 'select', 'text', 'textarea'].indexOf(item.type) !== -1
|
return SUPPORTED_OPTION_TYPES.includes(item.type)
|
||||||
&& item.required === true
|
&& item.required === true
|
||||||
&& !item.value;
|
&& !item.value;
|
||||||
});
|
});
|
||||||
@@ -100,32 +95,21 @@ const canAddToCart = computed(() => {
|
|||||||
return required.length === 0;
|
return required.length === 0;
|
||||||
});
|
});
|
||||||
|
|
||||||
const isInCartNow = computed(() => {
|
async function actionBtnClick() {
|
||||||
return cart.hasItem(rowId.value);
|
try {
|
||||||
});
|
error.value = '';
|
||||||
|
console.log(product.value);
|
||||||
const quantity = computed(() => {
|
await cart.addProduct(productId.value, product.value.name, product.value.price, quantity.value, product.value.options);
|
||||||
return cart.getQuantity(rowId.value);
|
|
||||||
});
|
|
||||||
|
|
||||||
function actionBtnClick() {
|
|
||||||
if (cart.hasItem(rowId.value)) {
|
|
||||||
window.Telegram.WebApp.HapticFeedback.selectionChanged();
|
|
||||||
router.push({name: 'cart.show'});
|
|
||||||
} else {
|
|
||||||
cart.addProduct(productId.value, product.value.name, product.value.price, 1, product.value.options);
|
|
||||||
window.Telegram.WebApp.HapticFeedback.notificationOccurred('success');
|
window.Telegram.WebApp.HapticFeedback.notificationOccurred('success');
|
||||||
|
} catch (e) {
|
||||||
|
await window.Telegram.WebApp.HapticFeedback.notificationOccurred('error');
|
||||||
|
error.value = e.message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setQuantity(newQuantity) {
|
function setQuantity(newQuantity) {
|
||||||
if (newQuantity === 0) {
|
quantity.value = newQuantity;
|
||||||
cart.removeItem(rowId.value);
|
window.Telegram.WebApp.HapticFeedback.selectionChanged();
|
||||||
window.Telegram.WebApp.HapticFeedback.notificationOccurred('warning');
|
|
||||||
} else {
|
|
||||||
cart.setQuantity(rowId.value, newQuantity);
|
|
||||||
window.Telegram.WebApp.HapticFeedback.selectionChanged();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
|
|||||||
22
spa/src/views/Products.vue
Normal file
22
spa/src/views/Products.vue
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<template>
|
||||||
|
<div ref="goodsRef">
|
||||||
|
<ProductsList
|
||||||
|
:products="productsStore.products.data"
|
||||||
|
:meta="productsStore.products.meta"
|
||||||
|
:isLoading="productsStore.isLoading"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import {useProductsStore} from "@/stores/ProductsStore.js";
|
||||||
|
import ProductsList from "@/components/ProductsList.vue";
|
||||||
|
import {onMounted} from "vue";
|
||||||
|
import {useRoute} from "vue-router";
|
||||||
|
|
||||||
|
const route = useRoute();
|
||||||
|
const categoryId = route.params.id ?? null;
|
||||||
|
|
||||||
|
const productsStore = useProductsStore();
|
||||||
|
onMounted(() => productsStore.fetchProducts(categoryId))
|
||||||
|
</script>
|
||||||
Reference in New Issue
Block a user