get item id / Requesting a quote based on the dynamic page

Hi, everyone. I have been using @code-queen 's tutorial and I am really struggling to get the ID of the current item from the dataset “lighting” so I can do the request button feature.

I have used the following code for the individual dynamic page.

import { session } from 'wix-storage';
import wixLocation from 'wix-location';

$w.onReady(function () {

 $w("#addButton").onClick((event) => {
  $w('#lighting').getItems(1,1)
   .then((results) => {
 let theLighting = results._id; //This line gets the ID of the current product
    session.setItem("results", theLighting); //This line saved the product ID into session storage
   })
   .then(() => {
    wixLocation.to("/selection");  //This line redirects you to the page with the request form
   })
   .catch((error) => {
    console.log(error);
   });
 });

});

But I do not understand where what the 1,1 in the getItems(function is). It says it has something to do with the Idex but I don’t know what that means.

I have tried to replace it with

 $w("#addButton").onClick((event) => {
  $w('#lighting').getItems()
   .then((results) => {

But this doesn’t work or

$w("#myDataset").getCurrentItem();

and this does work.

Below is the attempt I’ve made for the selection (quote) page.

import { quoteRequest } from 'backend/notifications';
import wixCrm from 'wix-crm';
import { session } from 'wix-storage';
import wixData from 'wix-data';

$w.onReady(function () {
 let result = session.getItem("result"); //This line will get the item from storage session

 $w("#dataset1").onReady(() => {
  console.log("The product dataset is ready to be filtered with the following ID:" + result);

  $w("#dataset1").setFilter(wixData.filter()
    .eq("_id", result) //This line will filter the product dataset to find the 1 matching product from storage session
   )
   .then(() => {
    console.log("The product is" + result);
    $w('#formStrip').expand(); //This line triggers the form strip to expand only if there are no errors in filtering dataset
    $w('#errorStrip').collapse(); //This line triggers the error strip to collapse only if there are no errors in filtering dataset
   })
   .catch((err) => {
    console.log(err);
    $w('#formStrip').collapse(); //This line triggers the form strip to collapse if an error occurs
    $w('#errorStrip').expand(); //This line triggers the error strip to expand only if an error occurs
   });
 });
 
 //Our dataset for our form is called #requestDataset
 $w("#requestDataset").onBeforeSave(() => {
  $w("#dataset1").onReady(() => {
 let productFound = $w("#dataset1").getCurrentItem();  //This line gets the current product from your Stores Dataset, in our example the Stores dataset is called #dataset1
 let theName = productFound.name;  //This line gets the product name
 let theImage = productFound.mainMedia; //This line gets the main image for the product 

 //The following lines will set the values for these fields in the database

   $w("#requestDataset").setFieldValues({
 "productName": theName, //Make sure to replace the field key of each line to match your database
 "productId": result, //Make sure to replace the field key of each line to match your database
 "productImage": theImage //Make sure to replace the field key of each line to match your database
   });

 let checkFirst = $w('#firstName').valid; //Make sure to replace the fields with your form elements ID names
 let checkLast = $w('#lastName').valid;
 let checkPhone = $w('#phone').valid;
 let checkEmail = $w('#email').valid;

 //This next line checks to make sure all the validations we set in the user input settings are valid

 if (checkEmail && checkPhone && checkLast && checkFirst) {
    $w('#requestDataset').save(); //This is the name of our form dataset
    $w('#errorText').hide(); //This line hides the error message if all fields are valid
   } else {
    $w('#errorText').show(); //This line shows the error message
    console.log("Canceling save");
 return false; //This line cancels the save function on the dataset
   }

  });

 });

 $w("#requestDataset").onAfterSave(() => {
  quoteRequest(); //This line triggers the notification from the backend.  In our example, the backend function is called quoteRequest .
  console.log("A new notification was sent to all contributors.");
  $w('#formStrip').collapse();
  $w('#errorStrip').collapse();
  $w('#successStrip').expand(); //This line triggers the form strip to expand after the form has been submitted.
  $w("#header1").scrollTo(); //This line scrolls the page to the top header
 });

});import wixLocation from 'wix-location';

Please help, if you can!

Still searching for help ?

Yes desperately. I recently tried to do this

import { session } from ‘wix-storage’;
import wixLocation from ‘wix-location’;

$w.onReady( function () {

$w(“#addButton”).onClick((event) => {
$w(“#lightingdata”).onReady( () => {
$w(“#lightingdata”).getItems(0,1000)
.then((result) => {
let theProduct = result._id; //This line gets the ID of the current product
session.setItem(“result”, theProduct); //This line saved the product ID into session storage
})
.then(() => {
wixLocation.to(“/quote”); //This line redirects you to the page with the request form
})
.catch((error) => {
console.log(error);
});
});

});

but didn’t work

Ok, please describe shortly your situation, without looking at your code.

  1. What do you want to achieve?
  2. Which elements are used?
  3. What is the structure/flow of your concept?
  4. Tell all relevant informations as most as possible in detail.

for example…

  1. I have 2-databases DATABASE-A (xxxx) and DATABASE-B (yyyy)
  2. DATABASE-A is connected trugh a DYNAMI-DATASET to my DYNAMIC-PAGE-1.
  3. On my DYNAMIC-PAGE i have aform, when i press X-BUTTON, then should happen the following… and so on.

The better your description, the better the helping result. :wink:

1 Like

Hi …

You mention that you have attempted to add code on your dynamic page. What type of page is this? Is it a Wix Stores page or custom dynamic page that you created from your own custom database collection?

If it is a Wix Stores page, then please follow the tutorial video and example code once again. (We use ‘getProduct’ not ‘getItems’ in our example code)

If it is a custom dynamic page that you have created then you must get the current item from the dynamic dataset in order to obtain it’s ID.

https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#getCurrentItem

Hi Nayeili,

Thank you so much for the response. I am using a dynamic dataset called “lighting”. I saw that you suggested that in one of your comments so I tried this with your code, but isn’t working.

import { session } from 'wix-storage';
import wixLocation from 'wix-location';

$w.onReady(function () {

 $w("#addButton").onClick((event) => {
  $w("#lighting").getCurrentItem();
   .then((results) => {
 let theLighting = results._id; //This line gets the ID of the current product
    session.setItem("results", theLighting); //This line saved the product ID into session storage
   })
   .then(() => {
    wixLocation.to("/selection");  //This line redirects you to the page with the request form
   })
   .catch((error) => {
    console.log(error);
   });
 });

});

Do you know where I have gone wrong? I don’t think I fully understand the concept of getcurrentItem, it also isn’t bringing the data through to the page.

Hi!
I have two pages Lighting (Single) - Dynamic Page and Selection - Form page

I have one dynamic dataset called “lighting” on the Lighting (Single page). It contains and items and I want to get the current ID of the and store it in the session storage.

When the user clicks the button “add to selection” button the page redirects to the Selection page. Here the form will store the data from the session storage of the page it just came from so users can submit a form and I can send a quote.

It would be great if I could collect the data from multiple items and store all of that information to send in the form.

Open up the Wix API link I added in the above comment. It is 1 line of code example. That is all you need. There is no ‘results’ in that example code.

First you get the item, then you get the ID of that item.

Thanks for replying, here is the code now:

import { session } from 'wix-storage';
import wixLocation from 'wix-location';

$w.onReady(function () {

 $w(“#addbutton”).onClick((event) => {
 let itemObj = $w(“#lighting”).getCurrentItem();
session.setItem("product", theProduct); //This line saved the product ID into session storage
   })
   .then(() => {
    wixLocation.to("/selection");  //This line redirects you to the page with the request form
   })
   .catch((error) => {
    console.log(error);
   });
 });

});

Am I still correct to have (“product”, theProduct) or will they need to be redefined?


I added back the defining theProduct, but that didn’t work either

import { session } from 'wix-storage';
import wixLocation from 'wix-location';

$w.onReady(function () {

 $w("#addButton").onClick((event) => {
 let itemObj = $w("#lightingdata").getCurrentItem();
 let theProduct = itemObj._id; //This line gets the ID of the current product
session.setItem("product", theProduct); //This line saved the product ID into session storage
   })
   .then(() => {
    wixLocation.to("/selection");  //This line redirects you to the page with the request form
   })
   .catch((error) => {
    console.log(error);
   });
 });

Ok, before i start to reconstruct your project to make an example for you, please take a look at this example here. It is very similar to your needs.

In the example you will see the same given situation like yours.
You have a dynamic page (Courses) connected to the Courses-database, and on the other side you have the instructors.

The question was: “How to set a quote to the right instructor”.
Almost the same situation, like yours. Take a look here…

https://russian-dima.wixsite.com/meinewebsite/dynamic-action

You will enter the “Overview-Page” which will show you the situation.
When you klick on the blue button, you will redirected to the dynamic page (courses) ----> like your “Lighting-page”.

Just take a look, i am sure it is absolutely the same process, as you need it.

And you do not need any —> Wix-Storage-Sessions to bring data to next page, this is not necessary.

Take a loo, and tell me if you could modify this example for your needs?