强制同一账单/发货地址,当购物车价值大于$500时

huangapple go评论70阅读模式
英文:

Force same billing/shipping when cart value > $500

问题

隐藏复选框li当价格> 500时。

英文:

I have a checkout page which I'd like to force users to use the same billing/shipping when their cart value is above $500. On the checkout page there is an option to have billing address match the shipping address, I want to hide that option above $500 and set it to default.

Full shipping form:

<li class="checkout-step optimizedCheckout-checkoutStep checkout-step--shipping"><div class="checkout-view-header"><div class="stepHeader is-readonly"><div class="stepHeader-figure stepHeader-column"><div class="icon stepHeader-counter optimizedCheckout-step"></div><h2 class="stepHeader-title optimizedCheckout-headingPrimary">Shipping</h2></div><div class="stepHeader-body stepHeader-column optimizedCheckout-contentPrimary" data-test="step-info"></div></div></div><div aria-busy="false" class="checkout-view-content checkout-view-content-enter-done"><div class="loading-skeleton"><div class="checkout-form"><div class="form-legend-container"><legend class="form-legend optimizedCheckout-headingSecondary" data-test="shipping-address-heading">Shipping Address</legend></div><div><form autocomplete="on" novalidate=""><fieldset class="form-fieldset"><div class="form-body"><fieldset id="checkoutShippingAddress" class="form-fieldset"><div class="form-body"><div><fieldset class="form-fieldset"><div class="form-body"><div class="checkout-address"><div class="dynamic-form-field dynamic-form-field--countryCode"><div class="form-field"><label for="countryCodeInput" id="countryCodeInput-label" class="form-label optimizedCheckout-form-label">Country</label><div class="dropdown-chevron"><div class="icon"><svg height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M16.59 8.59L12 13.17 7.41 8.59 6 10l6 6 6-6z"></path></svg></div></div><select aria-labelledby="countryCodeInput-label countryCodeInput-field-error-message" autocomplete="country" class="form-select optimizedCheckout-form-select" data-test="countryCodeInput-select" id="countryCodeInput" name="shippingAddress.countryCode"><option value="">Select a country</option><option value="GB">United Kingdom</option><option value="US">United States</option></select></div></div><div class="dynamic-form-field dynamic-form-field--firstName"><div class="form-field"><label for="firstNameInput" id="firstNameInput-label" class="form-label optimizedCheckout-form-label">First Name</label><input aria-labelledby="firstNameInput-label firstNameInput-field-error-message" autocomplete="given-name" id="firstNameInput" type="text" class="form-input optimizedCheckout-form-input" name="shippingAddress.firstName" data-test="firstNameInput-text" value=""></div></div><div class="dynamic-form-field dynamic-form-field--lastName"><div class="form-field"><label for="lastNameInput" id="lastNameInput-label" class="form-label optimizedCheckout-form-label">Last Name</label><input aria-labelledby="lastNameInput-label lastNameInput-field-error-message" autocomplete="family-name" id="lastNameInput" type="text" class="form-input optimizedCheckout-form-input" name="shippingAddress.lastName" data-test="lastNameInput-text" value=""></div></div><div class="dynamic-form-field dynamic-form-field--addressLine1"><div class="form-field"><label for="addressLine1Input" id="addressLine1Input-label" class="form-label optimizedCheckout-form-label">Address</label><input aria-labelledby="addressLine1Input-label addressLine1Input-field-error-message" autocomplete="address-line1" id="addressLine1Input" type="text" class="form-input optimizedCheckout-form-input" name="shippingAddress.address1" data-test="addressLine1Input-text" value=""></div></div><div class="dynamic-form-field dynamic-form-field--addressLine2"><div class="form-field"><label for="addressLine2Input" id="addressLine2Input-label" class="form-label optimizedCheckout-form-label">Apartment/Suite/Building <small class="optimizedCheckout-contentSecondary">(Optional)</small></label><input aria-labelledby="addressLine2Input-label addressLine2Input-field-error-message" autocomplete="address-line2" id="addressLine2Input" type="text" class="form-input optimizedCheckout-form-input" name="shippingAddress.address2" data-test="addressLine2Input-text" value=""></div></div><div class="dynamic-form-field dynamic-form-field--company"><div class="form-field"><label for="companyInput" id="companyInput-label" class="form-label optimizedCheckout-form-label">Company Name <small class="optimizedCheckout-contentSecondary">(Optional)</small></label><input aria-labelledby="companyInput-label companyInput-field-error-message" autocomplete="organization" id="companyInput" type="text" class="form-input optimizedCheckout-form-input" name="shippingAddress.company" data-test="companyInput-text" value=""></div></div><div class="dynamic-form-field dynamic-form-field--city"><div class="form-field"><label for="cityInput" id="cityInput-label" class="form-label optimizedCheckout-form-label">City</label><input aria-labelledby="cityInput-label cityInput-field-error-message" autocomplete="address-level2" id="cityInput" type="text" class="form-input optimizedCheckout-form-input" name="shippingAddress.city" data-test="cityInput-text" value=""></div></div><div class="dynamic-form-field dynamic-form-field--provinceCode"><div class="form-field"><label for="provinceCodeInput" id="provinceCodeInput-label" class="form-label optimizedCheckout-form-label">State/Province</label><div class="dropdown-chevron"><div class="icon"><svg height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M16.59 8.59L12 13.17 7.41 8.59 6 10l6 6 6-6z"></path></svg></div></div><select aria-labelledby="provinceCodeInput-label provinceCodeInput-field-error-message" autocomplete="address-level1" class="form-select optimizedCheckout-form-select" data-test="provinceCodeInput-select" id="provinceCodeInput" name="shippingAddress.stateOrProvinceCode"><option value="">Select a state</option><option value="AL">Alabama</option></select></div></div><div class="dynamic-form-field dynamic-form-field--postCode"><div class="form-field"><label for="postCodeInput" id="postCodeInput-label" class="form-label optimizedCheckout-form-label">Postal Code</label><input aria-labelledby="postCodeInput-label postCodeInput-field-error-message" autocomplete="postal-code" id="postCodeInput" type="text" class="form-input optimizedCheckout-form-input" name="shippingAddress.postalCode" data-test="postCodeInput-text" value=""></div></div><div class="dynamic-form-field dynamic-form-field--phone"><div class="form-field"><label for="phoneInput" id="phoneInput-label" class="form-label optimizedCheckout-form-label">Phone Number</label><input aria-labelledby="phoneInput-label phoneInput-field-error-message" autocomplete="tel" id="phoneInput" type="tel" class="form-input optimizedCheckout-form-input" name="shippingAddress.phone" data-test="phoneInput-text" value=""></div></div></div></div></fieldset></div></div></fieldset><div class="form-body"><div class="form-field"><input id="sameAsBilling" type="checkbox" class="form-checkbox optimizedCheckout-form-checkbox" name="billingSameAsShipping" value="true" checked=""><label for="sameAsBilling" class="form-label optimizedCheckout-form-label">My billing address is the same as my shipping address.</label></div></div></div></fieldset><fieldset id="checkout-shipping-options" class="form-fieldset"><legend class="form-legend optimizedCheckout-headingSecondary">Shipping Method</legend><div class="form-body"><div class="loading-skeleton"><div class="shippingOptions-panel optimizedCheckout-overlay"><p aria-live="polite" class="shippingOptions-panel-message optimizedCheckout-primaryContent" role="alert">Please enter a shipping address in order to see shipping quotes</p></div></div></div></fieldset><div class="form-actions"><button id="checkout-shipping-continue" class="button button--primary optimizedCheckout-buttonPrimary" type="submit">Continue</button></div></form></div></div></div></div></li>

Checkbox only portion of above code, this is what I want hidden:

<div class="form-field"><input id="sameAsBilling" type="checkbox" class="form-checkbox optimizedCheckout-form-checkbox" name="billingSameAsShipping" value="true" checked=""><label for="sameAsBilling" class="form-label optimizedCheckout-form-label">My billing address is the same as my shipping address.</label></div>

Cart total section - this is where I pull the value to determine if it is greater than $500:

<section class="cart-section optimizedCheckout-orderSummary-cartSection"><div data-test="cart-total"><div aria-live="polite" class="cart-priceItem optimizedCheckout-contentPrimary cart-priceItem--total"><span class="cart-priceItem-label"><span data-test="cart-price-label">Total (USD)  </span></span><span class="cart-priceItem-value"><span data-test="cart-price-value">$1,915.77</span></span></div></div></section>

My JS:

var selection = document.querySelector('.checkout-address') !== null;
if (selection){
const myNodelist = document.getElementsByClassName("cart-priceItem--total");
const role = document.getElementsByClassName('form-field');
if (Number(myNodelist[0].textContent.replaceAll("$","").replaceAll(",","").replaceAll("Total (USD)  ","")) > 500){
role[10].style.display = 'none';
}
}

Goal = Hide checkbox li when price > 500.

答案1

得分: 1

我认为实现这个的最简单方式是:

  1. 禁用用于输入邮寄地址的输入框。
  2. 选中与邮寄地址对应的复选框来匹配帐单地址。
  3. 禁用第2步中的复选框。

这是一些可以实现这一点的代码:

// 1. 禁用用于输入邮寄地址的输入框。
let inputElementToDisable = document.getElementById("shipping-address-input");
inputElementToDisable.setAttribute("disabled", "true");
// 2. 选中与邮寄地址对应的复选框来匹配帐单地址。
let checkbox = document.getElementById("chkbox-that-matches-ship-to-bill");
while (checkbox.checked === false) {
checkbox.click();
}
//  3. 禁用第2步中的复选框。
checkBox.setAttribute("disabled", "true");

考虑到你评论中的代码以及 The KVNB 的评论,以下是用于检查购物车值并在值大于 500 时选中和禁用复选框的代码:

// from The KNVB
var price = document.querySelector('[data-test="cart-price-value"]');
price = Number(price.textContent.replaceAll(",", "").replaceAll("$", ""));
// 检查复选框并在价格大于 500 时禁用它
if (price > 500) {
let checkbox = document.getElementById("sameAsBilling");
while (checkbox.checked === false) {
checkbox.click();
}
checkbox.setAttribute("disabled", "true");
}
英文:

I think the simplest way to do this is to:

  1. Disable the input(s) for shipping address.
  2. Check the checkbox that matches the shipping address to the billing address.
  3. Disable the checkbox in 2.

Here's some code that could do this:

// 1. Disable the input(s) for shipping address.
let inputElementToDisable = document.getElementById("shipping-address-input");
inputElementToDisable.setAttribute("disabled", "true");
// 2. Check the checkbox that matches the shipping address to the billing address.
let checkbox = document.getElementById("chkbox-that-matches-ship-to-bill");
while (checkbox.checked === false) {
checkbox.click();
}
//  3. Disable the checkbox in 2.
checkBox.setAttribute("disabled", "true");

Considering the code in your comment and the comment from The KVNB, here's code for checking the cart value, and checking and disabling the checkbox when the value is > 500:

// from The KNVB
var price = document.querySelector('[data-test="cart-price-value"]');
price = Number(price.textContent.replaceAll(",","").replaceAll("$",""));
// check checkbox and disable when price > 500
if (price > 500) {
let checkbox = document.getElementById("sameAsBilling");
while (checkbox.checked === false) {
checkbox.click();
}
checkbox.setAttribute("disabled", "true");
}

huangapple
  • 本文由 发表于 2023年6月13日 08:36:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461054.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定