Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

YSHOP2-682 : COD themes > The price isn't updated when the variants are radio buttons #359

Merged
merged 9 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion assets/product.css
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@
border-radius: 5px;
transition: width 1s ease-in-out;
}
.yc-single-product .product-details .product-options .active {
.yc-single-product .product-details .product-options .yc-colors-select .active,
.yc-single-product .product-details .product-options .yc-options-list .active,
.yc-single-product .product-details .product-options .yc-image-options-list .active {
border: 1px solid #000000;
}
.yc-single-product .product-details .product-description {
Expand Down
44 changes: 18 additions & 26 deletions assets/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ function setElementActive(element) {
element.classList.add('active');
}

/**
* Sets the variant id in the hidden input field of the product form
* @param {HTMLElement} parentSection
* @param {String} id variant id
*/
function setVariant(parentSection, id) {
const variantIdInput = parentSection.querySelector('#variantId');

variantIdInput.value = id;
}

/**
* Sets default options for a product
* @param {HTMLElement} parentSection
Expand All @@ -170,7 +181,9 @@ function selectDefaultOptions(parentSection) {
option.querySelector('.yc-options-item').classList.add('active');
break;
case 'radio_buttons':
option.querySelector('input').checked = true;
const radioLabel = option.querySelector('.yc-radio-buttons');
radioLabel.classList.add('active');
radioLabel.querySelector('input[type="radio"]').checked = true;
break;
case 'image_based_buttons':
option.querySelector('.yc-image-options-item').classList.add('active');
Expand Down Expand Up @@ -215,7 +228,7 @@ function getSelectedOptions(parentSection) {
break;
case 'radio_buttons':
selectedOptions[optionName] =
option.querySelector('input:checked')?.value;
option.querySelector('.yc-radio-buttons.active input[type="radio"]')?.value;
break;
case 'image_based_buttons':
selectedOptions[optionName] = option.querySelector(
Expand Down Expand Up @@ -252,24 +265,13 @@ function getSelectedVariant(parentSection) {
});
}

/**
* Sets the variant id in the hidden input field of the product form
* @param {HTMLElement} parentSection
* @param {String} id variant id
*/
function setVariant(parentSection, id) {
const variantIdInput = parentSection.querySelector('#variantId');

variantIdInput.value = id;
}

/**
* Updates product details after variant change
* @param {HTMLElement} parentSection
* @param {String} image
* @param {String} price
*/
function updateProductDetails(parentSection, image, price, variations) {
function updateProductDetails(parentSection, image, price) {
if (image) {
const mainImgs = parentSection.querySelectorAll('.main-image');

Expand All @@ -292,14 +294,6 @@ function updateProductDetails(parentSection, image, price, variations) {
}
})
}

if(variations) {
const productVariations = parentSection.querySelectorAll('.product-variations');

productVariations.forEach(el => {
el.innerHTML = Object.values(variations).join(' - ')
})
}
}

/**
Expand Down Expand Up @@ -478,7 +472,7 @@ function showSelectedVariants() {
variantOption = createAndSetText(variantName, colorBaseButton, 'colored-button').element;
break;
case 'radio_buttons':
const radioButton = variant.querySelector('input:checked')?.value;
const radioButton = variant.querySelector('.yc-radio-buttons.active input[type="radio"]')?.value;
variantOption = createAndSetText(variantName, radioButton).element;
break;
case 'dropdown':
Expand Down Expand Up @@ -537,7 +531,7 @@ function setup() {

singleProductSections.forEach((section) => {
const productDetails = section.querySelector('.product-options');
const variant = variants[0]
const variant = variants[0];

updateProductDetails(
section,
Expand All @@ -549,8 +543,6 @@ function setup() {
if (productDetails) {
const observer = new MutationObserver(() => {
const selectedVariant = getSelectedVariant(section);
const variantIdInput = section.querySelector('#variantId');
variantIdInput.value = selectedVariant.id;

updateProductDetails(
section,
Expand Down
4 changes: 2 additions & 2 deletions snippets/product-variants.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
{% when 'radio_buttons' %}
<div class='yc-radio'>
{% for item in option.values %}
<label class='yc-radio-buttons' for='{{ item }}'>
<input type='radio' id='{{ item }}' name='{{ option.name }}' value='{{ item }}'>
<label class='yc-radio-buttons' for='{{ item }}' onclick='setElementActive(this)'>
<input type='radio' id='{{ item }}' name='{{ option.name }}' value='{{ item }}'>
<span class='checkmark'></span>
<span class='label-text'>{{ item }}</span>
</label>
Expand Down
9 changes: 7 additions & 2 deletions styles/product.scss
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,13 @@
}
}
}
.active {
border: 1px solid #000000;

.yc-colors-select,
.yc-options-list,
.yc-image-options-list {
.active {
border: 1px solid #000000;
}
}
}
.product-description {
Expand Down