woocommerce variable products database structure - wordpress

I'm building a wordpress eshop using woocommerce and I want to integrate it with an existing erp tool. I have complete the 90% of all the connections but I'm facing a problem with variable products and I can't find a solution by reading the documentation. I need some more info about how to create a variable product through the erp.
Below is what I'm currently doing:
I am creating an entry in wp_posts with post_type product and for the variation and I'm creating another entry on wp_posts with post_type product_variation and post_parent the ID of the product that holds the variation.
I believe that I'm missing something because doing only the above is not working as expected.

I will write what I am using.
update wp_postmeta set meta_value = 270 where post_id in (
select id from wp_posts where post_parent = 46 and post_type = 'product_variation'
) and meta_key = '_regular_price';
... where 270 is my new price for all variable products.
... using select to test the solution:
select * from wp_postmeta where post_id in (
select id from wp_posts where post_parent = 46 and post_type = 'product_variation'
) and (meta_key = '_regular_price' or meta_key = '_sale_price');
You can see all meta_key related to the wp_posts with:
select distinct meta_key from wp_postmeta where post_id = XX;
In my case, the 46 is the post_id. For you see what is your, open the product in wp-admin.
I hope that I was helpfull.

I think you might be getting the types wrong. Variations are of the post_type "product_variation" not "product".
Also make sure you have the appropriate wp_postmeta values. For example if you have a product variation that has no price it will actually not work during checkout even if it's a free product.
To see what wp_postmeta values you need, simply search that table for a product id that you got from a product that you created through the WC back-end.

Related

Products with no category WordPress Panel

I am facing an issue. Inside wordpress panel at Products, some of them does not have any categories (not even uncategorized). I am playing arround via phpmyadmin Database and don't know if something I did messed up the categories.. How could I regenerate these products to have uncategorized category, or do something about it? Any guess or ideas?
*Furthermore, I am using a MySQL query in order to get the categories of a product via SKU:
$categories = $wpdb->get_results("
SELECT wp_term_relationships.term_taxonomy_id as category_id
FROM $wpdb->term_relationships
INNER JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->term_relationships.object_id
WHERE $wpdb->postmeta.meta_key = '_sku' AND ($wpdb->postmeta.meta_value = '$sku')
");
This query returned some results, but I see no categories in product, like the screenshot I have uploaded. Thanks in advance

Wordpress adds -2 to my post slug

Wordpress adds a -2 to my post slug. I know this can be caused by a post or attachment with the same name. It's a brand new blog with just 3 posts and 3 attachments, I could have checked for duplicates manually but used tools and a query just in case, to delete revisions, etc.
I used plugins Optimize Database after Deleting Revisions, Find Dupicates and Media Deduper, and it didn't work.
Plus I seached for duplicates on phmMyAdmin with query:
SELECT a.ID, a.post_title, a.post_type, a.post_status
FROM wp_posts AS a
INNER JOIN (
SELECT post_title, MIN( id ) AS min_id
FROM wp_posts
WHERE post_type = 'post'
AND post_status = 'publish'
GROUP BY post_title
HAVING COUNT( * ) > 1
) AS b ON b.post_title = a.post_title
AND b.min_id <> a.id
AND a.post_type = 'post'
AND a.post_status = 'publish'
And found no duplicates.
FYI I'm using plugins:
Akismet, Code Snippets, Genesis Simple Edits, Jetpack, and the ones mentioned above for database cleaning.
Any ideas on what I need to do to fix this issue?
I look forward to your replies. Thanks you very much.

Delete variations from 'simple' products

Some of my 'simple' still have variations, I want to delete all existing variations from product with product type 'simple'.
I have created the following sql statement, but I am not sure if this is correct.
I also am struggling to create a delete statement for this.
select * from wp_posts, wp_term_relationships where post_parent = object_id AND term_taxonomy_id =17 and post_type = 'product_variation' ;
Thanks for your help.

Wordpress wpdb query issue - how to filter out revisions?

Can someone tell me how to modify the code below so that it filters out revisions? I tried using revision deletion plugins but they only work for revisions of posts or pages... not custom post types.
<?php
global $wpdb;
global $post;
$review_id = $post->ID;
if (get_post_type($review_id) == "features") {
$assoc_id = get_post_meta($review_id, "associated_hosts_value", true);
$review_id = intval($assoc_id);
}
$assoc = $wpdb->get_col("SELECT DISTINCT post_id from wp_postmeta WHERE (meta_key='associated_hosts_value') AND ((meta_value LIKE ',{$review_id},%') OR (meta_value LIKE '%,{$review_id},%') OR (meta_value = '{$review_id}') OR (meta_value LIKE '%,{$review_id}'));");
Revision is stored in wp_posts under the post_type column, so you will need to inner join on wp_posts and query for a post_type NOT IN ('revision'):
"SELECT DISTINCT post_id from {$wpdb->postmeta} INNER JOIN {$wpdb->posts} ON ID = post_id WHERE post_type NOT IN ('revision') ..."
By the way, inserting raw SQL queries in to get_col is a bad practice, as it leaves you vulnerable to SQL injections. Use $wpdb->prepare() as shown here:
http://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks
Note that you will need to escape the % used in the LIKE statements by changing them to %% to work within prepare().
Also, you shouldn't hard code WP table names in to your query statements. The wpdb class stores table names for use in constructing your queries, i.e. $wpdb->posts, $wpdb->postmeta. Using these assures that your plugin will always query the correct WP tables. Try your code in a multi site installation to find out why this is important.

MYSQL Statement INNER JOIN with multiple condition

I have a WP site where I've also got a few php pages outside of my WP installation which I need to query the database that powers my WP site. I have a working query but it doesn't show what I really need output.
These are the two tables wp_posts and wp_postmeta
wp_posts
ID
post_author
post_date
post_date_gmt
post_content
post_title
post_excerpt
post_status
comment_status
pint_status
post_password
post_name
to_ping
pinged
post_modified
post_modified_gmt
post_content_filtered
post_parent
guid
menu_order
post_type
post_mine_type
comment_count
wp_postmeta
meta_id
post_id
meta_key
meta_value
and these is the query that show the publish post:
$query = "SELECT post_title, post_name FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 5";
and these is the query that show with the meta_key= '_wp_attached_file'
$query ="SELECT post.ID, post.post_title, post.post_excerpt, post.post_content, meta.meta_value FROM wp_posts AS post INNER JOIN wp_postmeta AS meta ON meta.post_id = post.id AND meta.meta_key = '_wp_attached_file' ";
what I want is to combine these two query to show post_status ='publish' and the meta_key = '_wp_attached_file'
here is the example of my query but doesn't show value
<?php
$query ="SELECT post.ID, post.post_title, post.post_excerpt, post.post_content, meta.meta_value FROM wp_posts AS post INNER JOIN wp_postmeta AS meta ON meta.post_id = post.id AND meta.meta_key = '_wp_attached_file' WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 5 ";
$result = mysql_query($query,$link);
if(mysql_num_rows($result)) {
while($post = mysql_fetch_object($result)) {
echo "$post->meta_value";
echo "$post->post_title";
echo "$post->post_content";
}
}
?>
many thanks guys.. .
another problem it show only 1 post and does not latest post
here is my updated code
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("wp_db",$con);
$sql = "SELECT
posts.ID,
posts.post_title AS title,
posts.post_content AS content,
files.meta_value AS filepath
FROM
wp_posts posts
INNER JOIN wp_posts attachments ON posts.ID = attachments.post_parent
INNER JOIN wp_postmeta files ON attachments.ID = files.post_id
WHERE 1 = 1
AND files.meta_key = '_wp_attached_file'";
$result = mysql_query($sql,$con);
while ($row = mysql_fetch_object($result))
{
echo $row->title . "<br />";
}
mysql_close($con);
?>
do I get something wrong with my code.. .thank for help thank you so much.. .
Hi #idontknowhow:
Your problem appears to be invalid assumptions regarding WordPress' database schema. WordPress stores it's attachments as a post_type='attachment'. Run this query to see what I mean:
SELECT
wp_posts.ID,
wp_posts.post_type
FROM
wp_posts
INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
WHERE 1 = 1
AND wp_postmeta.meta_key = '_wp_attached_file'
Records in wp_posts with a post_type='attachment' will typically have 'post_status='inherit' and 'post_parent' equal to your post's ID field value.
Now I didn't completely follow what you were looking to accomplish. You mentioned the technical problems you were having and but not the actual result you were trying to achieve. Without knowing the latter it's hard for me to verify that I understood your question. So I may have misunderstood what you were looking for, but I think this is what you wanted:
SELECT
posts.ID,
posts.post_title AS title,
posts.post_content AS content,
files.meta_value AS filepath
FROM
wp_posts posts
INNER JOIN wp_posts attachments ON posts.ID = attachments.post_parent
INNER JOIN wp_postmeta files ON attachments.ID = files.post_id
WHERE 1 = 1
AND files.meta_key = '_wp_attached_file'
But using direct SQL is frowned upon in the WordPress world; instead use of the WordPress API is the preferred choice if it provides what you need. There are many reasons for this; i.e. to avoid breakage if future WordPress versions change the database schema and because WordPress does a lot of data caching so it might be more performant in everyday usage to not use direct SQL and to use the WordPress API instead.
However, it is not 100% straightforward to use the WordPress API to address your question, though it can be done. If you'd like to see how may I suggest asking over at StackOverflow's sister site WordPress Answers where lots of WordPress enthusiasts can help?
Hope this helps.
-Mike
P.S. I find that a lot of questions about querying the WordPress database here on StackOverflow have people attempt to answer them even though they lack knowledge of the WordPress database schema and more importantly, are not aware of the WordPress API. People here know MySQL really well but often don't know WordPress well enough to give the right answer on WordPress-related questions. WordPress Answers is probably a better place to ask questions about WordPress.
Your query looks correct.
Are you sure you have published posts with _wp_attached_file?
Do the outputs of your queries above have any common records?
Did you tryed to filter mete.meta_key on WHERE? Something like that:
SELECT post.ID, post.post_title, post.post_excerpt,
post.post_content, meta.meta_value
FROM wp_posts AS post
INNER JOIN wp_postmeta AS meta ON meta.post_id = post.id
WHERE meta.meta_key = '_wp_attached_file'
AND post.post_type = 'post'
AND post.post_status = 'publish'
ORDER BY post.post_date DESC
LIMIT 5;

Resources