Applying "request a quote" code to wix store products

Hi, I’m trying to add a request a quote option to my store product page instead of enabling visitors to go to a cart and checkout. I’m currently using this tutorial: Tutorial: Request a Quote

Since im not familiar to coding at all, Ive used ChatGPT to help me make the code work. I’m stuck as the developer console sent this message back:
Running the code for the Product Page page. To debug this code in your browser’s dev tools, open cdi9h.js.

Product Page is ready

Product loaded:

{…}

Currency: USD

Bookshelf product detected

Quote-only product detected.

Error loading product: TypeError: addToCartButton.hide is not a function. (In ‘addToCartButton.hide()’, ‘addToCartButton.hide’ is undefined)

How can I fix this as I can’t physically click on the add to cart button and find its ID or anything.

I’m using Wix Editor, not studio. The code that I’ve run is below:

import wixWindow from "wix-window";
import wixLocation from "wix-location";

let product;
const bookshelfId = "3ab35961-c77f-c5a2-463e-e0ca084a52bd";

$w.onReady(async function () {
  console.log("Product Page is ready");

  try {
    await initProductPage();
  } catch (error) {
    console.error("Error during product page initialization:", error);
  }

  wixLocation.onChange((location) => {
    let newPath = location.path;
    console.log("Location changed:", newPath);
    if (newPath?.[0] === "product-page") {
      initProductPage();
    }
  });
});

async function initProductPage() {
  try {
    product = await $w("#productPage1").getProduct();
    console.log("Product loaded:", product);

    const currency = product?.currency || "USD";
    console.log("Currency:", currency);

    if (product._id === bookshelfId) {
      console.log("Bookshelf product detected");

      if (product.price === 0 || !product.price) {
        console.log("Quote-only product detected.");

        // Hide the default Add to Cart button
        const addToCartButton = $w('[data-hook="add-to-cart"]');
        if (addToCartButton) {
          addToCartButton.hide();  // Hide the button if possible
        }

        // Show and configure the custom Request a Quote button
        const requestQuoteButton = $w('#requestQuoteButton');  // Custom button added in Wix Editor
        if (requestQuoteButton) {
          requestQuoteButton.show();
          requestQuoteButton.onClick(() => onRequestQuote());
        } else {
          console.error("Custom Request a Quote button not found.");
        }
      }
    } else {
      console.log("Different product detected, no changes applied");
    }
  } catch (error) {
    console.error("Error loading product:", error);
  }
}

function onRequestQuote() {
  console.log("Request a Quote button clicked");
  getProductInfo().then((data) => {
    console.log("Product info collected:", data);
    wixWindow.openLightbox("Request a quote", data).then((results) => {
      console.log("Lightbox closed with results:", results);
    });
  }).catch((error) => {
    console.error("Error in onRequestQuote:", error);
  });
}

async function getProductInfo() {
  try {
    const bookshelfQuantity = await $w("#productPage1").getQuantity();
    const bookshelfChoices = await $w("#productPage1").getSelectedChoices();
    const bookshelfCustomText = await $w("#productPage1").getCustomText();

    console.log("Quantity:", bookshelfQuantity);
    console.log("Choices:", bookshelfChoices);
    console.log("Custom Text Fields:", bookshelfCustomText);

    return {
      product: product,
      quantity: bookshelfQuantity,
      choices: bookshelfChoices,
      customTextFields: bookshelfCustomText,
    };
  } catch (error) {
    console.error("Error in getProductInfo:", error);
    return {};
  }
}

ecommerce validation plugin codes:
validation-config.js file code:

import * as ecomValidations from "interfaces-ecommerce-v1-validations-provider";

export function getConfig() {
  return { validateInCart: true };
}

validation.js file code:

import * as ecomValidations from "interfaces-ecommerce-v1-validations-provider";

const bookshelfId = "3ab35961-c77f-c5a2-463e-e0ca084a52bd";

export const getValidationViolations = async (options, context) => {
  let violations = [];
  const severity = ecomValidations.Severity.ERROR;
  const description =
    "You can't purchase this product. You can only request a quote.";
  const requestAQuoteLineItem = options.validationInfo.lineItems.find(
    (lineItem) => isBookshelf(lineItem.catalogReference?.catalogItemId),
  );
  if (requestAQuoteLineItem != undefined) {
    const quantity = requestAQuoteLineItem.quantity;
    if (quantity > 0 && quantity < 200) {
      violations.push({
        severity,
        target: {
          lineItem: {
            name: ecomValidations.NameInLineItem.LINE_ITEM_DEFAULT,
            _id: requestAQuoteLineItem._id,
          },
        },
        description: description,
      });
    }
  }
  return { violations };
};

const isBookshelf = (catalogItemId) => {
  return catalogItemId != undefined && catalogItemId == bookshelfId;
};

Overall, I just need help to make code work, my biggest issue is the cart button as I can’t access the ID and I don’t know how to solve the issues stated by the developer console. If possible, I also need to know how to make this integrated to ALL store products, not just one. And if it’s possible to see a full cart of the products a customer wants (without prices) so they can add / remove product options before submitting the quote form –– I need them to be able to submit a quote form with multiple products.

Seems it might be related to this line, which is trying to interact with the DOM

const addToCartButton = $w('[data-hook="add-to-cart"]');
if (addToCartButton) {
  addToCartButton.hide();  // Hide the button if possible
}

In the example linked, it’s not hiding the button, rather changing it’s action to run a handler:

$w("#productPage1").onAddToCart(onAddToCartHandler);

Worth noting that the tutorial linked is intended for the Wix Studio editor which supports a slightly different product page compared to the Wix Editor

1 Like

Hi, is there any way to work around this then while in the Wix Editor? I really do not know how to code but I need a solution and it’s been more of a struggle because I can’t easily access the element ID of the button.

If I migrate my site to Wix Studio, can I continue editing my current site and working with this code?

If you simply want the Request a Quote button for all products, just copy paste this code:

Product Page Code:

import wixWindowFrontend from "wix-window-frontend";
import wixLocationFrontend from "wix-location-frontend";

let product;

$w.onReady(function () {
  initProductPage();
});

wixLocationFrontend.onChange((location) => {
  let newPath = location.path;
  if (newPath?.[0] === "product-page") {
    initProductPage();
  }
});

async function initProductPage() {
  product = await $w("#productPage1").getProduct();
  $w("#productPage1").setAddToCartLabel("Request a Quote");
  $w("#productPage1").onAddToCart(onAddToCartHandler);
}

async function onAddToCartHandler(resume, cancel) {
  const data = await getProductInfo();
  wixWindowFrontend.openLightbox("Request a quote", data).then((results) => {
    cancel();
  });
}

async function getProductInfo() {
  const itemQuantity = await $w("#productPage1").getQuantity();
  const itemChoices = await $w("#productPage1").getSelectedChoices();
  const itemCustomText = await $w("#productPage1").getCustomText();
  return {
    product: product,
    quantity: itemQuantity,
    choices: itemChoices,
    customTextFields: itemCustomText,
  };
}

in validation-spi-config.js

import * as ecomValidations from "interfaces-ecommerce-v1-validations-provider";

export function getConfig() {
  return { validateInCart: true };
}

in validation-spi.js

import * as ecomValidations from "interfaces-ecommerce-v1-validations-provider";

export const getValidationViolations = async (options, context) => {
  let violations = [];
  const severity = ecomValidations.Severity.ERROR;
  const description =  "You can't purchase this product. You can only request a quote.";
    const quantity = options.validationInfo.lineItems[0].quantity;
    if (quantity > 0 && quantity < 200) {
      violations.push({
        severity,
        target: {
          lineItem: {
            name: ecomValidations.NameInLineItem.LINE_ITEM_DEFAULT,
            _id: options.validationInfo.lineItems[0]._id,
          },
        },
        description: description,
      });
    }
  return { violations };
};

This is just modified code from the same tutorial that should work for all products.

Hi, I’ve applied the code you provided and I ran it on my site. However, this is the notification I’m getting from the developer console:

Running the code for the Product Page page. To debug this code in your browser’s dev tools, open cdi9h.js.

Error: $w(“#productPage1”).setAddToCartLabel is not a function. (In ‘$w(“#productPage1”).setAddToCartLabel(“Request a Quote”)’, ‘$w(“#productPage1”).setAddToCartLabel’ is undefined)

(thank you for all the help so far by the way!)

As Noah mentioned, you cannot edit the Add To Cart button label if you’re using the classic Wix editor.

You might want to check out the apps available in the app market that claim to offer this feature, else you can build a fully custom product page as per your linkings.

Is there any way for me to migrate the site to wix studio? If I begin using wix studio and duplicate the site, will it make it possible for me to edit it as a wix studio site?

As of now, there is no way to migrate your existing site directly to studio, you will have to rebuild it.