From 3ad0638875513a4f015f4b1699cd13cb58ec57c2 Mon Sep 17 00:00:00 2001 From: Moggach Date: Wed, 10 Jul 2024 16:47:33 +0000 Subject: [PATCH 1/2] if there is more than one product variations display them in a drop down selector --- app/templates/app/includes/cart_options.html | 61 ++++++++++---------- frontend/controllers/product-controller.ts | 12 +++- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/app/templates/app/includes/cart_options.html b/app/templates/app/includes/cart_options.html index a2026860..81af21d7 100644 --- a/app/templates/app/includes/cart_options.html +++ b/app/templates/app/includes/cart_options.html @@ -1,39 +1,42 @@ {% load djmoney books %} -
{% if not product.variants %}
- + Loading purchase options...
+ {% elif product.variants|length == 1 %} +
+ + {% with variant=product.variants.0 %} +

+ {% if variant.title != "Default Title" %}{{ variant.title }} - {% endif %} + {% money_localize variant.price "GBP" %} + {% if variant.compare_at_price is not None %} + {% money_localize variant.compare_at_price "GBP" %} + {% endif %} +

+ {% endwith %} +
+
+ +
{% else %} - {% for variant in product.variants %} -
- {% if variant.title != "Default Title" %}
{{ variant.title }}
{% endif %} - {% if variant.inventory_quantity == 0 and variant.inventory_policy == 'deny' %} - Out of Stock - {% elif loading %} - - Loading... - {% else %} - - {% endif %} -
- {% if variant.compare_at_price is not None %} - {% money_localize variant.price "GBP" %} {% money_localize variant.compare_at_price "GBP" %} - {% else %} +
+ +
+
+ +
{% endif %}
diff --git a/frontend/controllers/product-controller.ts b/frontend/controllers/product-controller.ts index 67afda28..40ba088a 100644 --- a/frontend/controllers/product-controller.ts +++ b/frontend/controllers/product-controller.ts @@ -8,8 +8,11 @@ import { isAfter } from "date-fns"; export default class ShopifyBuyControllerBase extends Controller { // Targets - static targets = ["template"]; + static targets = ["template", "variantSelector", "addToCartButton"]; + public templateTargets?: HTMLElement[]; + public variantSelectorTarget!: HTMLSelectElement; + public addToCartButtonTarget!: HTMLButtonElement; // Values static values = { @@ -40,6 +43,11 @@ export default class ShopifyBuyControllerBase extends Controller { this.getCart(); } + updateAddToCartButton() { + const selectedVariantId = this.variantSelectorTarget.value; + this.addToCartButtonTarget.setAttribute('data-product-variant-id-param', selectedVariantId); + } + private LOCALSTORAGE_CHECKOUT_ID = "checkoutId"; get checkoutId() { @@ -320,4 +328,4 @@ function currency( function length(arr: any[]) { if (!arr) return 0; return arr.length || 0; -} +} \ No newline at end of file From 88e59f94254c3841d5f8e32fd344c422c1a586fe Mon Sep 17 00:00:00 2001 From: Moggach Date: Thu, 11 Jul 2024 13:06:19 +0000 Subject: [PATCH 2/2] add inventory handling and disabled button for each product variant in the dropdown --- app/templates/app/includes/cart_options.html | 2 +- frontend/controllers/product-controller.ts | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/templates/app/includes/cart_options.html b/app/templates/app/includes/cart_options.html index 81af21d7..a032c04a 100644 --- a/app/templates/app/includes/cart_options.html +++ b/app/templates/app/includes/cart_options.html @@ -25,7 +25,7 @@