From 27b517d1dd29af9428995c0e6b4c43dcc1eedd47 Mon Sep 17 00:00:00 2001 From: Jade Geels Date: Wed, 15 Jan 2025 14:51:57 +0000 Subject: [PATCH] Dynamic product listing image and redirect + swatch logic fix (#698) --- resources/js/components/Product/AddToCart.vue | 25 +++++++-- .../views/listing/partials/item.blade.php | 56 ++++++++++--------- .../listing/partials/item/addtocart.blade.php | 34 ++++++----- 3 files changed, 65 insertions(+), 50 deletions(-) diff --git a/resources/js/components/Product/AddToCart.vue b/resources/js/components/Product/AddToCart.vue index eca58d1c7..9534d40c2 100644 --- a/resources/js/components/Product/AddToCart.vue +++ b/resources/js/components/Product/AddToCart.vue @@ -59,11 +59,7 @@ export default { methods: { async add() { - if ( - window.location.pathname !== this.product.url && - (this.product?.has_options || - ('children' in this.product && Object.values(this.product.children).length && !config.show_swatches)) - ) { + if (this.shouldRedirectToProduct) { Turbo.visit(window.url(this.product.url)) return } @@ -262,6 +258,25 @@ export default { }, computed: { + currentThumbnail: function () { + return this.simpleProduct?.thumbnail || this.simpleProduct?.images?.[0] || this.product?.thumbnail + }, + + shouldRedirectToProduct: function () { + // Never redirect if we're already on the product page + if (window.location.pathname === this.product.url) { + return false + } + + // Products with product options always have to be set on the product page + if (this.product?.has_options) { + return true + } + + // Check if all super_attributes have an option selected + return Object.keys(this.product?.super_attributes).join(',') !== Object.keys(this.options).join(',') + }, + simpleProduct: function () { var product = this.product diff --git a/resources/views/listing/partials/item.blade.php b/resources/views/listing/partials/item.blade.php index ca0662882..c21acda9b 100644 --- a/resources/views/listing/partials/item.blade.php +++ b/resources/views/listing/partials/item.blade.php @@ -1,31 +1,33 @@ diff --git a/resources/views/listing/partials/item/addtocart.blade.php b/resources/views/listing/partials/item/addtocart.blade.php index ad53e05bc..1574e74d2 100644 --- a/resources/views/listing/partials/item/addtocart.blade.php +++ b/resources/views/listing/partials/item/addtocart.blade.php @@ -1,21 +1,19 @@ - -
-
-
- @{{ (addToCart.simpleProduct.special_price || addToCart.simpleProduct.price) | price }} -
-
- @{{ addToCart.simpleProduct.price | price }} -
+ +
+
+ @{{ (addToCart.simpleProduct.special_price || addToCart.simpleProduct.price) | price }}
+
+ @{{ addToCart.simpleProduct.price | price }} +
+
-

- @lang('Sorry! This product is currently out of stock.') -

-
- @include('rapidez::listing.partials.item.super_attributes') +

+ @lang('Sorry! This product is currently out of stock.') +

+
+ @includeWhen(Rapidez::config('catalog/frontend/show_swatches_in_product_list', 1), 'rapidez::listing.partials.item.super_attributes') - -
- - + +
+