|
| 1 | +/** |
| 2 | + * @deprecated since version 1.0.0 |
| 3 | + */ |
| 4 | +import { |
| 5 | + CartGetters as CartGettersBase, |
| 6 | + AgnosticPrice, |
| 7 | + AgnosticTotals, |
| 8 | + AgnosticAttribute, |
| 9 | + AgnosticCoupon, |
| 10 | + AgnosticDiscount, |
| 11 | +} from '@vue-storefront/core'; |
| 12 | +import { |
| 13 | + Discount, |
| 14 | + Cart, |
| 15 | + CartItem, |
| 16 | + Product, |
| 17 | + SelectedShippingMethod, ConfigurableCartItem, ProductInterface, |
| 18 | +} from '@vue-storefront/magento-api'; |
| 19 | + |
| 20 | +import productGetters from './productGetters'; |
| 21 | +import { AgnosticPaymentMethod } from '../types'; |
| 22 | + |
| 23 | +export const getItems = (cart: Cart): CartItem[] => { |
| 24 | + if (!cart || !cart.items) { |
| 25 | + return []; |
| 26 | + } |
| 27 | + |
| 28 | + return cart.items; |
| 29 | +}; |
| 30 | + |
| 31 | +export const getItemName = (product: CartItem): string => productGetters.getName(product.product as Product); |
| 32 | + |
| 33 | +export const getSlug = (product: CartItem): string => productGetters.getSlug(product.product as Product); |
| 34 | + |
| 35 | +export const getItemImage = (product: CartItem): string => productGetters.getProductThumbnailImage(product.product as Product); |
| 36 | + |
| 37 | +export const getItemPrice = (product: CartItem): AgnosticPrice => { |
| 38 | + if (!product || !product.prices) { |
| 39 | + return { |
| 40 | + regular: 0, |
| 41 | + special: 0, |
| 42 | + }; |
| 43 | + } |
| 44 | + if (product.prices) { |
| 45 | + return { |
| 46 | + regular: product.prices.row_total.value || 0, |
| 47 | + special: product.prices.total_item_discount.value || 0, |
| 48 | + }; |
| 49 | + } |
| 50 | + const regularPrice = product.product?.price_range?.minimum_price?.regular_price?.value; |
| 51 | + const specialPrice = product.product?.price_range?.minimum_price?.final_price?.value; |
| 52 | + |
| 53 | + return { |
| 54 | + regular: regularPrice || 0, |
| 55 | + special: specialPrice || 0, |
| 56 | + // @ts-ignore |
| 57 | + credit: Math.round(specialPrice / 10), |
| 58 | + // @TODO: Who set this installment value? |
| 59 | + installment: Math.round((specialPrice * 1.1046) / 10), |
| 60 | + discountPercentage: 100 - Math.round((specialPrice / regularPrice) * 100), |
| 61 | + total: product.prices?.row_total?.value, |
| 62 | + }; |
| 63 | +}; |
| 64 | + |
| 65 | +export const productHasSpecialPrice = (product: CartItem): boolean => getItemPrice(product).regular < getItemPrice(product).special; |
| 66 | + |
| 67 | +export const getItemQty = (product: CartItem): number => product.quantity; |
| 68 | + |
| 69 | +export const getItemAttributes = ( |
| 70 | + { product }: CartItem & { product: Product }, |
| 71 | + _filterByAttributeName?: Array<string>, |
| 72 | +): Record<string, AgnosticAttribute | string> => { |
| 73 | + const attributes = {}; |
| 74 | + |
| 75 | + if (!product || !product.configurable_options) { |
| 76 | + return attributes; |
| 77 | + } |
| 78 | + |
| 79 | + const configurableOptions = product.configurable_options; |
| 80 | + |
| 81 | + // eslint-disable-next-line no-restricted-syntax |
| 82 | + for (const option of configurableOptions) { |
| 83 | + attributes[option.attribute_code] = { |
| 84 | + name: option.attribute_code, |
| 85 | + label: option.label, |
| 86 | + value: option.values.map((value) => { |
| 87 | + const obj = {}; |
| 88 | + obj[value.value_index] = value.label; |
| 89 | + return obj; |
| 90 | + }), |
| 91 | + } as AgnosticAttribute; |
| 92 | + } |
| 93 | + return attributes; |
| 94 | +}; |
| 95 | + |
| 96 | +export const getItemSku = (product: CartItem): string => product?.product?.sku || ''; |
| 97 | + |
| 98 | +const calculateDiscounts = (discounts: Discount[]): number => discounts.reduce((a, b) => Number.parseFloat(`${a}`) + Number.parseFloat(`${b.amount.value}`), 0); |
| 99 | + |
| 100 | +export const getTotals = (cart: Cart): AgnosticTotals => { |
| 101 | + if (!cart || !cart.prices) return {} as AgnosticTotals; |
| 102 | + |
| 103 | + const subtotal = cart.prices.subtotal_including_tax.value; |
| 104 | + return { |
| 105 | + total: cart.prices.grand_total.value, |
| 106 | + subtotal: cart.prices.subtotal_including_tax.value, |
| 107 | + special: (cart.prices.discounts) ? subtotal - calculateDiscounts(cart.prices.discounts) : subtotal, |
| 108 | + } as AgnosticTotals; |
| 109 | +}; |
| 110 | + |
| 111 | +export const getShippingPrice = (cart: Cart): number => { |
| 112 | + if (!cart.shipping_addresses) { |
| 113 | + return 0; |
| 114 | + } |
| 115 | + |
| 116 | + return cart.shipping_addresses |
| 117 | + .reduce(( |
| 118 | + acc, |
| 119 | + shippingAddress, |
| 120 | + ) => { |
| 121 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 122 | + const { selected_shipping_method } = shippingAddress; |
| 123 | + |
| 124 | + if (selected_shipping_method) { |
| 125 | + // eslint-disable-next-line @typescript-eslint/restrict-plus-operands |
| 126 | + return acc + selected_shipping_method.amount.value; |
| 127 | + } |
| 128 | + |
| 129 | + return acc; |
| 130 | + }, 0); |
| 131 | +}; |
| 132 | + |
| 133 | +export const getTotalItems = (cart: Cart): number => { |
| 134 | + if (!cart) { |
| 135 | + return 0; |
| 136 | + } |
| 137 | + return cart.total_quantity; |
| 138 | +}; |
| 139 | + |
| 140 | +export const getConfiguredVariant = (product: ConfigurableCartItem): ProductInterface | null => product?.configured_variant || null; |
| 141 | + |
| 142 | +// eslint-disable-next-line import/no-named-as-default-member |
| 143 | +export const getFormattedPrice = (price: number) => productGetters.getFormattedPrice(price); |
| 144 | + |
| 145 | +export const getCoupons = (cart: Cart): AgnosticCoupon[] => (Array.isArray(cart?.applied_coupons) ? cart.applied_coupons.map((c) => ({ |
| 146 | + id: c.code, |
| 147 | + name: c.code, |
| 148 | + value: 0, |
| 149 | + code: c.code, |
| 150 | +} as AgnosticCoupon)) : []); |
| 151 | + |
| 152 | +export const getDiscounts = (cart: Cart): AgnosticDiscount[] => (Array.isArray(cart?.prices?.discounts) ? cart.prices.discounts.map((d) => ({ |
| 153 | + id: d.label, |
| 154 | + name: d.label, |
| 155 | + description: '', |
| 156 | + value: d.amount.value, |
| 157 | + code: d.label, |
| 158 | +} as AgnosticDiscount)) : []); |
| 159 | + |
| 160 | +export const getAppliedCoupon = (cart: Cart): AgnosticCoupon | null => (Array.isArray(cart?.applied_coupons) && cart?.applied_coupons.length > 0 ? { |
| 161 | + id: cart.applied_coupons[0].code, |
| 162 | + name: cart.applied_coupons[0].code, |
| 163 | + value: 0, |
| 164 | + code: cart.applied_coupons[0].code, |
| 165 | +} : null); |
| 166 | + |
| 167 | +export const getSelectedShippingMethod = (cart: Cart): SelectedShippingMethod | null => (cart?.shipping_addresses?.length > 0 |
| 168 | + ? cart?.shipping_addresses[0]?.selected_shipping_method |
| 169 | + : null); |
| 170 | + |
| 171 | +export const getAvailablePaymentMethods = (cart: Cart): AgnosticPaymentMethod[] => cart?.available_payment_methods.map((p) => ({ |
| 172 | + label: p.title, |
| 173 | + value: p.code, |
| 174 | +})); |
| 175 | + |
| 176 | +export const getStockStatus = (product: CartItem): string => product.product.stock_status; |
| 177 | +export interface CartGetters extends CartGettersBase<Cart, CartItem> { |
| 178 | + getAppliedCoupon(cart: Cart): AgnosticCoupon | null; |
| 179 | + getAvailablePaymentMethods(cart: Cart): AgnosticPaymentMethod[]; |
| 180 | + getSelectedShippingMethod(cart: Cart): SelectedShippingMethod | null; |
| 181 | + productHasSpecialPrice(product: CartItem): boolean; |
| 182 | + getStockStatus(product: CartItem): string; |
| 183 | + getConfiguredVariant(product: ConfigurableCartItem): ProductInterface | null; |
| 184 | +} |
| 185 | + |
| 186 | +const cartGetters: CartGetters = { |
| 187 | + getAppliedCoupon, |
| 188 | + getAvailablePaymentMethods, |
| 189 | + getCoupons, |
| 190 | + getDiscounts, |
| 191 | + getFormattedPrice, |
| 192 | + getItemAttributes, |
| 193 | + getItemImage, |
| 194 | + getItemName, |
| 195 | + getSlug, |
| 196 | + getItemPrice, |
| 197 | + getItemQty, |
| 198 | + getItems, |
| 199 | + getItemSku, |
| 200 | + getSelectedShippingMethod, |
| 201 | + getShippingPrice, |
| 202 | + getTotalItems, |
| 203 | + getTotals, |
| 204 | + productHasSpecialPrice, |
| 205 | + getStockStatus, |
| 206 | + getConfiguredVariant, |
| 207 | +}; |
| 208 | + |
| 209 | +export default cartGetters; |
0 commit comments