-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(storefront): BCTHEME-11 hide product options that are out of stoc…
…k on cart
- Loading branch information
1 parent
4d4f9f2
commit 8d941cd
Showing
8 changed files
with
592 additions
and
399 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import utils from '@bigcommerce/stencil-utils'; | ||
import ProductDetailsBase, { optionChangeDecorator } from './product-details-base'; | ||
import { isEmpty } from 'lodash'; | ||
import { isBrowserIE, convertIntoArray } from './utils/ie-helpers'; | ||
|
||
export default class CartItemDetails extends ProductDetailsBase { | ||
constructor($scope, context, productAttributesData = {}) { | ||
super($scope, context); | ||
|
||
const $form = $('#CartEditProductFieldsForm', this.$scope); | ||
const $productOptionsElement = $('[data-product-attributes-wrapper]', $form); | ||
const hasOptions = $productOptionsElement.html().trim().length; | ||
const hasDefaultOptions = $productOptionsElement.find('[data-default]').length; | ||
|
||
$productOptionsElement.on('change', e => { | ||
this.setProductVariant(); | ||
}); | ||
|
||
const optionChangeCallback = optionChangeDecorator.call(this, hasDefaultOptions); | ||
|
||
// Update product attributes. Also update the initial view in case items are oos | ||
// or have default variant properties that change the view | ||
if ((isEmpty(productAttributesData) || hasDefaultOptions) && hasOptions) { | ||
const productId = this.context.productForChangeId; | ||
|
||
utils.api.productAttributes.optionChange(productId, $form.serialize(), 'products/bulk-discount-rates', optionChangeCallback); | ||
} else { | ||
this.updateProductAttributes(productAttributesData); | ||
} | ||
} | ||
|
||
setProductVariant() { | ||
const unsatisfiedRequiredFields = []; | ||
const options = []; | ||
|
||
$.each($('[data-product-attribute]'), (index, value) => { | ||
const optionLabel = value.children[0].innerText; | ||
const optionTitle = optionLabel.split(':')[0].trim(); | ||
const required = optionLabel.toLowerCase().includes('required'); | ||
const type = value.getAttribute('data-product-attribute'); | ||
|
||
if ((type === 'input-file' || type === 'input-text' || type === 'input-number') && value.querySelector('input').value === '' && required) { | ||
unsatisfiedRequiredFields.push(value); | ||
} | ||
|
||
if (type === 'textarea' && value.querySelector('textarea').value === '' && required) { | ||
unsatisfiedRequiredFields.push(value); | ||
} | ||
|
||
if (type === 'date') { | ||
const isSatisfied = Array.from(value.querySelectorAll('select')).every((select) => select.selectedIndex !== 0); | ||
|
||
if (isSatisfied) { | ||
const dateString = Array.from(value.querySelectorAll('select')).map((x) => x.value).join('-'); | ||
options.push(`${optionTitle}:${dateString}`); | ||
|
||
return; | ||
} | ||
|
||
if (required) { | ||
unsatisfiedRequiredFields.push(value); | ||
} | ||
} | ||
|
||
if (type === 'set-select') { | ||
const select = value.querySelector('select'); | ||
const selectedIndex = select.selectedIndex; | ||
|
||
if (selectedIndex !== 0) { | ||
options.push(`${optionTitle}:${select.options[selectedIndex].innerText}`); | ||
|
||
return; | ||
} | ||
|
||
if (required) { | ||
unsatisfiedRequiredFields.push(value); | ||
} | ||
} | ||
|
||
if (type === 'set-rectangle' || type === 'set-radio' || type === 'swatch' || type === 'input-checkbox' || type === 'product-list') { | ||
const checked = value.querySelector(':checked'); | ||
if (checked) { | ||
const getSelectedOptionLabel = () => { | ||
const productVariantslist = convertIntoArray(value.children); | ||
const matchLabelForCheckedInput = inpt => inpt.dataset.productAttributeValue === checked.value; | ||
return productVariantslist.filter(matchLabelForCheckedInput)[0]; | ||
}; | ||
if (type === 'set-rectangle' || type === 'set-radio' || type === 'product-list') { | ||
const label = isBrowserIE ? getSelectedOptionLabel().innerText.trim() : checked.labels[0].innerText; | ||
if (label) { | ||
options.push(`${optionTitle}:${label}`); | ||
} | ||
} | ||
|
||
if (type === 'swatch') { | ||
const label = isBrowserIE ? getSelectedOptionLabel().children[0] : checked.labels[0].children[0]; | ||
if (label) { | ||
options.push(`${optionTitle}:${label.title}`); | ||
} | ||
} | ||
|
||
if (type === 'input-checkbox') { | ||
options.push(`${optionTitle}:Yes`); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (type === 'input-checkbox') { | ||
options.push(`${optionTitle}:No`); | ||
} | ||
|
||
if (required) { | ||
unsatisfiedRequiredFields.push(value); | ||
} | ||
} | ||
}); | ||
|
||
let productVariant = unsatisfiedRequiredFields.length === 0 ? options.sort().join(', ') : 'unsatisfied'; | ||
const view = $('.modal-header-title'); | ||
|
||
if (productVariant) { | ||
productVariant = productVariant === 'unsatisfied' ? '' : productVariant; | ||
if (view.attr('data-event-type')) { | ||
view.attr('data-product-variant', productVariant); | ||
} else { | ||
const productName = view.html().match(/'(.*?)'/)[1]; | ||
const card = $(`[data-name="${productName}"]`); | ||
card.attr('data-product-variant', productVariant); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Hide or mark as unavailable out of stock attributes if enabled | ||
* @param {Object} data Product attribute data | ||
*/ | ||
updateProductAttributes(data) { | ||
super.updateProductAttributes(data); | ||
|
||
this.$scope.find('.modal-content').removeClass('hide-content'); | ||
} | ||
} |
Oops, something went wrong.