feat(saleor): Phase 5 - Remove WooCommerce
- Remove @woocommerce/woocommerce-rest-api dependency - Delete src/lib/woocommerce.ts - Delete src/stores/cartStore.ts (replaced by saleorCheckoutStore) - Clean up package.json dependencies - Project now fully migrated to Saleor GraphQL API
This commit is contained in:
@@ -1,129 +0,0 @@
|
||||
import WooCommerceRestApi from "@woocommerce/woocommerce-rest-api";
|
||||
|
||||
// Lazy initialization - only create API client when needed
|
||||
let apiInstance: WooCommerceRestApi | null = null;
|
||||
|
||||
function getApi(): WooCommerceRestApi {
|
||||
if (!apiInstance) {
|
||||
const url = process.env.NEXT_PUBLIC_WOOCOMMERCE_URL;
|
||||
const consumerKey = process.env.NEXT_PUBLIC_WOOCOMMERCE_CONSUMER_KEY;
|
||||
const consumerSecret = process.env.NEXT_PUBLIC_WOOCOMMERCE_CONSUMER_SECRET;
|
||||
|
||||
if (!url || !consumerKey || !consumerSecret) {
|
||||
throw new Error("WooCommerce API credentials not configured");
|
||||
}
|
||||
|
||||
apiInstance = new WooCommerceRestApi({
|
||||
url,
|
||||
consumerKey,
|
||||
consumerSecret,
|
||||
version: "wc/v3",
|
||||
queryStringAuth: true, // Use query string auth instead of basic auth (more reliable)
|
||||
});
|
||||
}
|
||||
return apiInstance;
|
||||
}
|
||||
|
||||
export interface WooProduct {
|
||||
id: number;
|
||||
name: string;
|
||||
slug: string;
|
||||
price: string;
|
||||
regular_price: string;
|
||||
sale_price: string;
|
||||
description: string;
|
||||
short_description: string;
|
||||
status: "publish" | "draft" | "private";
|
||||
stock_status: "instock" | "outofstock";
|
||||
images: { id: number; src: string; alt: string }[];
|
||||
sku: string;
|
||||
categories: { id: number; name: string; slug: string }[];
|
||||
meta_data: { key: string; value: string }[];
|
||||
}
|
||||
|
||||
export interface WooCategory {
|
||||
id: number;
|
||||
name: string;
|
||||
slug: string;
|
||||
description: string;
|
||||
image: { src: string } | null;
|
||||
}
|
||||
|
||||
export async function getProducts(perPage = 100): Promise<WooProduct[]> {
|
||||
try {
|
||||
const api = getApi();
|
||||
const response = await api.get("products", { per_page: perPage });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error fetching products:", error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function getProduct(id: number): Promise<WooProduct | null> {
|
||||
try {
|
||||
const api = getApi();
|
||||
const response = await api.get(`products/${id}`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error(`Error fetching product ${id}:`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getProductBySlug(slug: string): Promise<WooProduct | null> {
|
||||
try {
|
||||
const api = getApi();
|
||||
const response = await api.get("products", { slug });
|
||||
return response.data[0] || null;
|
||||
} catch (error) {
|
||||
console.error(`Error fetching product by slug ${slug}:`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getCategories(): Promise<WooCategory[]> {
|
||||
try {
|
||||
const api = getApi();
|
||||
const response = await api.get("product-categories", { per_page: 100 });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error fetching categories:", error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function getProductsByCategory(
|
||||
categoryId: number
|
||||
): Promise<WooProduct[]> {
|
||||
try {
|
||||
const api = getApi();
|
||||
const response = await api.get("products", {
|
||||
category: categoryId,
|
||||
per_page: 100,
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error(`Error fetching products for category ${categoryId}:`, error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export function formatPrice(price: string, currency = "RSD"): string {
|
||||
const num = parseFloat(price);
|
||||
if (isNaN(num)) return "0 RSD";
|
||||
return new Intl.NumberFormat("sr-RS", {
|
||||
style: "currency",
|
||||
currency: currency,
|
||||
minimumFractionDigits: 0,
|
||||
}).format(num);
|
||||
}
|
||||
|
||||
export function getProductImage(product: WooProduct): string {
|
||||
if (product.images && product.images.length > 0) {
|
||||
return product.images[0].src;
|
||||
}
|
||||
return "/placeholder-product.jpg";
|
||||
}
|
||||
|
||||
export default getApi;
|
||||
@@ -1,86 +0,0 @@
|
||||
import { create } from "zustand";
|
||||
import { persist } from "zustand/middleware";
|
||||
|
||||
export interface CartItem {
|
||||
id: number;
|
||||
name: string;
|
||||
price: string;
|
||||
quantity: number;
|
||||
image: string;
|
||||
sku: string;
|
||||
}
|
||||
|
||||
interface CartStore {
|
||||
items: CartItem[];
|
||||
isOpen: boolean;
|
||||
addItem: (item: CartItem) => void;
|
||||
removeItem: (id: number) => void;
|
||||
updateQuantity: (id: number, quantity: number) => void;
|
||||
toggleCart: () => void;
|
||||
openCart: () => void;
|
||||
closeCart: () => void;
|
||||
clearCart: () => void;
|
||||
getTotal: () => number;
|
||||
getItemCount: () => number;
|
||||
}
|
||||
|
||||
export const useCartStore = create<CartStore>()(
|
||||
persist(
|
||||
(set, get) => ({
|
||||
items: [],
|
||||
isOpen: false,
|
||||
|
||||
addItem: (item) => {
|
||||
const items = get().items;
|
||||
const existingItem = items.find((i) => i.id === item.id);
|
||||
|
||||
if (existingItem) {
|
||||
set({
|
||||
items: items.map((i) =>
|
||||
i.id === item.id
|
||||
? { ...i, quantity: i.quantity + item.quantity }
|
||||
: i
|
||||
),
|
||||
});
|
||||
} else {
|
||||
set({ items: [...items, item] });
|
||||
}
|
||||
set({ isOpen: true });
|
||||
},
|
||||
|
||||
removeItem: (id) => {
|
||||
set({ items: get().items.filter((i) => i.id !== id) });
|
||||
},
|
||||
|
||||
updateQuantity: (id, quantity) => {
|
||||
if (quantity <= 0) {
|
||||
set({ items: get().items.filter((i) => i.id !== id) });
|
||||
} else {
|
||||
set({
|
||||
items: get().items.map((i) =>
|
||||
i.id === id ? { ...i, quantity } : i
|
||||
),
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
toggleCart: () => set({ isOpen: !get().isOpen }),
|
||||
openCart: () => set({ isOpen: true }),
|
||||
closeCart: () => set({ isOpen: false }),
|
||||
clearCart: () => set({ items: [] }),
|
||||
|
||||
getTotal: () => {
|
||||
return get().items.reduce((total, item) => {
|
||||
return total + parseFloat(item.price) * item.quantity;
|
||||
}, 0);
|
||||
},
|
||||
|
||||
getItemCount: () => {
|
||||
return get().items.reduce((count, item) => count + item.quantity, 0);
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: "manoonoils-cart",
|
||||
}
|
||||
)
|
||||
);
|
||||
Reference in New Issue
Block a user