move to gitea
This commit is contained in:
181
src_backup/lib/woocommerce.ts
Normal file
181
src_backup/lib/woocommerce.ts
Normal file
@@ -0,0 +1,181 @@
|
||||
// src/lib/woocommerce.ts
|
||||
|
||||
// @ts-ignore
|
||||
import WooCommerceRestApi from "@woocommerce/woocommerce-rest-api";
|
||||
import { WooCommerceProduct, ApiResponse } from "@/types/woocommerce";
|
||||
|
||||
// Configuration de l'API WooCommerce
|
||||
export const api = new WooCommerceRestApi({
|
||||
url: process.env.NEXT_PUBLIC_WC_API_URL || "",
|
||||
consumerKey: process.env.NEXT_PUBLIC_WC_CONSUMER_KEY || "",
|
||||
consumerSecret: process.env.NEXT_PUBLIC_WC_CONSUMER_SECRET || "",
|
||||
version: "wc/v3",
|
||||
queryStringAuth: true,
|
||||
});
|
||||
|
||||
// Fonctions utilitaires pour l'API
|
||||
export class WooCommerceService {
|
||||
// Récupérer tous les produits
|
||||
static async getProducts(params?: {
|
||||
page?: number;
|
||||
per_page?: number;
|
||||
category?: string;
|
||||
search?: string;
|
||||
orderby?: string;
|
||||
order?: 'asc' | 'desc';
|
||||
on_sale?: boolean;
|
||||
featured?: boolean;
|
||||
}): Promise<ApiResponse<WooCommerceProduct[]>> {
|
||||
try {
|
||||
const defaultParams = {
|
||||
page: 1,
|
||||
per_page: 20,
|
||||
status: 'publish',
|
||||
...params
|
||||
};
|
||||
|
||||
const response = await api.get("products", defaultParams);
|
||||
|
||||
return {
|
||||
data: response.data as WooCommerceProduct[],
|
||||
success: true,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la récupération des produits:", error);
|
||||
return {
|
||||
data: [],
|
||||
success: false,
|
||||
message: "Erreur lors de la récupération des produits"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Récupérer un produit par son slug
|
||||
static async getProductBySlug(slug: string): Promise<ApiResponse<WooCommerceProduct | null>> {
|
||||
try {
|
||||
const response = await api.get("products", { slug, status: 'publish' });
|
||||
const products = response.data as WooCommerceProduct[];
|
||||
|
||||
return {
|
||||
data: products.length > 0 ? products[0] || null : null,
|
||||
success: true,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la récupération du produit:", error);
|
||||
return {
|
||||
data: null,
|
||||
success: false,
|
||||
message: "Produit non trouvé"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Récupérer un produit par son ID
|
||||
static async getProductById(id: number): Promise<ApiResponse<WooCommerceProduct | null>> {
|
||||
try {
|
||||
const response = await api.get(`products/${id}`);
|
||||
|
||||
return {
|
||||
data: response.data as WooCommerceProduct,
|
||||
success: true,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la récupération du produit:", error);
|
||||
return {
|
||||
data: null,
|
||||
success: false,
|
||||
message: "Produit non trouvé"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Récupérer les produits en vedette
|
||||
static async getFeaturedProducts(limit: number = 6): Promise<ApiResponse<WooCommerceProduct[]>> {
|
||||
return this.getProducts({
|
||||
featured: true,
|
||||
per_page: limit,
|
||||
orderby: 'date',
|
||||
order: 'desc'
|
||||
});
|
||||
}
|
||||
|
||||
// Récupérer les produits en promotion
|
||||
static async getSaleProducts(limit: number = 6): Promise<ApiResponse<WooCommerceProduct[]>> {
|
||||
return this.getProducts({
|
||||
on_sale: true,
|
||||
per_page: limit,
|
||||
orderby: 'date',
|
||||
order: 'desc'
|
||||
});
|
||||
}
|
||||
|
||||
// Récupérer les nouvelles arrivées
|
||||
static async getNewArrivals(limit: number = 8): Promise<ApiResponse<WooCommerceProduct[]>> {
|
||||
return this.getProducts({
|
||||
per_page: limit,
|
||||
orderby: 'date',
|
||||
order: 'desc'
|
||||
});
|
||||
}
|
||||
|
||||
// Récupérer les catégories
|
||||
static async getCategories(): Promise<ApiResponse<any[]>> {
|
||||
try {
|
||||
const response = await api.get("products/categories", {
|
||||
per_page: 100,
|
||||
hide_empty: true
|
||||
});
|
||||
|
||||
return {
|
||||
data: response.data,
|
||||
success: true,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la récupération des catégories:", error);
|
||||
return {
|
||||
data: [],
|
||||
success: false,
|
||||
message: "Erreur lors de la récupération des catégories"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Rechercher des produits
|
||||
static async searchProducts(query: string, limit: number = 20): Promise<ApiResponse<WooCommerceProduct[]>> {
|
||||
return this.getProducts({
|
||||
search: query,
|
||||
per_page: limit
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Fonctions utilitaires pour les prix
|
||||
export const formatPrice = (price: string | number, currency: string = 'MRU'): string => {
|
||||
const numPrice = typeof price === 'string' ? parseFloat(price) : price;
|
||||
return `${numPrice.toLocaleString('fr-FR')} ${currency}`;
|
||||
};
|
||||
|
||||
// Vérifier si un produit est en promotion
|
||||
export const isOnSale = (product: WooCommerceProduct): boolean => {
|
||||
return product.on_sale && product.sale_price !== '';
|
||||
};
|
||||
|
||||
// Calculer le pourcentage de réduction
|
||||
export const getDiscountPercentage = (product: WooCommerceProduct): number => {
|
||||
if (!isOnSale(product)) return 0;
|
||||
|
||||
const regularPrice = parseFloat(product.regular_price);
|
||||
const salePrice = parseFloat(product.sale_price);
|
||||
|
||||
return Math.round(((regularPrice - salePrice) / regularPrice) * 100);
|
||||
};
|
||||
|
||||
// Obtenir la première image d'un produit
|
||||
export const getProductImage = (product: WooCommerceProduct): string => {
|
||||
return product.images && product.images.length > 0 ? product.images[0]!.src : '/placeholder-product.jpg';
|
||||
};
|
||||
|
||||
// Obtenir toutes les images d'un produit
|
||||
export const getProductImages = (product: WooCommerceProduct): string[] => {
|
||||
return product.images ? product.images.map(image => image.src) : [];
|
||||
};
|
||||
Reference in New Issue
Block a user