I use ReactJS to build a mobile web app for a WooCommerce shop based on Rest API. Now the question is how to get the price of variation product when selecting the different attributes.
I made a simple demo to descript my issue. https://mofect.com/test
In this demo, after all data is loaded, you can see two options and JSON data in Chrome console, I want to show the final price after set the Size and Color attributes.
I can only get the attributes and variants id from the product Rest API:
{
id: 40
attributes:[
{id: 0, name: "Color", position: 1, visible: true, variation: true}
{id: 0, name: "Size", position: 2, visible: true, variation: true}
]
variations:[106, 107, 108, 109]
}
Then, according to the WooCommerce API documentation http://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-product-variations, I can get all the variations price by product id:
'products/40/variations/'
{
id: 109
attributes:[
{id: 0, name: "Color", option: "White"}
{id: 0, name: "Size", option: "Large"}
],
price: "40"
}
//.... and the other variations arguments
Now the question is how I get variation price from selected attributes?
Any ideas can inspire me would be appreciated!
My source code is available to download here: https://www.dropbox.com/s/yikqwdox47dwo6t/woo-test.zip?dl=0
Thanks!
First, you have to call the products details by the default endpoint of WooCommerce is "/products/41" Where you can get all products details with the variation data.
Variation Data you will get like this:
"variations": [
64,
57,
58,
59
]
Then you can get the product variation by the variation id like this:
/products/41/variations/64
Using this you can get all details of the variation along with the variation image, attributes, price, etc.
Related
Was working on a real Estate app that contains three kinds of post, properties for Rent, Sale, and Explore. when i was working with a local file i could access them just fine but now am trying to create the same structure on firebase the problem is the upload form. this is an example of the data structure am trying to create on firebase. how do i go about making a generic form that can upload these kind of data
Data.js
proforsale=[
id: 1,
uploader: "name",
status: "Sale",
photos: []
]
proforrent=[
id: 1,
uploader: "name",
status: "Rent",
photos: []
]
explore=[
]
My JSON data looks like this:
safarisDetails: {
loading: false,
safaridetails: {
safari: [
{
id: 1,
safari_name: '3 DAYS GORILLA TREKKING TRIP',
days: 3,
days_category: '<4',
budget: '900',
title: '3 DAYS GORILLA TREKKING TOUR',
text: 'Test.',
travelertype: '',
date_created: '2021-10-08T15:22:31.733347Z'
}
],
highlight: [
[
{
id: 1,
highlight: 'See the famous mountain gorillas',
safaris: 1
},
{
id: 2,
highlight: 'Get to know the Batwa community',
safaris: 1
}
]
]
I want to render objects in my component such as the safari_name and loop trough the highlights. However, when I use {safarisdetails.safari.safari_name} i get an error message stating: 'safari_name' is not defined.
There are a couple of problems based on the same you provided...
safarisdetails is repeated twice in the JSON, but not in your check
safari is an array in the JSON, but accessed like an object in your check
For example, to get the first name, you'd write something like:
safarisdetails.safarisdetails.safari[0].safari_name
Also, is this JSON even valid? Maybe you posted a partial snippet, but the JSON has to have {} around the whole thing.
Working with a frontend variable like below in javascript with many objects... (Note - the scores would be different for each user therefore I would need to be able to get the info from the frontend)
var campgrounds = [{ name: "State Park #1" score: 0, }, { name: "State Park #2" score: 0, }, { name: "State Park #3" score: 0 }]
How would I be able to store that data in MongoDB? Could I somehow loop through the data? Or could I store the entire variable "campgrounds" in Mongo? like $("#quizData").value(campgrounds)
Would I set up a Schema like below? Even if I did I'm not sure how to actually get the data in the variable into Mongo. Not sure how to accomplish my goal. Thanks for any help! :)
var campgroundsSchema = new Schema ({
"campgrounds" : [{
name : {type: String},
score : {type : Number}
}],
});
If you connect to mongo using mongoshell ...
mongo --host localhost:27017
you can issue the following javascript commands...
use campgroundsdb
var campgrounds = [{ name: "State Park #1", score: 0 }, { name: "State Park #2", score: 0, }, { name: "State Park #3", score: 0 }]
db.campgroundscollection.insertMany(campgrounds)
Then find them by issuing...
db.campgroundscollection.find().pretty()
If you want to create a program written in JavaScript it will need the ability to connect to the mongodb process. Node.js tutorials fit this requirement...
https://www.w3schools.com/nodejs/nodejs_mongodb.asp
Let's say we have objects like this in a mongodb collection:
{
_id: 00000001
colors: ["green", "yellow"],
houses: [
{
number: 1,
owner: "John"
},
{
number: 2,
owner: "John"
},
{
number:3,
owner: "Dave"
}
]
},
{
_id: 00000002
colors: ["green", "red"],
houses: [
{
number: 15,
owner: "Dave"
},
]
}
So, to get every object where the color array contains the color green the query I would need to write would look smth like this: collection.find({colors: "green"});
Now if I would like to get all the objects in which John owns a house, how would I formulate such a query?
Basically what I am asking is, if my query would be collection.find({houses: {owner: "John", number: ?}}) what would I need to replace the "?" with to tell mongo that I don't care what value number has.
Or maybe there is another approach that I haven't thought of?
Thank you for any help!
(Btw this is a made up example hence why the IDs look weird and the object in itself doesn't seem very useful.)
To query an array of objects you can use the dot notation, try:
db.collection.find({ "houses.owner": "John"}})
I'm not able to describe my thought precisely in words, so here's an example:
[
{
'description': 'fruits',
'examples': [
{
'name': 'Apple',
'color': ['red', 'green']
},
{
'name': 'Banana',
'color': 'yellow'
}
]
},
{
'description': 'vegetables',
'examples': [
{
'name': 'Tomato',
'color': 'red'
},
{
'name': 'Carrot',
'color': 'orange'
}
]
},
{
'description': 'Wweets',
'examples': [
{
'name': 'Chocolate',
'color': ['brown', 'black', 'white']
},
{
'name': 'Candy',
'color': 'various'
}
]
}
]
Let's go step by step:
If I want to see all food categories, I query by the following command
db.food.find()
I want to see the vegetables
db.food.find({ 'description': 'vegetables' })
Now let's say I forgot how a Carrot looks like (lol). What do I do? I tried the following (Native node.js MongoDB driver):
collection.find({'examples.name': 'Carrot'}, function(err, example){
console.log(example)
// It still returns me the whole object!
});
As a result I expected MongoDB to return the next highest instance of the object. For example I wanted to do this.
console.log(example.color)
// 'orange'
Do you have any idea? I'm new to document oriented DBs :/
When you store a bunch of objects in a single document you will (by default) get back the entire document. [*]
When one of the fields of the document is an array, you will get back the full array if the item you are trying to match is found in the array.
Don't fall for the temptation to cram everything into a single document if you will normally be getting back only a subset of those things.
You have an alternative available:
You can store a collection of foods where every food has a field "type" which is "fruit" or "vegetable" or "...". You can still query for all foods, or just foods of type "fruit" or just food with name "carrot", etc.
Arrays are great for lists of attributes of a particular object/document, they are not as good when you cram documents into them that you then want to get back as first-class objects.
[*] there is a way to project and get only a subset of the fields, but you will still get back entire fields.