Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TQLG-392: preselected-shipping-cost #107

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions components/Checkout/CheckoutSummary.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup lang="ts">

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

i think we can delete this line, or? but not that important for me haha

withDefaults(
defineProps<{
reducedDisplay?: boolean;
Expand All @@ -7,6 +8,9 @@ withDefaults(
reducedDisplay: false,
},
);
const { preselectShippingMethod } = useShippingMethod();
Comment on lines 10 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
);
const { preselectShippingMethod } = useShippingMethod();
);
const { preselectShippingMethod } = useShippingMethod();

await preselectShippingMethod();

const { totalPrice, shippingTotal, cart } = useCart();

const netPrice = computed(() => {
Expand Down
27 changes: 27 additions & 0 deletions composables/useShippingMethod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { Schemas } from '@shopware/api-client/api-types';
import { useCart, useCheckout } from "#build/imports";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { useCart, useCheckout } from "#build/imports";

I dont think we need these imports, since we are not in the module


export function useShippingMethod() {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

const preselectShippingMethod = async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to rename this function to "preselectShippingMethodFallback", since it serves as the fallback in case the usual preselected one is not available (and doesnt include the actual logic for the preselection in the non fallback case)

const { getShippingMethods, setShippingMethod, selectedShippingMethod } = useCheckout();
const { refreshCart } = useCart();

const shippingMethods = await getShippingMethods();

const isPreselectedShippingAvailable = shippingMethods.value.find((method: Schemas['ShippingMethod']) => method.id === selectedShippingMethod.value.id);

if (isPreselectedShippingAvailable) {
return;
}

const cheapestMethod = shippingMethods.value.reduce((lowest : Schemas['ShippingMethod'], current: Schemas['ShippingMethod']) => {
return current.prices?.[0].currencyPrice?.[0].gross < lowest.prices?.[0].currencyPrice?.[0].gross ? current : lowest;
});

await setShippingMethod({id: cheapestMethod.id});
await refreshCart();
}

return { preselectShippingMethod };
}