Product update

I want to run my .jsw file automatically from the home page, when opening the page, and update the parameters of a product.
when I run the .jsw file it takes me to set parameters even if they are already inserted in the .js file
if I insert id and product name in parameter set, the product is updated correctly, but I would like it to do it with the parameters in automatically.

I would like to run this run automatically


if I enter the parameters here, the product is updated, but I would like you to get the parameters from the .js file

Well,…
…first → rename your exported function on your BACKEND from → “Product” to
…" get_Product " or " set_Product ", so it will be clear what exactly your functions is doing → getting or setting your Product!

Always try to code logical and with a clear code-structure.

My question would be → Where is your import from “product.jsw” on your front-end, or on your public-js-file?

Something like…

import { set_Product } from 'backend/product.jws'

And where do you call your function → “Product” ???

And please next time do not use → IMAGES to show your CODE!
Paste your code into a CODE-Block…

I would like to attach my code as you told me but I am having trouble publishing, it says I cannot post link even if I entered the code.
I would like to perform this change automatically, from home. in set_product the program starts only if I run it, and asks me to re-enter the parameters (shouldn’t it set them automatically?)
I apologize for the lack of clarity but it is my first approach to wix.
I hope I was clear


@raffaele Can’t download your attachment. Please than use again your previous method to show your data.

You can’t use the CODE-Block → Because you are totaly new! But it seems you are not new to CODING! You are able to work on BACKEND (alsmost able).

You will be able to use all FORUM-FUNCTIONS after 5-days of beeing a registered forum-user as i know.

But i am still missing something in your code-setup!
Could you show your HOME-CODE-PART?

Also pay attention onto your 2x declared CONSTANTS (const) on the public-JS-file.
I had somekind of an similar problem to declare variables or constants on a public file.

Inspect the RESULTS of the two mentioned constans in your CODE of the public-JS-file, by a usage of a console-log.

Which results do you get?

May be you consider to use the wix-storage to transfer data from page to public or from public to backend.

@russian-dima


I apologize for the photos.
i created the files in the backend.


these are the three files I created.
I would like to call the execution of the .jsw file from the home. i tried with $ w but it gives error.
Now my problem is that when I click on the play button in set_Product, it opens the run screen where I should enter the parameters. I would like this run to do it automatically and to take the parameters it requires by itself (null, null).


when i run the function the parameters are null. but if I enter them manually and run, the change happens correctly

@raffaele
Ok, look this…

I would like to call the execution of the .jsw file from the home. i tried with $ w but it gives error.
…is exactly what i wanted to know.

Normaly what you are trying to do is to UPDATE one of your PRODUCTS on BACKEND, right? And i could not recognize what for you need the public JS-File.

How should it work instead?

  1. You have your HOME-PAGE (front-end)
  2. You have your “product.jsw” (back-end).
  3. I assume you can do a product-update ONLY on/from backend.
  4. But now you do not know how to call (how to send data to back-end) from frond-end.

This is all you need on BACKEND:

import wixStoresBackend from 'wix-stores-backend';

export function set_Product(productID, productInfo) {
    return(wixStoresBackend.updateProductFields(productID, productInfo))
}

And this one will be on your FRONT-END:

import { set_Product } from 'backend/product.jws'

$w.onReady(async function(){
   let updatedProd = await set_Product(productID, productInfo); console.log(updatedProd);
});

Of course you also will have to prepare the product_ID and product_Info first…

let productID = "ENTER_PRODUCT_ID_HERE"
let productInfo = {name: "Bermuda-Kappa-XYZ"}

(almost) Complete code:


import { set_Product } from 'backend/product.jws'

$w.onReady(async function(){
   let productID = "ENTER_PRODUCT_ID_HERE";
   let productInfo = {name: "Bermuda-Kappa-XYZ"}
   let updatedProd = await set_Product(productID, productInfo); console.log(updatedProd);
});

First test it with setting the PRODUCT-ID and INFO → MANUALY on your HOME-Frontend. Test it.

Perhaps my first assumption was wrong and you are not that experienced with coding as i thought, but now with this new knowledge → you should be able to generate an automatic process.

Good luck!

@russian-dima IT WORKED!!!. thank you very much, I have been trying for a long time and finally thanks to you I succeeded.
there were some functions I didn’t know about such as async and await.
The product was edited successfully when I click preview. I was wondering if it was now possible to make it do the same thing when I load my domain home. Because my aim is to do an update check of all the products through a csv file that I get from a management software. Thanks again and I apologize for the many questions. Thanks to you I am learning many things.

@raffaele No problem!