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

Allow user to define variants and their values #127

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
259 changes: 141 additions & 118 deletions js/variant-selector-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const VariantSelector = (
quantity: 1,
color: undefined,
size: undefined,
attributes_definition: [],
attributes: {},
},
triggers: {
buyNow: "",
Expand Down Expand Up @@ -41,108 +43,110 @@ const VariantSelector = (
setLoading: () => {
const loading = document.createElement("div");
loading.innerHTML = `
<style>
.new__loader,
.new__loader:before,
.new__loader:after {
border-radius: 50%;
width: 2.5em;
height: 2.5em;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
-webkit-animation: load7 1.8s infinite ease-in-out;
animation: load7 1.8s infinite ease-in-out;
}
.new__loader {
color: #000000;
font-size: 10px;
margin: 10px auto 50px;
position: relative;
text-indent: -9999em;
-webkit-transform: translateZ(0);
-ms-transform: translateZ(0);
transform: translateZ(0);
-webkit-animation-delay: -0.16s;
animation-delay: -0.16s;
}
.new__loader:before,
.new__loader:after {
content: '';
position: absolute;
top: 0;
}
.new__loader:before {
left: -3.5em;
-webkit-animation-delay: -0.32s;
animation-delay: -0.32s;
}
.new__loader:after {
left: 3.5em;
}
@-webkit-keyframes load7 {
0%,
80%,
100% {
box-shadow: 0 2.5em 0 -1.3em;
}
40% {
box-shadow: 0 2.5em 0 0;
}
}
@keyframes load7 {
0%,
80%,
100% {
box-shadow: 0 2.5em 0 -1.3em;
}
40% {
box-shadow: 0 2.5em 0 0;
}
}

.loading-overlay {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(0, 0, 0, 0.7);
transition: opacity 500ms;
opacity: 1;
z-index: 111;
width: 100%;
}
.popup {
margin: 20% auto;
padding: 40px;
background: #fff;
border-radius: 5px;
width: 30%;
min-width: 300px;
position: relative;
transition: all 5s ease-in-out;
text-align: center;
}

.popup h2 {
margin-top: 0;
color: #333;
font-family: Tahoma, Arial, sans-serif;
}

.popup .content {
max-height: 30%;
overflow: auto;
}
</style>
<div class="loading-overlay">
<div class="popup">
<div class="new__loader"></div>
<div class="content">
Checking availability...
</div>
</div>
</div>`;
<style>
.new__loader,
.new__loader:before,
.new__loader:after {
border-radius: 50%;
width: 2.5em;
height: 2.5em;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
-webkit-animation: load7 1.8s infinite ease-in-out;
animation: load7 1.8s infinite ease-in-out;
}
.new__loader {
color: #000000;
font-size: 10px;
margin: 10px auto 50px;
position: relative;
text-indent: -9999em;
-webkit-transform: translateZ(0);
-ms-transform: translateZ(0);
transform: translateZ(0);
-webkit-animation-delay: -0.16s;
animation-delay: -0.16s;
}
.new__loader:before,
.new__loader:after {
content: '';
position: absolute;
top: 0;
}
.new__loader:before {
left: -3.5em;
-webkit-animation-delay: -0.32s;
animation-delay: -0.32s;
}
.new__loader:after {
left: 3.5em;
}
@-webkit-keyframes load7 {
0%,
80%,
100% {
box-shadow: 0 2.5em 0 -1.3em;
}
40% {
box-shadow: 0 2.5em 0 0;
}
}
@keyframes load7 {
0%,
80%,
100% {
box-shadow: 0 2.5em 0 -1.3em;
}
40% {
box-shadow: 0 2.5em 0 0;
}
}

.loading-overlay {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(0, 0, 0, 0.7);
transition: opacity 500ms;
opacity: 1;
z-index: 111;
width: 100%;
}
.popup {
margin: 20% auto;
padding: 40px;
background: #fff;
border-radius: 5px;
width: 30%;
min-width: 300px;
position: relative;
transition: all 5s ease-in-out;
text-align: center;
}

.popup h2 {
margin-top: 0;
color: #333;
font-family: Tahoma, Arial, sans-serif;
}

.popup .content {
max-height: 30%;
overflow: auto;
}
</style>

<div class="loading-overlay">
<div class="popup">
<div class="new__loader"></div>
<div class="content">
Checking availability...
</div>
</div>
</div>
`;

document.body.appendChild(loading);

Expand Down Expand Up @@ -240,32 +244,51 @@ const VariantSelector = (

if (!quantity) throw new Error("VariantSelector: No quantity");

const color =
typeof config.product.color === "function"
? config.product.color()
: config.product.color;
const { attributes_definition, attributes } = config.product;

if (attributes_definition.length && Object.keys(attributes).length) {
attributesToValue = attributes_definition.reduce(
(result, attributeName) => {
const variant = attributes[attributeName];
const value = typeof variant === "function" ? variant() : variant;

if (!value) {
throw new Error(`VariantSelector: No ${attributeName}`);
}

result[attributeName] = value;
return result;
},
{}
);
} else {
const color =
typeof config.product.color === "function"
? config.product.color()
: config.product.color;

if (!color) throw new Error("VariantSelector: No color");
if (!color) throw new Error("VariantSelector: No color");

const size =
typeof config.product.size === "function"
? config.product.size()
: config.product.size;
const size =
typeof config.product.size === "function"
? config.product.size()
: config.product.size;

if (!size) throw new Error("VariantSelector: No size");
if (!size) throw new Error("VariantSelector: No size");

attributesToValue[ATTRIBUTES.Color] = color;
attributesToValue[ATTRIBUTES.Size] = size;
}

Utils.setLoading();
RomuloDevelop marked this conversation as resolved.
Show resolved Hide resolved

const variantFound =
state.product.variations_definition.product_variations.find((variant) => {
const isTheRightVariant = variant.variation_attributes.every(
(attribute) => {
if (attribute.name === ATTRIBUTES.Color) {
return attribute.value === color;
}
if (attribute.name === ATTRIBUTES.Size) {
return attribute.value === size;
}
const value = attributesToValue[attribute.name];

if (value) return attribute.value === value;

return false;
}
Expand Down