Wix : struggling to connect a dataset with a product using JS

I’m stuck trying to setup a custom bottle in Wix.

Let me explain this as quick as possible : I’m selling bottles that can be filled with four little glasses. There are twenty differents kinds of these. What I tried (hard since this morning) is to create ether four dropdown menus or four sliders that could allow users to choose the glasses they want in their custom bottle, and see the result in the preview bottle.

I upload a picture of an empty bottle, and I create a dataset with every single glasse. Each of them has its own picture, and a picture of where it should be displayed on the single bottle when the user click on it.

Here’s the page : https://www.caveduchardon.ch/studer

Here’s my code :

import wixWindow from 'wix-window';
import { cart } from 'wix-stores';

//-------------Global Variables-------------//
const NUMBER_OF_CHOICES = 4;
const productId = "5833e0c1-bd8a-1721-f2c7-f66b4ccbaf94";
let selectedOptions = {};

// Start with clean slate when page loads.
$w.onReady(function () {
    // Clear the customizations by calling the clearSelection() function.
    clearSelection();

    // Set the action that occurs when a user clicks the refresh button.
    $w('#refreshButton').onClick((event) => {
        // Clear the customizations by calling the clearSelection() function.
        clearSelection();
    })

    //-------------Repeaters Setup-------------//

    selectionRepeatersReady()

    //-------------Expand/Collapse Sections-------------//

    // Set the action that occurs when a user clicks the first option header.
    $w('#option1Button').onClick((event) => {
        // Expand and collapse the appropriate sections by calling the toggleFold() function.
        toggleFold(1);
    })

    // Set the action that occurs when a user clicks the second option header.
    $w('#option2Button').onClick((event) => {
        // Expand and collapse the appropriate sections by calling the toggleFold() function.
        toggleFold(2);
    })

    // Set the action that occurs when a user clicks the third option header.
    $w('#option3Button').onClick((event) => {
        // Expand and collapse the appropriate sections by calling the toggleFold() function.
        toggleFold(3);
    })

    // Set the action that occurs when a user clicks the fourth option header.
    $w('#option4Button').onClick((event) => {
        // Expand and collapse the appropriate sections by calling the toggleFold() function.
        toggleFold(4);
    })

    //-------------Cart Button-------------//

    $w('#addToCartButton').onClick((event) => {
        cart.addProducts([{
            "productId": productId,
            "quantity": 1,
            "options": { choices: selectedOptions }
        }])
    })
});

// Clear customization images and selected options
function clearSelection() {
    selectedOptions = {};
    $w('#verre1Img').hide();
    $w("#verre2Img").hide();
    $w('#verre3Img').hide();
    $w("#verre4Img").hide();
    $w("#verre1Img").src = 'https://';
    $w("#verre2Img").src = 'https://';
    $w("#verre3Img").src = 'https://';
    $w("#verre4Img").src = 'https://';
    $w("#addToCartButton").disable();
}

// Select a specific choice for a specific customization option.
function selectChoiceForOption($item, option, choiceData) {
    // Set the selected choice in the selectedOptions global object.
    selectedOptions[capitalizeFirstLetter(option)] = choiceData.title;
    // Change the image for the selected option to the selected choice's image.
    $item(`#${option}Img`).src = choiceData.displayImage;
    // Show the option image.
    $item(`#${option}Img`).show();
    // If a choice has been selected for all of the options:
    if (Object.keys(selectedOptions).length === NUMBER_OF_CHOICES) {
        //Enable the "Add to Cart" button.
        $item('#addToCartButton').enable();
    }
}

// Utility function for capitalizing the first letter of a string.
function capitalizeFirstLetter(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
}

function selectionRepeatersReady() {
    // Set up each item in the body selection repeater as it is loaded.
    $w('#verre1SelectionRepeater').onItemReady(($item, itemData, index) => {
        // Set the action that occurs when a user clicks a choice for the body option.
        $item('#selectBodyButton').onClick(() => {
            // Select the choice using the selectChoiceForOption() function.
            selectChoiceForOption($item, 'verre1', itemData);

            //on mobile, collapse the repeater and expand the next one
            if (wixWindow.formFactor === 'Mobile') {
                toggleFold(2);
            }
        });
    })

    // Set up each item in the sleeves selection repeater as it is loaded.
    $w('#verre2SelectionRepeater').onItemReady(($item, itemData, index) => {
        // Set the action that occurs when a user clicks a choice for the sleeves option.
        $item('#selectSleevesButton').onClick(() => {
            // Select the choice using the selectChoiceForOption() function.
            selectChoiceForOption($item, 'verre2', itemData);

            //on mobile, collapse the repeater and expand the next one
            if (wixWindow.formFactor === 'Mobile') {
                toggleFold(3);
            }
        });
    })
// Set up each item in the pocket selection repeater as it is loaded.
    $w('#verre3SelectionRepeater').onItemReady(($item, itemData, index) => {
        // Set the action that occurs when a user clicks a choice for the pocket option.
        $item('#button2').onClick(() => {
            // Select the choice using the selectChoiceForOption() function.
            selectChoiceForOption($item, 'verre3', itemData);

            //on mobile, collapse the repeater and expand the next one
            if (wixWindow.formFactor === 'Mobile') {
                toggleFold(4);
            }
        });
    })

    // Set up each item in the neckline selection repeater as it is loaded.
    $w('#verre4SelectionRepeater').onItemReady(($item, itemData, index) => {
        // Set the action that occurs when a user clicks a choice for the neckline option.
        $item('#button3').onClick(() => {
            // Select the choice using the selectChoiceForOption() function.
            selectChoiceForOption($item, 'verre4', itemData);

            //on mobile, make all repeaters collapsed
            if (wixWindow.formFactor === 'Mobile') {
                toggleFold(4);
            }
        });
    })
}

// Expand the specified section if it is collapsed. Collapse the specified section if it is expanded.
// If expanding, collapse all other sections.
function toggleFold(index) {
    // Set variables for the elements that correspond to specified index's section.  
    let fold = $w(`#option${index}Box`);
    let arrowDown = $w(`#arrowDown${index}`);
    let arrowUp = $w(`#arrowUp${index}`);

    // If the specified section is currently collapsed:
    if (fold.collapsed) {
        // Set its elements to the expanded state.
        fold.expand();
        arrowDown.show();
        arrowUp.hide();
        // If the specified section is currently expanded:
    } else {
        // Set its elements to the collapsed state.
        fold.collapse();
        arrowDown.hide();
        arrowUp.show();
    }

    // For each section index:
    for (let i = 1; i <= NUMBER_OF_CHOICES; i++) {
        // If it is not the specified index:
        if (i !== index) {
            // Set its elements to the collapsed state.
            $w(`#option${i}Box`).collapse();
            $w(`#arrowDown${i}`).hide();
            $w(`#arrowUp${i}`).show();
        }
    }
}

Everything seems to work fine, except two things :

  1. the most important : the add to cart button is not working. I can’t figure out why, since console.log and console.error show that it’s working. But my basket icon remains empty, even if I click on it.
  2. The first toggle doesn’t collapse properly, as you can see.

Could you help me ?

I tried very different codes, using this tutorial as an example and asking ChatGPT for help.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật