How do I add a document to existing filled collection? - php

I am building an eShop for educational purposes and I need to handle the orders from a user. A user has a basket which is getting filled with products. If he decides buy another product I have insert a document into the existing collection of the card
Current MongoDB collection:
{
"_id": {
"$oid": "61f3d79c921000006000547d"
},
"username": "mike",
"products": {[
"number": "3",
"name": "Honduras",
"price": 7,
"stock": 10,
]},
"status": "UNPAID"
}
By adding another product, needs to be inserted in the existing collection in the field of products.
Expected to look like:
{
"_id": {
"$oid": "61f3d79c921000006000547d"
},
"username": "mike",
"products": {[
"number": "3",
"name": "Honduras",
"price": 7,
"stock": 10,
], [
"number": "4",
"name": "India",
"price": 10,
"stock": 11,
]},
"status": "UNPAID"
}
I am using PHP for the back end operations. The script that I wrote it is simple. I am searching if orders with user's username exist. If they exist then I lock the current order and make the operations needed.
I think that I am missing something in syntax of the update for the purpose described above:
PHP script:
if (isset($_POST['add'])){
// Ordered by name from URL
$username = $_GET['username'];
// Product info
$name = $_POST['add'];
// Finds the product selected from products
$product = $collection -> findOne(array("name" => "$name"));
// Serialize product to be added.
$json = MongoDB\BSON\toJSON(MongoDB\BSON\fromPHP($product));
// Searching for order from certain user
$collection = $db -> orders;
$exists = $collection -> findOne(array("username" => "$username"));
if (!is_null($exists)){
// The problem is here (maybe?)
$exists->updateOne(
array("products" => {}),array('$set'=>$json);
);
}
Any help and suggestions would be really appreciated!

Well, you need to use something like below
db.collection.update(
{find Condition},
{$push: {products : {key: value, key2: value 2}}
)
Here the catch is push. It adds an element to array. Here the element is an object.

Related

WooCommerce product WPML translation inserted by REST API doesn't include variations of original product

Unfortunately I'm working with wordpress and woocommerce, what I'm trying to accomplish is to insert products and relative translations using the woocommerce REST API, here's the scenario:
First, i create a simple product
{
"name": "Dummy",
"type": "variable",
"attributes": [
{
"id": 2,
"name": "Color",
"position": 1,
"variation": "true",
"options": [
"Bianco",
"Nero"
]
},
{
"id": 3,
"name": "Size",
"position": 2,
"variation": "true",
"options": [
"A",
"B",
"C"
]
}
]
}
This is a variable product, so I need to create its variations as well
The product created return its id, let's say 10
Then I create several product variations like this:
{
"regular_price": "10.10",
"stock_quantity": 10,
"attributes": [
{
"id": 2,
"name": "Color",
"option": "Bianco"
},
{
"id": 3,
"name": "Size",
"option": "A"
}
]
}
So I have a product with variations, now i want to create a translation of the product with wpml, according to wmpl rest api specifications, I create a product translation like this:
{
"name": "Dummy en",
"type": "variable",
"lang": "en",
"translation_of": 10
}
I specified the language en, the main language is it
I included translation_of with the id of the original product
What happens is that the english product is created, but without variations
What I have tried:
Several issues similar to this are solved by the troubleshooting page of wpml, where I can generate missing variations but I need this to be done automatically, products are created automatically, so relative translations too must be
If I enter product page from wordpress back end and I press the update button, variations are created and adjusted, this word for the original product and the variation, but again, I need this to be done automatically
I tried to modify and insert the translation in almost all ways, adding/removing sku, id, attributes, categories ecc.
Any hints? Thanks

Woocommerce Rest API issue with stock quantity

I create Woocommerce products in my ionic app. I would like also to push the stock quantity when I create the product. However, the stock always is with NULL stored in my MYSQL DB. Here is the JSON feed
{
"product": {
"title": "test stock",
"stock_quantity": "35",
"images": [],
"post_author": 88,
"type": "simple",
"categories": []
}
}:
And here the piece of code from the app
var productInfo = {
title: this.productName,
regular_price: this.productPrice,
sale_price: this.productDiscount,
stock_quantity: this.productStock,
description: this.productDesc,
images: new Array<any>(),
post_author: this.userService.id,
type: this.productType,
categories: new Array<any>()
};
I tried different tags for stock_quantity like only "stock" and tried to push the variable with "" and without it. But I'm facing still the same issue.
Your JSON syntax is correct. Also, you are passing the correct parameter for the stock as stock_quantity and as I reviewed your JSON request you have not passed the manage_stock parameter as true by default that considers as false. so, you need to pass that parameter also.
You can try the following code.
{
"product": {
"title": "test stock",
"stock_quantity": "35",
"manage_stock": true,
"images": [],
"post_author": 88,
"type": "simple",
"categories": []
}
}:
you can refere the WooCommerce REST API Product Properties for create products

Podio PHP: category is empty when no value for item

We are using the Podio PHP library to create forms for our users. These forms load their data from Podio trough the Podio PHP library. A form is linked to a specific Podio Item ID for this.
With this ID we get the fields and their possible values. Most of them are Category fields. We get the values for this trough:
<?php
$item_data = PodioItem::get_basic( $podio_item_id );
$car_types = $item_data->fields["car-type"]->config['settings']['options'];
var_dump( $car_types );
?>
But the curious thing is, if the item in Podio has no value selected for this category, getting the options fail. It's empty. It doesn't exists.
How do we populate a form with values even if the item has no options selected for it?
Podio doesn't store empty value, it's empty, it doesn't exist, so there is nothing to store. In fact, what you are probably looking for is Application definition, which has list fields. Then category field has config with list of possible values and id's and colors.
Like this:
"fields": [
{
"status": "active",
"type": "category",
"field_id": 81772,
"label": "Status",
"config": {
"required": true,
"label": "Status",
.....some other config values .....
"settings": {
"multiple": false,
"options": [
{
"status": "deleted", <= example of deleted category option
"text": "Not groomed",
"id": 13,
"color": "DCEBD8"
},
{
"status": "active", <= example of active category option
"text": "Open",
"id": 1,
"color": "FFD5C2"
},
.... <= other category options goes here
You can read more about Apps here: https://developers.podio.com/doc/applications/get-app-22349

Search post by categories Wordpress WP-API

I want to search posts by categories in the WP-API.
I know I can search posts by the attribute categories or filter[cat].
But the posts contains more than one category.
I tried to search like this:
{host}/wp-json/wp/v2/posts?categories=69&filter[cat]=[228,246,237]&per_page=50
or
{host}/wp-json/wp/v2/posts?categories=69&filter[cat]=228&filter[cat]=246&filter[cat]=237&per_page=50
or
{host}/wp-json/wp/v2/posts?categories=69&categories=246&categories=237&categories=228
This didn't work for me. It caused the search to look for the last attribute.
Any ideas?
This is the structure of Json response
{
"id": 9333,
"date": "2016-08-02T14:17:01",
"date_gmt": "2016-08-02T12:17:01",
"guid": {
"rendered": "{post}/?p=9333"
},
"modified": "2016-08-03T08:50:35",
"modified_gmt": "2016-08-03T06:50:35",
"slug": "{post}",
"type": "post",
"link": "{host}/{post}/",
"title": {
"rendered": "{post}"
},
"content": {
"rendered": "{post}"
},
"excerpt": {
"rendered": "{post}"
},
"author": 3,
"featured_media": 0,
"comment_status": "closed",
"ping_status": "closed",
"sticky": false,
"format": "standard",
"categories": [
228,
237,
207,
217,
246,
231,
69,
221,
270,
244
],
"tags": [],
"_links": []
}
Thanks!
If you want to get posts from multiple categories, there are few solutions, depending from your needs.
In case when you want to get posts from category with ID = 1 OR category with ID = 2 use the following URL:
http://localhost/lifelog/wp-json/wp/v2/posts?filter[cat]=1,2
or:
http://localhost/lifelog/wp-json/wp/v2/posts?categories=1,2
For case when you want to get posts from category with ID = 1 AND category with ID = 2 you can use:
http://localhost/lifelog/wp-json/wp/v2/posts?filter[category__and][]=1&filter[category__and][]=2
BUT - some filter values used in the filter array needs authenticated user with edit_posts privileges.
Fortunately, there is a more simple solution - WordPress supports links like:
http://example.com/category/test1+test2/
Under above URL you will get list of posts which are assigned to the test1 AND test2 categories. And in the REST API you can achieve the same behaviour with the following URL:
http://localhost/lifelog/wp-json/wp/v2/posts?filter[category_name]=test1%2Btest2
Please remember that you have to replace the + sign into %2B.

How to add and retrieve metafields to order when adding new order via api

I am trying to add metafields to new order as follows:
{ "order": {
"line_items": [
{
"title": "Test Order item 1",
"price": 0.00,
"grams": "1700",
"quantity": 1, "taxable": false
}
],
"metafield": {
"namespace": "inventory",
"key": "amazonOrderId",
"value": 123456789,
"value_type": "integer" },
"email": "rahul#testOrder.com",
"total_tax": 0.00 } }
The order is being created on shopify admin panel but on retrieving same order metafields are not returned. I am not sure where to find added metafield, is it added or not. Please help.
Thanks
Ok, so I found how to store and retrieve metafields for an order in Shopify..
we need to make a POST call to /admin/orders/{order_id}/metafields.json with following format:
{
"metafield": {
"namespace": "global",
"key": "amazonID",
"value": "dfsfsd",
"value_type": "string"
}
}
and then to retrieve the metafields for that particular order we use following url:
/admin/orders/{order_id}/metafields.json
Reference: https://docs.shopify.com/api/reference/metafield#create

Categories