下記のように埋め込みコードをカスタマイズされるとどうでしょうか?
ShopifyBuy.UI.onReady(client).then(function (ui) {
console.log(ui);
ui.createComponent('product', {
id: '6574139670688',
node: document.getElementById('product-component-1653907257261'),
moneyFormat: '%C2%A5%7B%7Bamount_no_decimals%7D%7D',
options: {
"product": {
"styles": {
"product": {
"@media (min-width: 601px)": {
"max-width": "calc(25% - 20px)",
"margin-left": "20px",
"margin-bottom": "50px"
}
},
},
"text": {
"button": "Add to cart"
}
},
"productSet": {
"styles": {
"products": {
"@media (min-width: 601px)": {
"margin-left": "-20px"
}
}
}
},
カスタマイズ状況によってコードは異なりますが、上記のコードを下記のようにします。
ShopifyBuy.UI.onReady(client).then(function (ui) {
console.log(ui);
ui.createComponent('product', {
id: '6574139670688',
node: document.getElementById('product-component-1653907257261'),
moneyFormat: '%C2%A5%7B%7Bamount_no_decimals%7D%7D',
options: {
"product": {
"styles": {
"product": {
"@media (min-width: 601px)": {
"max-width": "calc(25% - 20px)",
"margin-left": "20px",
"margin-bottom": "50px"
}
},
},
"text": {
"button": "Add to cart"
},
"events": {
"afterRender": function(component) {
const selectElements = component.node.childNodes[0].contentWindow.document.querySelectorAll('[data-element="option.select"]');
const optionElements = selectElements[0].querySelectorAll('option');
component.model.variants.forEach((variant,index) => {
if (!variant.available) {
optionElements[index].innerText = optionElements[index].innerText + " (Sold Out)";
}
});
}
}
},
"productSet": {
"styles": {
"products": {
"@media (min-width: 601px)": {
"margin-left": "-20px"
}
}
}
},
具体的に言うと、
options.productに下記を追加しました。
"events": {
"afterRender": function(component) {
const selectElements = component.node.childNodes[0].contentWindow.document.querySelectorAll('[data-element="option.select"]');
const optionElements = selectElements[0].querySelectorAll('option');
component.model.variants.forEach((variant,index) => {
if (!variant.available) {
optionElements[index].innerText = optionElements[index].innerText + " (Sold Out)";
}
});
}
}
なお、下記は注意点です。
- オプションが1つしか存在しないことを前提としたコードになっている。(アパレルの「色」と「サイズ」のように、オプションが2つ以上存在しない)
- とり急ぎ、試してみただけなので、思わぬ不具合があるかもしれない。
もし、オプションが2つ以上存在する場合は、上記のコードは全く意味をなしません。
オプションが2つ以上存在する場合の実装が少し大変なので割愛させていただきましたが、
もし必要であれば、このスレッドにご返信ください。
即答はできないかもしれませんが、カスタマイズを考えてみます。
ご参考まで。
(キュー田辺)