import { Context , Logger , useShippingFactory , UseShippingParams , } from '@vue-storefront/core' ; import { SetShippingAddressesOnCartInput , } from '@vue-storefront/magento-api' ; import useCart from '../useCart' ; import useGetShippingMethods from '../useGetShippingMethods' ; const factoryParams = { provide ( ) { return { useGetShippingMethods : useGetShippingMethods ( ) , cart : useCart ( ) , } ; } , load : async ( context , { customQuery } ) => { Logger . debug ( '[Magento] loadShipping' , { customQuery } ) ; if ( ! context . cart . cart ?. value ?. shipping_addresses ) { await context . cart . load ( { customQuery } ) ; } return context . cart . cart . value . shipping_addresses [ 0 ] ; } , save : async ( context , params ) => { Logger . debug ( '[Magento] save user shipping address' , { params } ) ; const { id } = context . cart . cart . value ; const { apartment , neighborhood , extra , customerAddressId , ... address } = params . shippingDetails ; const shippingData = customerAddressId ? ( { customer_address_id : customerAddressId , } ) : ( { address : { ... address , street : [ address . street , apartment , neighborhood , extra ] , } , } ) ; const shippingAddressInput = { cart_id : id , shipping_addresses : [ { ... shippingData , } , ] , } ; const { data } = await context . $magento . api . setShippingAddressesOnCart ( shippingAddressInput ) ; Logger . debug ( '[Result]:' , { data } ) ; context . useGetShippingMethods . setState ( data . setShippingAddressesOnCart . cart . shipping_addresses [ 0 ] . available_shipping_methods ) ; return data . setShippingAddressesOnCart . cart . shipping_addresses [ 0 ] ; } , } ; export default useShippingFactory < any , any > ( factoryParams ) ;