import { CartGetters as CartGettersBase , AgnosticPrice , AgnosticTotals , AgnosticAttribute , AgnosticCoupon , AgnosticDiscount , } from '@vue-storefront/core' ; import { Discount , Cart , CartItem , Product , SelectedShippingMethod , } from '@vue-storefront/magento-api' ; import productGetters from './productGetters' ; import { AgnosticPaymentMethod } from '../types' ; export const getItems = ( cart ) : CartItem [ ] => { if ( ! cart || ! cart . items ) { return [ ] ; } return cart . items ; } ; export const getItemName = ( product ) : string => productGetters . getName ( product . product as Product ) ; export const getSlug = ( product ) : string => productGetters . getSlug ( product . product as Product ) ; export const getItemImage = ( product ) : string => productGetters . getProductThumbnailImage ( product . product as Product ) ; export const getItemPrice = ( product ) : AgnosticPrice => { if ( ! product || ! product . prices ) { return { regular : 0 , special : 0 , } ; } if ( product . prices ) { return { regular : product . prices . row_total . value || 0 , special : product . prices . total_item_discount . value || 0 , } ; } const regularPrice = product . product ?. price_range ?. minimum_price ?. regular_price ?. value ; const specialPrice = product . product ?. price_range ?. minimum_price ?. final_price ?. value ; return { regular : regularPrice || 0 , special : specialPrice || 0 , credit : Math . round ( specialPrice / 10 ) , installment : Math . round ( ( specialPrice * 1.1046 ) / 10 ) , discountPercentage : 100 - Math . round ( ( specialPrice / regularPrice ) * 100 ) , total : product . prices ?. row_total ?. value , } ; } ; export const productHasSpecialPrice = ( product ) : boolean => getItemPrice ( product ) . regular < getItemPrice ( product ) . special ; export const getItemQty = ( product ) : number => product . quantity ; export const getItemAttributes = ( { product } , _filterByAttributeName ? ) : Record < string , AgnosticAttribute | string > => { const attributes = { } ; if ( ! product || ! product . configurable_options ) { return attributes ; } const configurableOptions = product . configurable_options ; for ( const option of configurableOptions ) { attributes [ option . attribute_code ] = { name : option . attribute_code , label : option . label , value : option . values . map ( ( value ) => { const obj = { } ; obj [ value . value_index ] = value . label ; return obj ; } ) , } as AgnosticAttribute ; } return attributes ; } ; export const getItemSku = ( product ) : string => { if ( ! product . product ) { return '' ; } return product . product . sku ; } ; const calculateDiscounts = ( discounts ) : number => discounts . reduce ( ( a , b ) => Number . parseFloat ( `${ a }` ) + Number . parseFloat ( `${ b . amount . value }` ) , 0 ) ; export const getTotals = ( cart ) : AgnosticTotals => { if ( ! cart || ! cart . prices ) return { } as AgnosticTotals ; const subtotal = cart . prices . subtotal_including_tax . value ; return { total : cart . prices . grand_total . value , subtotal : cart . prices . subtotal_including_tax . value , special : ( cart . prices . discounts ) ? subtotal - calculateDiscounts ( cart . prices . discounts ) : subtotal , } as AgnosticTotals ; } ; export const getShippingPrice = ( cart ) : number => { if ( ! cart . shipping_addresses ) { return 0 ; } return cart . shipping_addresses . reduce ( ( acc , shippingAddress ) => { const { selected_shipping_method } = shippingAddress ; if ( selected_shipping_method ) { return acc + selected_shipping_method . amount . value ; } return acc ; } , 0 ) ; } ; export const getTotalItems = ( cart ) : number => { if ( ! cart ) { return 0 ; } return cart . total_quantity ; } ; export const getFormattedPrice = ( price ) => productGetters . getFormattedPrice ( price ) ; export const getCoupons = ( cart ) : AgnosticCoupon [ ] => ( Array . isArray ( cart ?. applied_coupons ) ? cart . applied_coupons . map ( ( c ) => ( { id : c . code , name : c . code , value : 0 , code : c . code , } as AgnosticCoupon ) ) : [ ] ) ; export const getDiscounts = ( cart ) : AgnosticDiscount [ ] => ( Array . isArray ( cart ?. prices . discounts ) ? cart . prices . discounts . map ( ( d ) => ( { id : d . label , name : d . label , description : '' , value : d . amount . value , code : d . label , } as AgnosticDiscount ) ) : [ ] ) ; export const getAppliedCoupon = ( cart ) : AgnosticCoupon | null => ( Array . isArray ( cart ?. applied_coupons ) && cart ?. applied_coupons . length > 0 ? { id : cart . applied_coupons [ 0 ] . code , name : cart . applied_coupons [ 0 ] . code , value : 0 , code : cart . applied_coupons [ 0 ] . code , } : null ) ; export const getSelectedShippingMethod = ( cart ) : SelectedShippingMethod | null => ( cart ?. shipping_addresses ?. length > 0 ? cart ?. shipping_addresses [ 0 ] ?. selected_shipping_method : null ) ; export const getAvailablePaymentMethods = ( cart ) : AgnosticPaymentMethod [ ] => cart ?. available_payment_methods . map ( ( p ) => ( { label : p . title , value : p . code , } ) ) ; export interface CartGetters extends CartGettersBase < Cart , CartItem > { getAppliedCoupon ( cart ) : AgnosticCoupon | null ; getAvailablePaymentMethods ( cart ) : AgnosticPaymentMethod [ ] ; getSelectedShippingMethod ( cart ) : SelectedShippingMethod | null ; productHasSpecialPrice ( product ) : boolean ; } const cartGetters = { getAppliedCoupon , getAvailablePaymentMethods , getCoupons , getDiscounts , getFormattedPrice , getItemAttributes , getItemImage , getItemName , getSlug , getItemPrice , getItemQty , getItems , getItemSku , getSelectedShippingMethod , getShippingPrice , getTotalItems , getTotals , productHasSpecialPrice , } ; export default cartGetters ;