Good day,
I'm using the wp-cli for adding products to Wordpress, for example i use this one:
wp --allow-root wc product create --user=1 --name="Restricted" --regular_price=1
I do have some attributes called test_1 (checkbox for yes) and test_2 is a multiselect. But is there a way to fill that attributes?
I did try this:
wp wc product create --user=1 --name="Restricted" --regular_price=1 --test_1=yes --test_2=testvalue,testvalue2
But that did result in an error:
Error: Parameter errors:
unknown --test_1 parameter
unknown --test_2 parameter
And did this one, but the values were still empty:
wp wc product create --user=1 --name="Restricted" --regular_price=1 --attributes='[{"test_1": "yes", "test_2": ["testvalue","testvalue2"]}]'
And this one:
wp wc product create --user=1 --name="Restricted" --regular_price=1 --attributes='[{"test_1": 1, "test_2": ["testvalue","testvalue2"]]'
You need to specify attributes as JSON. Since you have 2 attributes, the proper command along with JSON Structure is.
wp wc product create --name='Product Name' --user=1
--attributes='[
{ "name": "test_1", "visible": true, "options" : ["yes", "no"] },
{ "name": "test_2", "visible": true, "options" : ["small", "medium"] }
]'
Check the 2nd FAQ here
It says that certain properties needs to be passed as JSON.
Some 'lists' are objects, for example, if you want to set categories for a product the REST API expects an array of objects: https://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties
this reference to create woocommerce product using WP-CLI
https://github.com/woocommerce/woocommerce/wiki/WC-CLI-Overview#frequently-asked-questions
https://nicola.blog/2016/03/10/managing-products-woocommerce-cli/
https://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties
if you add product Custom attributes or category through CLI than use JSON format like this
--attributes= [{ "name": "color", "visible": true, "options":["black","blue"]}]
--categories= [ { "id" : category_id } ]
Example demo:-
wp wc product create --name="mobile11" --description="this is mobile 11" --type=simple --regular_price=500 --sale_price=400 --user=dharmesh --categories='[ { "id" : 35 } ]' --attributes='[{ "name": "color", "visible": true, "options":["black","blue","red"]}]' --allow-root
Most of the time terminal isn't formatted properly sometimes it skips , sometime not formatting bash variable causes empty values. It depends how you're using declaring bash variable and using within woocommerce cli.
I was looking way correct format for adding / updating product attributes. This is how I was able to add product attributes for my product.
wp wc product update 2898 --user=1 --attributes='[{ "name":"Background Style","options":"White"},{ "name":"Demographic Level","options":"college-university"}]'
Where,
product_id is 2898
attributes are "Background Style" and "Demographic Level" and options are its corresponding terms.
Related
This is follow up question on Querying wordpress with meta queries from Gatsby
After a bit debugging I've gathered, and please correct me if I'm wrong, that Gatsby on build downloads the entire data structure and caches it. So all the GraphQL queries are performed against the cache. This makes all adjustments I try to make to wordpress (for example https://www.wpgraphql.com/2020/04/14/query-posts-based-on-advanced-custom-field-values-by-registering-a-custom-where-argument/) useless. I'm confined to using the filter argument for my GraphQL queries in Gatsby.
Consider the following query:
query Test {
allWpPage(filter: {pagesGeo: {}}) {
edges {
node {
pagesGeo {
genericPage {
... on WpPage {
id
}
}
hreflangValue
}
}
}
}
}
In this case I want to filter on genericPage, but it's not in the list of available filters in the GraphiQL query tester.
In Wordpress the custom field generic_page is defined with the help of advanced custom fields and it's of the field type 'Post Object'. As you can see I'm able to query the field just fine, and it would be easy for me to create a meta query in Wordpress to filter on the field. It would looks something like:
$query_args['meta_query'] = [
"relation" => "OR",
[
'key' => 'generic_page',
'value' => $postObjectId,
'compare' => '='
],
[
'key' => 'generic_page',
'value' => $postObjectId2,
'compare' => '='
],
];
Is there a way to make it possible for me to filter on genericPage in Gatsby?
If not, are there any alternative solutions for me to extract the data I need?
After a bit debugging I've gathered, and please correct me if I'm
wrong, that Gatsby on build downloads the entire data structure and
caches it. So all the GraphQL queries are performed against the cache.
Yes, that's correct.
And I don't know much about Gatsby or GraphQL coding, but this is what I did and it worked quite well for me:
I created a Gatsby site using the "WordPress blog" starter: https://github.com/gatsbyjs/gatsby-starter-wordpress-blog
Then on my WordPress site, I installed these plugins: ACF (free version, v5.10.2), WP Gatsby v1.1.3, WP GraphQL v1.6.4, "WPGraphQL for Advanced Custom Fields" v0.5.3, and (just for testing) WPGraphQL Meta Query v0.1.0
Then I created an ACF field group named "Pages GEO":
In the GraphQL meta box, I set "Show in GraphQL" to "Yes" and then I set "GraphQL Field Name" to pagesGeo (which is what you used, right?)
Then I created a Post Object field named "Generic Page" (generic_page) and then I set "Show in GraphQL" to "Yes"
And then of course, I created/edited some Pages (i.e. post type page) and then selected a "Generic Page" for those Pages.
So regarding this:
In this case I want to filter on genericPage, but it's not in the list
of available filters in the GraphiQL query tester.
I was not able to add filter.pagesGeo.genericPage, but I managed to filter by that field (i.e. filter.genericPage) by doing this:
I registered my very own custom WP GraphQL field named genericPage
In my theme functions.php, I added:
The code is based on the one here.
add_action( 'graphql_register_types', function() {
register_graphql_field( 'Page', 'genericPage', [
'type' => 'Integer',
'description' => 'generic_page meta value',
'resolve' => function( \WPGraphQL\Model\Post $post ) {
return (int) get_post_meta( $post->databaseId, 'generic_page', true );
}
] );
});
Then in GraphiQL IDE (at http://localhost:8000/__graphql), I ran this query:
So in the above query, I filtered the posts by the generic_page meta, where I queried for posts where the meta value (i.e. the post ID, or databaseId in GraphQL) is 2 or 82.
And I used the in comparator, but you could just use any other comparator that suits your query. See https://www.gatsbyjs.com/docs/query-filters/#supported-comparators
And the result was:
So as you could see, for each node/post (in data.allWpPage.nodes), the root genericPage matches the pagesGeo.genericPage.databaseId.
Additional Notes
I used the gatsby develop command which (if I was not mistaken) rebuilds the cache, but if necessary, the cache can be cleared manually — see https://www.gatsbyjs.com/docs/build-caching/#clearing-cache.
Haven't used this in a while and not sure if this is right. I have a few custom fields with the same key. For example:
weapon -> {"name": "Club"}
weapon -> {"name": "Bow"}
Using this example: https://www.advancedcustomfields.com/resources/get_fields/ I'm trying:
echo($post->ID);
$values = get_fields($post->ID);
var_dump($values);
But it returns 24bool(false). The post id is there but no custom fields. I tried with unique keys like weapon1, weapon2 just to test but it doesn't fix my problem.
What I would like to do is get all custom fields for any key and display them in a loop.
Something like $values = get_fields("weapon", $post->ID);
Doing this only returns one value. I currently have two with same key.
How can this be done?
I'm trying to select posts -- or more specifically, a custom post type -- that belong to a specific term, and display them in my Gutenberg block's edit screen. I'm wanting to recreate what would be tax_query with WP_Query, but in Javascript.
I'm able to select posts, but I'm unsure what parameters to use with getEntityRecords to select by term (or if it is even possible). The documentation is still a little vague at this point.
Here's where I'm at. This successfully selects all posts of the 'rmenu' type:
const items = select("core").getEntityRecords(
"postType",
"rmenu"
);
Does anyone know if getEntityRecords is the right way to handle this?
Thanks.
The third parameter to getEntityRecords is a query object. You can pass any query argument accepted by Wordpress API such as categories or tags. Selecting by category term would look like this:
const items = select("core").getEntityRecords(
"postType",
"rmenu",
{ categories: [ 13 ] }
}
Although Capi's answer is correct, it wasn't clear for me that this would also work for custom taxonomies. It turns out wp.data will add your custom taxonomies automatically as properties to the post object. For example, a post could look like this:
{
title: "hello world",
content: "this is a post",
id: 123,
type: 'my-custom-posttype'
my-custom-tax: [5, 8, 24],
...
}
So, in order to get all the posts of the type my-custom-posttype that have a term with ID 4 or 8 of the taxonomy my-custom-tax, you would run this query:
wp.data.select( 'core' ).getEntityRecords( 'postType', 'my-custom-posttype', { 'my-custom-tax':[4,8] })
Important! If you test this in your browser, you need to run it twice. The first time it will return an empty array, because it only invokes the promise.
I have two entities. Categories and Products. Each product can have many categories and each category can belong to many categories. I would like to be able to return all products and for each one to have all the categories it belongs to and their parent categories. Is datastore a good option for something like this?
example Product response:
{
id: 1,
name: "shoes",
categories: [{
id:1,
ordinal:1
},{
id:2,
ordinal:1
}]
}
I assume you meant 'Datastore' where you wrote 'dataflow'
It depends if you want to query later based on categories or not.
In case you won't need to query based on categories
I'd suggest defining your categories in a model (with the appropriate parent/child relationships) and then adding each category to a product in a LocalStructuredProperty. Datastore will save as a blob but will reconstruct the category entity model when you retrieve the product. You could also add a JsonProperty with a serialized string containing the category structure for each product. For example:
[
0: {
category: 'Electronics',
subcategories: ['Smartphones', 'Telephones', 'Gadgets']
},
1: {
category: 'Apple',
subcategories: ['iPhone']
}
]
Read more about the LocalStructuredProperty and the JsonProperty here (for Python client library).
If you need to query based on categories
Then you should use a StructuredProperty. Create a model to define your categories and their ancestor paths. Then you add one or more categories (along with their parents) to the Product entity when you instantiate it.
The Entity Property Reference in Datastore documentation has a good example of how to implement it (in Python, but also available for other languages). And here's how you filter for StructuredProperty values.
I'm using "Woocommerce CSV Export" plugin and i've just added 2 custom fileds into my WC checkout page, I was wondering if i can add those fileds to the export plugin.
Just add the following code into your "woocommerce-export-csv.php"
add_filter( 'woocommerce_export_csv_extra_columns', 'sites_add_custom_meta', 10);
and the define your function
function sites_add_custom_meta() {
$custom_meta_fields['columns'][] = 'IP Address';
$custom_meta_fields['data'][] = '_customer_ip_address';
return $custom_meta_fields;
}
In this function you have two things you need to define. First, in the columns array you need to define the titles of your custom meta fields and then in the data array you need to define actual meta field name where the data for that field is located at. You need to have the same order in both arrays to be able to have correct information output under right column title.
Source : http://docs.woothemes.com/document/ordercustomer-csv-exporter/#section-4