”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 超级市场

超级市场

发布于2024-07-30
浏览:673

Mega Mart

This is a submission for the Wix Studio Challenge.

Background

Why follow outdated methods when AI is transforming the business landscape? I created this ecommerce because nowadays AI is the trend for companies. Mega Mart is a platform that not only offers a wide variety of products but also enhances the UI/UX through advanced features made in Wix Studio.


What I Built

I developed an advanced AI search options to solve the issue of inefficient product discovery, having the goal of creating a more intuitive and enjoyable shopping experience for users.

Have you ever seen someone wearing a t-shirt you like but didn’t want to ask them the brand? With Mega Market, simply take a photo and upload it to our platform; we’ll search for it in our product database.

Why go to the mall when you can search for the product you want just as you would ask to a store employee? Simply write your query in natural language, and we’ll show you examples of the products you're looking for. For example, try typing I need a winter jacket for women or I would like a black smartphone with camera.

The best part is that the site doesn't requires for a Wix subscription to finish the checkout process because it's entirely built using Wix elements and Velo APIs.


How to buy at Mega Market

Create an account (or not...)

At Mega Mart, you have the option to purchase products with or without an account.
Creating an account offers several benefits:

  • During checkout, your personal information and previous addresses are automatically filled in, saving you time.
  • Additionally, you receive instant cashback that can be used for future purchases.
  • Love a product but don’t want to buy it yet? Your account includes a wishlist feature, allowing you to save products for later.

Search product items

As mentioned earlier, you can search for products by uploading images from your device, using AI, or browsing all items to select the one you like the most.

Verify Your Cart

Before checking out, ensure you have added all the desired products to your cart. Adjust the quantity of items if needed or delete any mistakes. Additionally, review your total and the estimated delivery date.

Checkout

If you are logged in, your personal information will be automatically filled out. You can choose one of your previously saved addresses or enter a new one, which will be added to your account for future orders.
Enter your credit or debit card information, then click the final button to complete the checkout.

Your order is completed!

You will be redirected to the order's page, where you can view the details of your purchase and its status: whether it is being processed, in transit for delivery, or already delivered. You will also receive an email confirmation of your order.


Mega Mart Benefits

Passwordless

Who wants to remember passwords these days? At Mega Mart, you don't have to worry about them; each time you want to log in to your account, you will receive a one-time digit code via email, SMS, or call.

Cashback

Who doesn't love getting rewarded for shopping? Each time you purchase at Mega Mart, you will receive 1% cashback, which you can use for paying future orders.

Wishlist

Ever found a product you love but aren’t ready to buy yet? At Mega Mart, you can save it to your wishlist for a future purchase.

Order status

You don't need to switch between pages to see the status of your shipment. From the order's page, you can easily check whether it's in process, in transit, or already delivered.

Full responsive

Ever wonder if a website can be just as smooth on your computer, tablet, or phone? At Mega Mart, our site is fully responsive across all devices.

Save your addresses

Curious about how convenient shopping can be? At Mega Mart, your addresses are saved for future purchases, and you can manage them easily from your account page.

No account needed

While many ecommerce sites require account creation to shop, at Mega Mart, you can purchase as a guest. However, remember all the benefits you can have as a Mega Mart's frequent customer.


Development Journey

Utilizing Wix Studio has helped me accelerating frontend development, significantly reducing time while ensuring a fully responsive design across all devices. JavaScript plays an important role in enhancing user interaction with Wix Studio elements, enabling dynamic features seamlessly. Moreover, our site employs robust security measures, including database's, web methods', and router's security, to safeguard user data and ensure a secure browsing experience.

Before diving into how I created Mega Mart, it's important to say that I did not install any apps from the Wix Market. While I used Wix Stores for saving products to the cart and then retrived them in some pages, I opted to build the product pages, cart, and checkout pages entirely from scratch using Wix Elements.

Disclaimer
Since this is a demo for the contest, I have only saved 29 products in the database. To get the most out of the AI image product recognition and AI search methods, please first familiarize yourself with the available products in the store. For example, searching for a printer or a hammer might not yield results, as these items are not in the demo database. However, if this project were scaled up for large companies like Home Depot or Walmart, which have thousands of products in their databases, the AI functionalities would perform comprehensive searches and provide a wider range of results.


Try it out!

Curious to see what Mega Mart can bring to your shopping experience? Click to explore and discover the possibilities.
Lets go!


How I built it

Developing this ecommerce site wasn't easy, especially given the tight timeline, but I'm proud to have built it with all its functionality within just 2 weeks.

Home

The first page I developed was the home page, where I aimed to incorporate features commonly found in other ecommerce sites such as showcasing best sellers, new arrivals, and highlighting the benefits of shopping at the store.
Below is an example of how I retrieved data for best sellers and newest products in the code.

import wixData from 'wix-data';
import wixWindowFrontend from 'wix-window-frontend';
import wixLocationFrontend from 'wix-location-frontend';

const limit = wixWindowFrontend.formFactor === "Mobile" ? 6 : 5

const bestSellers = (await wixData.query("Products").descending("rating").limit(limit).find()).items
$w('#repeater2').data = bestSellers

$w('#repeater2').onItemReady(($item, itemData, index)=>{
    $item('#imageX9').src = itemData.imageUrl[0].src
    $item('#imageX9').alt = itemData.name

    $item('#text12').text = itemData.name
    $item('#text13').text = "$"   itemData.price

    $item('#box15').onClick(() => {
        wixLocationFrontend.to(`/product/${itemData._id}`)
    })
})

const newProucts = (await wixData.query("Products").descending("_createdDate").limit(limit).find()).items
$w('#repeater3').data = newProucts

$w('#repeater3').onItemReady(($item, itemData, index)=>{
    $item('#imageX10').src = itemData.imageUrl[0].src
    $item('#imageX10').alt = itemData.name

    $item('#text15').text = itemData.name
    $item('#text14').text = "$"   itemData.price

    $item('#box19').onClick(() => {
        wixLocationFrontend.to(`/product/${itemData._id}`)
    })
})

As you can see, the code also includes logic to detect whether the user is browsing on a mobile device or not. In mobile view, the repeaters are displayed in two columns, while in tablet and computer modes, they are arranged in a single row with five columns.

Login & Register

I believe that the register and login pages should have an intuitive UI/UX interface. If either processes are difficult or frustrating for users, they may leave the site, resulting in a lost sale.
To prevent this, Mega Mart uses a passwordless system. Each time you want to log in, a one-time code will be sent via SMS, call, or email, eliminating the need to remember multiple passwords.

Register

import { verify, sendVerification } from "backend/twilio.web"
import { register } from "backend/members.web"

$w('#button1').onClick(async () => {
    $w('#imageX9').show()
    $w('#button1').disable()
    if ($w('#box37').isVisible) {
        if ($w('#input5').valid) {

            $w('#text35').collapse()

            const verifyEmailResponse = await verify($w('#input3').value, $w('#input5').value)
            const verifyPhoneResponse = await verify($w('#input4').value, $w('#input5').value)

            if (verifyEmailResponse || verifyPhoneResponse) {
                try {
                    const sessionToken = await register($w('#input1').value, $w('#input2').value, $w('#input3').value, $w('#input4').value)
                    await authentication.applySessionToken(sessionToken)
                } catch (error) {
                    $w('#text35').text = "Error registering: "   new Error(error).message
                    $w('#text35').expand()
                }

            } else {
                $w('#text35').text = "Wrong verification code."
                $w('#text35').expand()
            }
        }
    } else {
        const sendEmailResponse = await sendVerification("email", $w('#input3').value)
        const sendSMSResponse = await sendVerification("sms", $w('#input4').value)
        if (sendEmailResponse && sendSMSResponse) {
            $w('#input1,#input2,#box36').collapse()
            $w('#box37').expand()
            $w('#button1').label = "Register"
            $w('#text37').hide()
        }
    }
    $w('#imageX9').hide()
    $w('#button1').enable()
})

This code handles the registration process when the button is clicked. If the verification code input box is visible and the verification code input is valid, it verifies the user's email or phone number with the provided verification code. If either verification is successful, it attempts to register the user and then apply a session token for authentication. If the verification fails, an error message is displayed.
If the box is not visible, it sends verification codes to the user's email and phone, then updates the UI to allow the user to complete the registration.

Login

Sending verification code to user

import { sendVerification } from "backend/twilio.web"
import { verifyEmailOrPhoneNumber} from "backend/members.web"

$w('#button2,#button3').onClick(async (rest) => {
    if ($w('#input7').valid || $w('#input8').valid) {
        $w('#imageX10').show()
        $w('#text34').collapse()
        $w('#button2,#button3').disable()

        let toSearch = $w('#input7').isVisible ? $w('#input7').value : $w('#input8').value
        if (!(await verifyEmailOrPhoneNumber(toSearch))) {
            $w('#text34').text = "There isn't an account with that email or phone number."
            $w('#text34').expand()
        } else {
            switch (rest.target) {
            case $w('#button2'):
                let response = false
                if ($w('#input7').isVisible) {
                    response = await sendVerification("email", $w('#input7').value)
                    verificationTypeAtLogin = "email"
                    $w('#text31').text = "Please check you email. It may be at junk mails."
                } else {
                    response = await sendVerification("sms", $w('#input8').value)
                    verificationTypeAtLogin = "sms"
                    $w('#text31').text = "Please check you SMS."
                }

                if (response) {
                    $w('#text33,#box41,#box47').collapse()
                    $w('#box46,#box48').expand()
                    $w('#text36').hide()
                } else {
                    $w('#text34').text = "We couldn't send the verification code. Try using another method."
                    $w('#text34').expand()
                }
                break;
            case $w('#button3'):
                verificationTypeAtLogin = "call"
                $w('#text31').text = "You will receive a phone call in a few moments."

                if (await sendVerification("call", $w('#input8').value)) {
                    $w('#text33,#box41,#box47').collapse()
                    $w('#box46,#box48').expand()
                    $w('#text36').hide()
                } else {
                    $w('#text34').text = "We couldn't send the verification code. Try using another method."
                    $w('#text34').expand()
                }
                break;
            }
        }
        $w('#imageX10').hide()
        $w('#button2,#button3').enable()
    }
})

It first verifies the existence of the entered email or phone number.
Depending on which button is clicked, it sends a verification code via email, SMS, or call. Then it expands verification code input for the user to write the received code.
If verification fails, an appropriate error message is displayed. Finally, the loading image is hidden, and the buttons are re-enabled.

Verifying verification code

import { verify } from "backend/twilio.web"
import { login } from "backend/members.web"

if ($w('#input9').valid) {
    $w('#imageX11').show()
    $w('#text34').collapse()
    $w('#button4').disable()

    let verifyResponse = false
    if (verificationTypeAtLogin === "email") verifyResponse = await verify($w('#input7').value, $w('#input9').value)
    else verifyResponse = await verify($w('#input8').value, $w('#input9').value)

    if (verifyResponse) {
        try {
            const sessionToken = await login(verificationTypeAtLogin === "email" ? $w('#input7').value : "", verificationTypeAtLogin !== "email" ? $w('#input8').value : "")
            await authentication.applySessionToken(sessionToken)
        } catch (error) {
            $w('#text34').text = "Error registering: "   new Error(error).message
            $w('#text34').expand()
            $w('#imageX11').hide()
            $w('#button4').enable()
        }

    } else {
        $w('#text34').text = "Wrong verification code."
        $w('#text34').expand()
        $w('#imageX11').hide()
        $w('#button4').enable()
    }

}

It verifies the provided code based on the verification type (email or phone). If verification is successful, it attempts to log the user in and apply the session token for authentication.

Redirecting the user

Whether the user is logging in or registering, after a successful process, they will be redirected to the last page they visited.

import { authentication } from "wix-members"
import wixLocationFrontend from 'wix-location-frontend'

authentication.onLogin(() => {
    wixLocationFrontend.to(wixLocationFrontend.query.redirect)
})

Search bar window

In order to allow users to search for products from any page, I added the search bar inside a lightbox.

Search type

First check what search type the user selected.

import wixWindowFrontend from 'wix-window-frontend';

$w('#imageX7').hide()
$w('#button2,#input1,#searchByImage,#searchByAI').enable()
const type = wixWindowFrontend.lightbox.getContext()

let instructions = ""
switch (type) {
case "text":
    $w('#searchByTextMobile').show("fade", { duration: 500 })
    instructions = "Enter keywords or product names into the search bar."
    break;
case "image":
    $w('#searchByImage').show("fade", { duration: 500 })
    instructions = "Upload an image of the product you are looking for."
    break;
case "AI":
    $w('#searchByAI').show("fade", { duration: 500 })
    instructions = "Type your query in natural language (e.g., 'I need a winter jacket for women')."
    break;
}

$w('#text21').text = `Search by ${type}`
$w('#text22').text = instructions

Handle search type

By image

import { getImageLabels } from "backend/googleVision.web"

$w('#searchByImage').onChange(async () => {
    $w('#searchByImage').disable()
    $w('#imageX7').show()
    const file = await $w('#searchByImage').uploadFiles()
    const labels = await getImageLabels(file[0].fileUrl)

    sendTo("tags", labels)
})

First, the image is uploaded to the Media Manager to obtain its URL. Then, the URL is sent to Google Vision to identify tags related to the image.

By AI

$w('#searchByAI').onKeyPress(async (rest) => {
    if ($w('#searchByAI').valid && rest.key === "Enter") {
        $w('#searchByAI').disable()
        $w('#imageX7').show()

        const labels = await getTextLabels($w('#searchByAI').value)
        sendTo("tags", labels)
    }
})

If the user presses Enter, the input text is tokenized to extract nouns.

Search redirection

Once one of the methods is completed, the user is redirected to the search router page with the queries included in the URL.

import wixLocationFrontend from 'wix-location-frontend';

function sendTo(type = "", value = []) {
    wixLocationFrontend.to(`/search?type=${type}&value=${JSON.stringify(value)}`)
}

Search page

This is a Wix Router page that helped me show the different types of search, and also the wishlist of the products the user saved.

Repeater display

import wixWindowFrontend from 'wix-window-frontend';
import { cart } from "wix-stores-frontend";

let originalItem = wixWindowFrontend.getRouterData()
let items = wixWindowFrontend.getRouterData()
initRepeater(items)

$w('#repeater1').onItemReady(async ($item, itemData, index) => {
    $item('#imageX9').src = itemData.imageUrl[0].src
    $item('#text12').text = itemData.name
    $item('#text13').text = "$"   itemData.price
    $item('#ratingsDisplay1').rating = itemData.rating

    $item('#button1').onClick(async () => {
        const product = [{
            productId: itemData._id,
            quantity: 1,
        }]

        await cart.addProducts(product)
        await showAlert("add")
    })

    $item('#box38').onClick(() => {
        wixLocationFrontend.to(`/product/${itemData._id}`)
    })

    if (authentication.loggedIn()) {
        const userID = await currentMember.getMember({ fieldsets: ["FULL"] })
        const item = await wixData.query("Wishlist").eq("product", itemData._id).eq("_owner", userID._id).find()
        if (item.totalCount === 0) $item('#button2').expand()
        else {
            itemData.wishList = item.items[0]._id
            $item('#button3').expand()
        }
    } else $item('#button2').expand()

    $item('#button2').onClick(async () => {
        if (authentication.loggedIn()) {
            const itemWishlist = await wixData.insert("Wishlist", { product: itemData._id })
            itemData.wishList = itemWishlist._id
            await showAlert("save")
            $item('#button2').collapse().then(() => {
                $item('#button3').expand()
            })
        } else wixWindowFrontend.openLightbox("loginToWishlist")
    })

    $item('#button3').onClick(async () => {
        await wixData.remove("Wishlist", itemData.wishList)
        await showAlert("remove")
        $item('#button3').collapse().then(() => {
            $item('#button2').expand()
        })
    })
})

function initRepeater(items = [], changeFilter = true) {
    $w('#repeater1').data = items

    if ($w('#repeater1').data.length === 0) {
        if (wixLocationFrontend.query.type === "wishlist") {
            $w('#text27').text = "You haven't add any item to your wishlist. Add your first product!"
        } else {
            $w('#text27').text = "Try searching with another method or include more details if you are using AI."
        }
        $w('#section5').collapse()
        $w('#section6').expand()
    } else {
        const htmlLinks = items[0].tags.slice(0, 5).map(word => `${word}`).join(' - ');
        $w('#text22').html = htmlLinks
        $w('#section6').collapse()
        $w('#section5').expand()

        $w('#switch1').checked = false

        if (changeFilter) {
            const maxPrice = sortByPrice(items, "desc")[0]

            $w('#slider1').max = maxPrice.price
            $w('#slider2').max = maxPrice.price

            $w('#slider2').value = maxPrice.price
        }

    }

}

As you can see, this includes the import { cart } from "wix-stores-frontend" statement, which allows the product to be added to the cart when the user clicks the button inside the repeater.

Sort products

$w('#switch1').onChange(() => {
    $w('#repeater1').data = []

    items = sortByPrice(items, $w('#switch1').checked ? "desc" : "asc")
    $w('#repeater1').data = items
})

function sortByPrice(products = [{}], order = 'asc') {
    return products.sort((a, b) => {
        if (order === 'asc') {
            return a.price - b.price;
        } else if (order === 'desc') {
            return b.price - a.price;
        } else {
            throw new Error("Order must be either 'asc' or 'desc'");
        }
    });
}

Filter products

$w('#slider1,#slider2').onChange(() => {
    items = filterByPriceRange(originalItem, $w('#slider1').value, $w('#slider2').value)
    initRepeater(items, false)
})

function filterByPriceRange(products, minPrice, maxPrice) {
    return products.filter(product => product.price >= minPrice && product.price 



Wixlocation.onChange()

Since searching for new products redirects the user to the same page /search and only changes the URL queries, I needed to handle this with wixLocationFrontend.onChange() to update the repeater items.

Product page

This is also a router page /product/${productID} that fetches product information from the backend and displays it using wixWindowFrontend.getRouterData().

import wixWindowFrontend from 'wix-window-frontend';

const product = wixWindowFrontend.getRouterData()

$w('#gallery1').items = product.imageUrl
$w('#features').text = product.features
$w('#title').text = product.name
$w('#brand').text = product.brand
$w('#color').text = product.color
$w('#price').text = String(product.price)
$w('#description').text = product.description
$w('#ratings').rating = product.rating
$w('#stock').text = `${product.stock} available`

Since you can add the product to your cart from here, with the option to set a quantity, I used the following code to achieve this.

import { cart } from "wix-stores-frontend"

$w('#addCart').onClick(async () => {
    const productToSave = [{
        productId: product._id,
        quantity: Number($w('#inputQuantity').value),
    }]

    await cart.addProducts(productToSave)
    await showAlert("add")

})

$w('#quantityLess,#quantityMore').onClick((rest) => {
    let value = Number($w('#inputQuantity').value)

    switch (rest.target) {
    case $w('#quantityLess'):
        if (value > 1) value--
        break;
    case $w('#quantityMore'):
        value  
        break;
    }

    $w('#inputQuantity').value = String(value)
})

Also having the option to add the product to the wishlist or delete it.

$w('#addWishList').onClick(async () => {
    if (authentication.loggedIn()) {
        await wixData.insert("Wishlist", { product: product._id })
        await showAlert("save")
        $w('#addWishList').collapse().then(() => {
            $w('#deleteWishlist').expand()
        })
    } else wixWindowFrontend.openLightbox("loginToWishlist")
})

$w('#deleteWishlist').onClick(async () => {
    await wixData.remove("Wishlist", product.wishList)
    await showAlert("remove")

    $w('#deleteWishlist').collapse().then(() => {
        $w('#addWishList').expand()
    })
})

Cart page

Need to review your cart? This page allows you to check if you have added the desired quantities of the products you want to buy and also lets you delete any items if needed.

import { cart } from "wix-stores-frontend"

$w('#repeater1').onItemReady(($item, itemData, index) => {
$item('#quantityLess,#quantityMore').onClick(async (rest) => {
        $item('#quantityLess,#quantityMore,#button3').disable()
        let value = Number($item('#inputQuantity').value)

        switch (rest.target) {
        case $item('#quantityLess'):
            if (value > 1) value--
            break;
        case $item('#quantityMore'):
            value  
            break;
        }

        $item('#inputQuantity').value = String(value)
        $item('#text22').text = `$${(itemData.price * value).toFixed(2)}`

        const updadedCart = await cart.updateLineItemQuantity(Number(itemData._id), value)
        $item('#quantityLess,#quantityMore,#button3').enable()

        $w('#text25').text = `Total: $${updadedCart.totals.total.toFixed(2)}`
    })

    $item('#button3').onClick(async () => {
        $item('#quantityLess,#quantityMore,#button3').disable()
        await cart.removeProduct(Number(itemData._id))
        await initCart()
    })
})

Clicking the buttons #quantityLess and #quantityMore triggers an event to edit the product quantity. Clicking the #button3 removes the product from the cart.

Checkout

This is the final step for purchasing at Mega Market.
If the user is a Site Member, it retrieves their saved addresses and accumulated cashback from their account.

import { authentication, currentMember } from "wix-members-frontend"

if (authentication.loggedIn()) {
    $w('#text25').collapse()

    const user = await currentMember.getMember({ fieldsets: ["FULL"] })

    $w('#input1').value = user.contactDetails.firstName
    $w('#input2').value = user.contactDetails.lastName
    $w('#input3').value = user.loginEmail
    $w('#input4').value = user.contactDetails.phones[0]

    const addresses = await getUserAdderess(user._id)

    if (addresses.length === 0) {
        $w('#dropdown1').options = [{ value: "No addresses founded", label: "No addresses founded" }]
        $w('#dropdown1').value = "No addresses founded"
        $w('#dropdown1').disable()
    } else {
        const addressesOptions = addresses.map(address => ({
            label: `${address.addressLine} ${address.state}, ${address.country}`,
            value: JSON.stringify(address)
        }));

        $w('#dropdown1').options = addressesOptions
    }

    const totalCashback = await getTotalCashback(user._id)

    if (totalCashback === 0) {
        $w('#switch1').disable()
        $w('#switch1').checked = false
        $w('#text28').text = `You will earn $${Number(currentCart.totals.total * 0.01).toFixed(2)} in cashback with this order.`
    } else {
        cashback = totalCashback > currentCart.totals.total ? currentCart.totals.total : totalCashback;
        $w('#switch1').enable()
        $w('#text28').text = `Use my cashback: $${Number(cashback).toFixed(2)}`
    }

}

It also validates that the card number of the user is a Master Card, Visa or AMEX.

$w('#input10').onCustomValidation((value, reject) => {
        const cardsInfo = {
            "Unknown": {
                "image": "https://static.wixstatic.com/media/4c0a11_600a3b93f607463296a7c9149268800e~mv2.gif",
                "regex": /^\d $/,
                "length": 16
            },
            "Master Credit": {
                "image": "https://www.mercadopago.com/org-img/MP3/API/logos/master.gif",
                "regex": ,
                "length": 16
            },
            "Visa Credit": {
                "image": "https://www.mercadopago.com/org-img/MP3/API/logos/visa.gif",
                "regex": ,
                "length": 16
            },
            "AMEX": {
                "image": "https://www.mercadopago.com/org-img/MP3/API/logos/amex.gif",
                "regex": ,
                "length": 15
            },
            "Visa Debit": {
                "image": "https://www.mercadopago.com/org-img/MP3/API/logos/debvisa.gif",
                "regex": ,
                "length": 16
            },
            "Master Debit": {
                "image": "https://www.mercadopago.com/org-img/MP3/API/logos/debmaster.gif",
                "regex": ,
                "length": 16
            }
        }

        const cardCategory = identifyCardCategory(value)
        $w('#imageX8').src = cardsInfo[cardCategory].image
        $w('#input10').maxLength = cardsInfo[cardCategory].length
        if (!cardsInfo[cardCategory].regex.test(value)) {
            reject("Invalid card number")
        }
    })

function identifyCardCategory(cardNumber) {
    const patterns = {
        masterCredit: ,
        visaCredit: ,
        amex: ,
        visaDebit: ,
        masterDebit: 
    };

    if (patterns.masterCredit.test(cardNumber)) return "Master Credit";
    if (patterns.visaCredit.test(cardNumber)) return "Visa Credit";
    if (patterns.amex.test(cardNumber)) return "AMEX";
    if (patterns.visaDebit.test(cardNumber)) return "Visa Debit";
    if (patterns.masterDebit.test(cardNumber)) return "Master Debit";

    return "Unknown";
}

After the user inputs all the required information, it's time to process the order.

$w('#button2').onClick(async () => {
    if ($w('#input1').valid && $w('#input2').valid && $w('#input3').valid && $w('#input4').valid && $w('#input5').valid && $w('#input6').valid && $w('#input7').valid && $w('#input8').valid && $w('#input9').valid && $w('#input10').valid && $w('#dropdown2').valid && $w('#dropdown3').valid && $w('#dropdown4').valid) {
        $w('TextInput,Dropdown').disable()
        $w('#box53').show()

        const productArray = currentCart.lineItems.map(item => ({
            quantity: item.quantity,
            name: item.name,
            priceData: {
                price: item.price,
            },
            mediaItem: {
                src: item.mediaItem.src
            }
        }));

        const insertedItem = await wixData.insert("Orders", {
            lineItems: productArray,
            totals: {
                subtotal: subTotal,
                total: total,
            },
            shippingInfo: {
                shipmentDetails: {
                    address: {
                        city: $w('#dropdown4').value,
                        country: $w('#dropdown2').value,
                        addressLine: $w('#input5').value,
                        postalCode: $w('#input6').value,
                        subdivision: $w('#dropdown3').value,
                    }
                }
            },
            billingInfo: {
                address: {
                    city: $w('#dropdown4').value,
                    country: $w('#dropdown2').value,
                    addressLine: $w('#input5').value,
                    postalCode: $w('#input6').value,
                    subdivision: $w('#dropdown3').value,
                },
                lastName: $w('#input2').value,
                firstName: $w('#input1').value,
                email: $w('#input3').value,
                phone: $w('#input4').value
            },
            cashback: $w('#switch1').checked
        })

        for (let lineItem of currentCart.lineItems) {
            await cart.removeProduct(lineItem.id)
        }

        wixLocationFrontend.to(`/order/${insertedItem._id}`)
    }
})

This wixData.insert() triggers a data hook that creates the order using wixStoresBackend.createOrder() which I'll talk of it later.

Order page

This page displays the order information passed though the wixWindowFrontend.getRouterData().

import wixWindowFrontend from 'wix-window-frontend';

const orderInfo = wixWindowFrontend.getRouterData()
const contact = orderInfo.billingInfo
const shipping = contact.address

$w('#text21').text = `Order #${orderInfo.orderNumber}`
$w('#text48').text = `Amount paid: $${orderInfo.totals.total}`
$w('#text47').text = `Subtotal: $${Number(orderInfo.totals.subtotal).toFixed(2)} | Discounts: $${Number(orderInfo.totals.subtotal - orderInfo.totals.total).toFixed(2)}`

$w('#text26').text = contact.firstName
$w('#text27').text = contact.lastName
$w('#text28').text = contact.phone
$w('#text29').text = contact.email

$w('#text30').text = shipping.addressLine
$w('#text31').text = shipping.city
$w('#text32').text = shipping.postalCode
$w('#text33').text = `${shipping.subdivision}, ${shipping.country}`

APIs

Velo APIs

API Method Location in Project
Wix Data 1. query() 2. insert() 3. remove() 4. get() 1. Home page 2. Search page 3. Checkout
Wix Window Frontend 1. formFactor 2. lightbox.getContext() 3. getRouterData() 4. openLightbox() 1. Home page 2. Login 3. Search bar 4. Search page 5. Product page
Wix Location Frontend 1. to() 2. onChange() 3. query 1. Home page 2. Login 3. Search bar 4. Search page 5. Cart page 6. Checkout 7. Order page
{authentication} from Wix Members 1. applySessionToken() 2. onLogin() 1. Login 2. Checkout
{ cart } from Wix Stores 1. addProducts() 1. Search page 2. Product page 3. Cart page 4. Checkout
{currentMember} from Wix Members 1. getMember() 1. Product page 2. Checkout
Wix Secrets getSecret() Backend

External services

country-state-city

Helped to get the countries, states of countries and cities of states.

import { Permissions, webMethod } from "wix-web-module";

export const getAllCountries = webMethod(
    Permissions.Anyone,
    async () => {
        const countries = await require('country-state-city').Country.getAllCountries();

        const toReturn = countries.map(state => ({
            label: state.name,
            value: state.isoCode
        }));

        return toReturn;
    }
);

export const getStatesOfCountry = webMethod(
    Permissions.Anyone,
    async (country) => {
        const states = await require('country-state-city').State.getStatesOfCountry(country);

        const toReturn = states.map(state => ({
            label: state.name,
            value: state.isoCode
        }));

        return toReturn;
    }
);

export const getCitiesOfStates = webMethod(
    Permissions.Anyone,
    async (country, state) => {
        const states = await require('country-state-city').City.getCitiesOfState(country, state);

        const toReturn = states.map(state => ({
            label: state.name,
            value: state.name
        }));

        return toReturn;
    }
);

google-cloud/vision

Reads the image and provides labels of what it identifies.

const vision = require('@google-cloud/vision');

export const getImageLabels = webMethod(
    Permissions.Anyone,
    async (imageUrl) => {
        const credentials = JSON.parse(await wixSecretsBackend.getSecret("googleVisionCredentials"))

        const client = new vision.ImageAnnotatorClient({ credentials });

        try {
            const [result] = await client.labelDetection(getPublicURL(imageUrl));
            const labels = result.labelAnnotations.map(label => label.description);

            const lowerCaseLabels = labels.map(element => element.toLowerCase());

            return lowerCaseLabels;
        } catch (error) {
            console.error('Error detecting labels:', error);
            return []
        }
    }
);

Compromise

Extracts the nouns from a text.

const nlp = require('compromise');

export const getTextLabels = webMethod(
    Permissions.Anyone,
    async (text) => {
        let doc = nlp(text);

        let categories = doc.match('#Noun').out('array');
        const lowerCaseCategories = categories.map(element => element.toLowerCase());

        return lowerCaseCategories
    }
);

Twilio

Used for sending the verification code via SMS, call or email and to verify the one-time digit code.

export const sendVerification = webMethod(
    Permissions.Anyone,
    async (type =  "email" || "sms" || "call", to = "[email protected]") => {
        try {
            const { accountSid, authToken, service } = await auth()

            const client = twilio(accountSid, authToken);

            await client.verify.v2
                .services(service)
                .verifications.create({
                    channel: type,
                    to: to,
                });

            return true
        } catch (error) {
            return false
        }
    }
);

export const verify = webMethod(
    Permissions.Anyone,
    async (to = "", code = "") => {
        const { accountSid, authToken, service } = await auth()

        const client = twilio(accountSid, authToken);

        const verificationCheck = await client.verify.v2
            .services(service)
            .verificationChecks.create({
                code: code,
                to: to,
            });
        const status = verificationCheck.status === "approved" ? true : false;

        return status
    }
);
版本声明 本文转载于:https://dev.to/deapco/mega-mart-3b9p?1如有侵犯,请联系[email protected]删除
最新教程 更多>
  • Hexabot 设置和可视化编辑器教程:构建您的第一个 AI 聊天机器人
    Hexabot 设置和可视化编辑器教程:构建您的第一个 AI 聊天机器人
    聊天机器人爱好者大家好!在本教程中,我们将指导您完成设置和使用开源 AI 聊天机器人构建器 Hexabot 的过程。我们将首先克隆 GitHub 存储库、安装依赖项并为 Hexabot 配置环境变量。您还将学习如何使用 Docker 启动项目、访问管理面板以及使用可视化编辑器创建聊天机器人流程。 在...
    编程 发布于2024-11-02
  • mysql_fetch_row()、mysql_fetch_assoc() 和 mysql_fetch_array():您应该选择哪一个?
    mysql_fetch_row()、mysql_fetch_assoc() 和 mysql_fetch_array():您应该选择哪一个?
    mysql_fetch_row()、mysql_fetch_assoc() 和 mysql_fetch_array() 解释背景:如果您正在使用已弃用的MySQL 扩展中,在从结果集中检索数据的 mysql_fetch_row()、mysql_fetch_assoc() 和 mysql_fetch_...
    编程 发布于2024-11-02
  • Next.js - 概述
    Next.js - 概述
    本文作为初学者友好的指南和使用 Next.js 的步骤。 Next.js 是一个用于构建 Web 应用程序的灵活框架。相反,它是一个构建在 Node.js 之上的 React 框架。 设置您的 Next.js 项目 要启动新的 Next.js 项目,您需要在计算机上安装 Node.js。 安装 安装...
    编程 发布于2024-11-02
  • 如何在代码中使用 Unsplash 图片
    如何在代码中使用 Unsplash 图片
    作为一名从事新 SaaS 项目的开发人员,我需要直接通过 URL 链接一些 Unsplash 图像。 最初,我看到一篇推荐使用 https://source.unsplash.com/ API 的文章(链接)。但是,此方法不再有效,并且仅从 URL 字段复制链接并不能提供嵌入所需的直接图像 URL...
    编程 发布于2024-11-02
  • 如何合并关联数组、处理缺失键以及填充默认值?
    如何合并关联数组、处理缺失键以及填充默认值?
    合并多个关联数组并添加具有默认值的缺失列将关联数组与不同的键集组合起来创建统一的数组可能具有挑战性。这个问题探索了一种实现此目的的方法,所需的输出是一个数组,其中键被合并,缺失的列用默认值填充。为了实现这一点,建议结合使用 array_merge 函数精心设计的键数组:$keys = array()...
    编程 发布于2024-11-02
  • 通过 testcontainers-go 和 docker-compose 来利用您的测试套件
    通过 testcontainers-go 和 docker-compose 来利用您的测试套件
    Welcome back, folks! Today, we will cover the end-to-end tests in an intriguing blog post. If you've never written these kinds of tests or if you stri...
    编程 发布于2024-11-02
  • 以下是一些适合您文章的基于问题的标题:

**直接简洁:**

* **如何在Windows控制台中正确显示UTF-8字符?**
* **为什么传统方法无法显示
    以下是一些适合您文章的基于问题的标题: **直接简洁:** * **如何在Windows控制台中正确显示UTF-8字符?** * **为什么传统方法无法显示
    在 Windows 控制台中正确显示 UTF-8 字符使用传统方法在 Windows 控制台中显示 UTF-8 字符的许多尝试均失败正确渲染扩展字符。失败的尝试:使用 MultiByteToWideChar() 和 wprintf() 的一种常见方法被证明是无效的,只留下 ASCII 字符可见。此外...
    编程 发布于2024-11-02
  • ReactJS 的模拟介绍
    ReactJS 的模拟介绍
    ReactJS 19:重要部分 并发模式增强: ReactJS 19 中最大的改进是并发模式,它不仅在应用程序自身更新时保持 UI 平滑和响应灵敏,而且还确保了无缝界面,尤其是在复杂的过渡(例如动画)时。 改进的服务器组件: 在 Python 的引领下,ReactJ...
    编程 发布于2024-11-02
  • 首届DEV网页游戏挑战赛评委
    首届DEV网页游戏挑战赛评委
    我被要求对DEV团队9月份组织的第一届网页游戏挑战赛提交的参赛作品进行评判,结果在10月初发布。 我们几个月来一直在 DEV 上组织挑战(迷你黑客马拉松),并计划宣布我们的第一个网页游戏挑战。鉴于您在游戏社区 和 dev.to 的专业知识和参与度,我们想知道您是否有兴趣成为客座评委。 谁能对此说“不...
    编程 发布于2024-11-02
  • 购买经过验证的现金应用程序帐户:安全可靠的交易
    购买经过验证的现金应用程序帐户:安全可靠的交易
    Buying verified Cash App accounts is not recommended. It can lead to security risks and potential account bans. If you want to more information just k...
    编程 发布于2024-11-02
  • 为什么 `std::function` 缺乏相等比较?
    为什么 `std::function` 缺乏相等比较?
    揭开 std::function 的等式可比性之谜难题:为什么是 std::function,现代 C 代码库的一个组成部分,不具备相等比较功能?这个问题从一开始就困扰着程序员,导致管理可调用对象集合的混乱和困难。早期的歧义:在 C 语言的早期草案中11 标准中,operator== 和operat...
    编程 发布于2024-11-02
  • JavaScript 类型检查 |编程教程
    JavaScript 类型检查 |编程教程
    介绍 本文涵盖以下技术技能: 在本实验中,我们将探索一个 JavaScript 函数,该函数检查提供的值是否属于指定类型。我们将使用 is() 函数,它利用构造函数属性和 Array.prototype.includes() 方法来确定值是否属于指定类型。本实验将帮助您更好地了解 ...
    编程 发布于2024-11-02
  • 使用 Streamlit 将机器学习模型部署为 Web 应用程序
    使用 Streamlit 将机器学习模型部署为 Web 应用程序
    介绍 机器学习模型本质上是一组用于进行预测或查找数据模式的规则或机制。简单地说(不用担心过于简单化),在 Excel 中使用最小二乘法计算的趋势线也是一个模型。然而,实际应用中使用的模型并不那么简单——它们通常涉及更复杂的方程和算法,而不仅仅是简单的方程。 在这篇文章中,我将首先构...
    编程 发布于2024-11-02
  • ## utf8_unicode_ci 与 utf8_bin:哪种 MySQL 排序规则最适合德国网站?
    ## utf8_unicode_ci 与 utf8_bin:哪种 MySQL 排序规则最适合德国网站?
    为德语选择最佳 MySQL 排序规则在设计为德语受众量身定制的网站时,支持像 ä、 ü 和 ß。当涉及特定于语言的要求时,排序规则的选择起着重要作用。字符集和排序规则对于字符处理,UTF-8 仍然是首选选项,提供广泛的字符支持。至于排序规则,需要考虑德语特定字符。排序规则类型MySQL 提供各种排序...
    编程 发布于2024-11-02
  • 异常处理基础知识
    异常处理基础知识
    Java中的异常处理由五个关键字管理:try、catch、 throw、throws和finally。 这些关键字构成了一个相互关联的子系统。 要监视的指令位于 try 块内。 如果try块中发生异常,则会抛出异常。 代码可以使用catch捕获并处理异常。 系统异常由Java运行时自动抛出。 要手...
    编程 发布于2024-11-02

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3