FleetCart/Themes/Storefront/resources/assets/public/js/mixins/CartItemHelpersMixin.js

91 lines
2.2 KiB
JavaScript
Raw Normal View History

2023-12-03 14:07:47 +00:00
export default {
methods: {
productUrl({ product, variant }) {
return route("products.show", {
slug: product.slug,
...(variant !== null && {
variant: variant.uid,
}),
});
},
hasBaseImage({ item }) {
return item.base_image.length !== 0;
},
baseImage(cartItem) {
if (this.hasBaseImage(cartItem)) {
return cartItem.item.base_image.path;
}
return `${window.FleetCart.baseUrl}/build/assets/image-placeholder.png`;
},
optionValues(option) {
let values = [];
for (let value of option.values) {
values.push(value.label);
}
return values.join(", ");
},
maxQuantity({ item }) {
return item.is_in_stock && item.does_manage_stock ? item.qty : null;
},
exceedsMaxStock({ item, qty }) {
return item.does_manage_stock && item.qty < qty;
},
isQtyIncreaseDisabled(cartItem) {
return (
this.maxQuantity(cartItem) !== null &&
cartItem.qty >= cartItem.item.qty
);
},
changeQuantity(cartItem, qty) {
if (isNaN(qty) || qty < 1) {
qty = 1;
this.updateCart(cartItem, qty);
return;
}
cartItem.qty = qty;
if (this.exceedsMaxStock(cartItem)) {
qty = cartItem.item.qty;
this.updateCart(cartItem, qty);
return;
}
this.updateCart(cartItem, qty);
},
updateQuantity(cartItem, qty) {
if (isNaN(qty) || qty < 1) {
qty = 1;
cartItem.qty = 1;
return;
}
cartItem.qty = qty;
if (this.exceedsMaxStock(cartItem)) {
cartItem.qty = cartItem.item.qty;
return;
}
this.updateCart(cartItem, qty);
},
},
};