Phantom-Tech-IND / school-pictures

0 stars 0 forks source link

Add items correctly to the cart #72

Closed bodnarescu-riccardo closed 3 months ago

bodnarescu-riccardo commented 4 months ago

Task: Add Product to Cart with Selected Images

Function Implementation (example in JavaScript)


function addToCart(productId, imageSelections, quantity, options) {
    const cartItem = {
        productId: productId,
        images: {
            stickers: imageSelections.stickers,
            portraitSet: imageSelections.portraitSet,
            largePortrait: imageSelections.largePortrait
        },
        quantity: quantity,
        options: options,
        subtotal: calculateSubtotal(productId, quantity, options)
    };

    function calculateSubtotal(productId, quantity, options) {
        const basePrice = getProductPrice(productId);
        const optionsPrice = options.digitalData ? 39 : 0;
        return (basePrice + optionsPrice) * quantity;
    }

    shoppingCart.push(cartItem);
    updateCartDisplay();
}

const productId = "SparSet1";
const imageSelections = {
    stickers: "img123",
    portraitSet: "img234",
    largePortrait: "img345"
};
const quantity = 1;
const options = {
    digitalData: true
};

addToCart(productId, imageSelections, quantity, options);