I created a sticky add to cart button following a guide by this guy here
I’ve used display: none; to hide a few elements in the Sticky Product Form leaving only the button.
I’ve also modified the sticky-product-form.js file with an intention to sync the sticky add to cart button with the regular one, partly because the original code given by the guy didn’t add the item to cart at all. Here is the modified version of the code below:
customElements.define(‘sticky-product-form’, class StickyProductForm extends HTMLElement {> > constructor() {> > super();> > }> > connectedCallback() {> > this.productForm = document.querySelector(‘product-form’);> > this.productStickyForm = document.querySelector(‘.product–sticky-form’);> > this.productFormBounds = {};> > this.onScrollHandler = this.onScroll.bind(this);> > window.addEventListener(‘scroll’, this.onScrollHandler, false);> > this.createObserver();> > // Sync variant and quantity inputs between forms> > this.syncInputs();> > }> > disconnectedCallback() {> > window.removeEventListener(‘scroll’, this.onScrollHandler);> > }> > createObserver() {> > let observer = new IntersectionObserver((entries, observer) => {> > this.productFormBounds = entries[0].isIntersecting;> > });> > observer.observe(this.productForm);> > }> > onScroll() {> > this.productFormBounds ? requestAnimationFrame(this.hide.bind(this)) : requestAnimationFrame(this.reveal.bind(this));> > }> > hide() {> > if (this.productStickyForm.classList.contains(‘product–sticky-form__active’)) {> > this.productStickyForm.classList.remove(‘product–sticky-form__active’);> > this.productStickyForm.classList.add(‘product–sticky-form__inactive’);> > }> > }> > reveal() {> > if (this.productStickyForm.classList.contains(‘product–sticky-form__inactive’)) {> > this.productStickyForm.classList.add(‘product–sticky-form__active’);> > this.productStickyForm.classList.remove(‘product–sticky-form__inactive’);> > }> > }> > syncInputs() {> > // Listen for changes on the main product form> > if (this.productForm) {> > this.productForm.addEventListener(‘change’, (event) => {> > const stickyFormInput = this.productStickyForm.querySelector(‘[name="’ + event.target.name + ‘"]’);> > if (stickyFormInput) {> > stickyFormInput.value = event.target.value;> > }> > });> > }> > // Listen for changes on the sticky product form> > if (this.productStickyForm) {> > this.productStickyForm.addEventListener(‘change’, (event) => {> > const mainFormInput = this.productForm.querySelector(‘[name="’ + event.target.name + ‘"]’);> > if (mainFormInput) {> > mainFormInput.value = event.target.value;> > }> > });> > }> > }> > });
But after I modified the code, it still doesn’t add the item to cart except it does sync the Sold Out status to the sticky button (not immediately, only after refreshing the page).
How can I fix this problem?jav