Skip to content

Commit

Permalink
[FEATURE] JS changes available BE variant stock
Browse files Browse the repository at this point in the history
With activated stock handling for BE variants
the JavaScript which changes the prices does
also change change the available stock.

Requirements:
-This requires that the select view contains
a data atrribute 'data-available-stock' with
the stock for the BE variant.
- Furthermore some HTML has to be present which
will be used to insert the available stock.

Both changes will be made in a commit in
EXT:cart_products.

!!!
This commit changes also the buggy behaviour that
reselecting "Please choose..." from the select
field shows the price of the last chosen item.
Instead a ` ` will be inserted (to display
no price while avoiding 'jumping' layout.

Fixes: #487
  • Loading branch information
rintisch committed May 3, 2024
1 parent c9d9eec commit 21cda83
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
27 changes: 22 additions & 5 deletions Build/Sources/JavaScript/change_be_variant.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import { dispatchCustomEvent } from './helper/dispatch_custom_event';

document.addEventListener('DOMContentLoaded', () => {
function setValue (parentElement, targetElementClass, value) {
function setValue (parentElement, targetElementClass, spanClass, value) {

const targetElement = parentElement.querySelector(targetElementClass);
if (targetElement) {
targetElement.querySelector('.price').innerHTML = value;
const spanElement = targetElement.querySelector(spanClass);

if(typeof(value) === 'undefined'){
spanElement.innerHTML = ' ';
return
}

if (spanClass !== '.stock') {
spanElement.innerHTML = value;
} else {
const template = Number(value) === 1 ? spanElement.dataset.stockSingular : spanElement.dataset.stockPlural;
const placeholder = '%1$s';
const text = template.replace(placeholder, value);
spanElement.innerHTML = text;
}
}
}

Expand All @@ -16,10 +31,12 @@ document.addEventListener('DOMContentLoaded', () => {
const { specialPrice } = selectedOption.dataset;
const { regularPrice } = selectedOption.dataset;
const { specialPricePercentageDiscount } = selectedOption.dataset;
const { availableStock } = selectedOption.dataset;

setValue(productPrice, '.special_price', specialPrice);
setValue(productPrice, '.regular_price', regularPrice);
setValue(productPrice, '.special_price_percentage_discount', specialPricePercentageDiscount);
setValue(productPrice, '.special_price', '.price', specialPrice);
setValue(productPrice, '.regular_price', '.price', regularPrice);
setValue(productPrice, '.special_price_percentage_discount', '.price', specialPricePercentageDiscount);
setValue(productPrice, '.available_stock', '.stock', availableStock);

dispatchCustomEvent(
'extcode:be-variant-was-changed',
Expand Down
24 changes: 19 additions & 5 deletions Resources/Public/JavaScript/change_be_variant.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,22 @@

// JavaScript/change_be_variant.js
document.addEventListener("DOMContentLoaded", () => {
function setValue(parentElement, targetElementClass, value) {
function setValue(parentElement, targetElementClass, spanClass, value) {
const targetElement = parentElement.querySelector(targetElementClass);
if (targetElement) {
targetElement.querySelector(".price").innerHTML = value;
const spanElement = targetElement.querySelector(spanClass);
if (typeof value === "undefined") {
spanElement.innerHTML = " ";
return;
}
if (spanClass !== ".stock") {
spanElement.innerHTML = value;
} else {
const template = Number(value) === 1 ? spanElement.dataset.stockSingular : spanElement.dataset.stockPlural;
const placeholder = "%1$s";
const text = template.replace(placeholder, value);
spanElement.innerHTML = text;
}
}
}
const productPrice = document.querySelector("#product-price");
Expand All @@ -26,9 +38,11 @@
const { specialPrice } = selectedOption.dataset;
const { regularPrice } = selectedOption.dataset;
const { specialPricePercentageDiscount } = selectedOption.dataset;
setValue(productPrice, ".special_price", specialPrice);
setValue(productPrice, ".regular_price", regularPrice);
setValue(productPrice, ".special_price_percentage_discount", specialPricePercentageDiscount);
const { availableStock } = selectedOption.dataset;
setValue(productPrice, ".special_price", ".price", specialPrice);
setValue(productPrice, ".regular_price", ".price", regularPrice);
setValue(productPrice, ".special_price_percentage_discount", ".price", specialPricePercentageDiscount);
setValue(productPrice, ".available_stock", ".stock", availableStock);
dispatchCustomEvent(
"extcode:be-variant-was-changed",
{
Expand Down

0 comments on commit 21cda83

Please sign in to comment.