Woocommerce delete products which are "Out of stock" - php

So, as the title, my question is how I can:
Delete all products which are "Out of stock", not only to hide them, but to delete from the DB.
I found the following code but it is deleting all products, not only those that are out of stock:
DELETE p FROM wp_posts p WHERE p.post_type = 'product';
DELETE pm FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL;
DELETE tr FROM wp_term_relationships tr INNER JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.taxonomy != 'link_category' AND tr.object_id NOT IN ( SELECT ID FROM wp_posts );

Products is a custom post type, you can delete "out of stock" products by selecting such products using meta query from within WordPress
However if you want to go the SQL Query way, the following query will do the job
DELETE p FROM wp_posts p join wp_postmeta pm on p.ID = pm.post_id WHERE p.post_type = 'product' and pm.meta_key='_stock_status' and pm.meta_value='outofstock';

Try this :
DELETE p FROM wp_posts p WHERE p.post_type = 'product' and p.id = '$post->ID';
EDITED :
DELETE p FROM wp_posts p join wp_postmeta m on p.ID = m.post_id WHERE p.post_type = 'product' and m.meta_key='_stock' and m.meta_value='0';

Related

Get product ids from specific product attributes values using SQL in Woocommerce

I am trying to get product ids where product attributes pa_color = 'Black' and pa_waterproof = 'Yes'. But I have not succeeded yet.
This is my code:
global $wpdb;
$product_id = $wpdb->get_results( "
SELECT post_id
FROM $wpdb->postmeta
WHERE meta_key='pa_color'
AND meta_value='Black'
AND meta_key='pa_waterproof'
AND meta_value='Yes'
" );
Any help is appreciated
Update (related to your comment)
You are may be trying to get the variable product ID instead which is not the same thing.
Then you can use normal product attribute taxonomy and term names values (or even term Ids or term slugs).
In this case try the following:
global $wpdb;
$results = $wpdb->get_col( "
SELECT p.ID
FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}term_relationships as tr ON p.ID = tr.object_id
INNER JOIN {$wpdb->prefix}term_taxonomy as tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id
INNER JOIN {$wpdb->prefix}term_relationships as tr2 ON p.ID = tr2.object_id
INNER JOIN {$wpdb->prefix}term_taxonomy as tt2 ON tr2.term_taxonomy_id = tt2.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms as t2 ON tt2.term_id = t2.term_id
WHERE p.post_type LIKE 'product'
AND p.post_status LIKE 'publish'
AND tt.taxonomy LIKE 'pa_color'
AND t.name = 'Black'
AND tt2.taxonomy LIKE 'pa_waterproof'
AND t2.name = 'Yes'
");
// Raw output
echo '<pre>'; print_r($results); echo '</pre>';
Tested and works
First answer.
Get product variation ids from specific product attribute values using SQL in Woocommerce
In the data base the product attribute registered terms for product variations in the post_meta table are term slugs (not term names) and all product attributes meta_key start with attribute_pa_ instead of pa_.
So it will be attribute_pa_color = 'black' and attribute_pa_waterproof = 'yes' instead.
Also you need to make a SQL query on wp_posts table for product_variation post type Joining 2 times the wp_postmeta table to query each product attribute.
So try:
global $wpdb;
$variation_id = $wpdb->get_var( "
SELECT p.ID FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
INNER JOIN {$wpdb->prefix}postmeta as pm2 ON p.ID = pm2.post_id
WHERE p.post_type LIKE 'product_variation'
AND p.post_status LIKE 'publish'
AND pm.meta_key LIKE 'attribute_pa_color'
AND pm.meta_value = 'black'
AND pm2.meta_key LIKE 'attribute_pa_waterproof'
AND pm2.meta_value = 'yes'
");
// Output
echo $variation_id . '<br>';
Tested and works

Remove duplicate items from Woocommerce

I had problem during importing products. And now some of the products are duplicated. All of the duplicated products have the same title and attribute (collection_id). Also there are a lot of duplicated media files. Is there a way to remove duplicated products? At least i want to remove products.
I had the same problem and it was I did.
1.- In my database found duplicated products (searching by sku) with the query:
SELECT meta_value, count(*) AS total FROM wp_postmeta WHERE meta_key = '_sku' GROUP BY meta_value HAVING total > 1
2.- I create a new table called wp_repeated with these skus:
CREATE TABLE wp_repeated AS SELECT meta_value, count(*) AS total FROM wp_postmeta WHERE meta_key = '_sku' GROUP BY meta_value HAVING total > 1
3.- I got the post_id from these skus:
SELECT p.ID FROM `wp_posts` as p INNER JOIN wp_postmeta as m on p.ID = m.post_id WHERE p.post_type IN ('product','product_variation') AND m.meta_key = '_sku' AND m.meta_value in (select meta_value from wp_repeated)
4.- And, finally, remove and delete from all tables where the post_id appears:
DELETE relations.*, taxes.*, terms.* FROM wp_term_relationships AS relations INNER JOIN wp_term_taxonomy AS taxes ON relations.term_taxonomy_id=taxes.term_taxonomy_id INNER JOIN wp_terms AS terms ON taxes.term_id=terms.term_id WHERE object_id IN (SELECT p.ID FROM `wp_posts` as p INNER JOIN wp_postmeta as m on p.ID = m.post_id WHERE p.post_type IN ('product','product_variation') AND m.meta_key = '_sku' AND m.meta_value in (select meta_value from wp_repeated)); DELETE FROM wp_postmeta WHERE post_id IN ( SELECT p.ID FROM `wp_posts` as p INNER JOIN wp_postmeta as m on p.ID = m.post_id WHERE p.post_type IN ('product','product_variation') AND m.meta_key = '_sku' AND m.meta_value in (select meta_value from wp_repeated)); DELETE FROM wp_posts WHERE ID IN (SELECT p.ID FROM `wp_posts` as p INNER JOIN wp_postmeta as m on p.ID = m.post_id WHERE p.post_type IN ('product','product_variation') AND m.meta_key = '_sku' AND m.meta_value in (select meta_value from wp_repeated));
I hope it can be helpful to you. Regards.
TAKE CARE: these queries remove ALL repeated products.

Show wordpress post thumbnail with SQL query interacting with wp_post, wp_term_relationships and wp_terms

I have this query that when I execute it, it pulls 3 single posts, which I chose based on their id. The only thing that doesn't get pulled is the post thumbnail. I've tried intercepting with post_meta joining with post_id etc, but no success at all
Here's what I got until now
SELECT *
FROM wp_posts p
#LEFT JOIN wp_postmeta pm ON (pm.post_id = p.ID AND pm.meta_value IS NOT NULL AND pm.meta_key = '_thumbnail_id')
#LEFT JOIN wp_postmeta pm ON (pm.meta_value = pm.post_id AND pm.meta_key = '_wp_attached_file' AND pm.meta_value IS NOT NULL)
#LEFT JOIN wp_postmeta pm ON p.ID = pm.post_id AND pm.meta_key = '_wp_attachment_metadata'
LEFT OUTER JOIN wp_term_relationships r ON r.object_id = p.ID
LEFT OUTER JOIN wp_terms t ON t.term_id = r.term_taxonomy_id
WHERE p.ID in ('800','808', '569')
AND p.post_type in ('post', 'attachment')
AND p.post_status = 'publish'
The ones with hash-tags are my failed attempt, I get null value on meta_key column

how can select all post relevant to category in wordpress?

How can select all posts related to one category in wordpress by mysql query and echo by json format ?
I hope this MySQL will serve your purpose.
SELECT *
FROM wp_posts p
LEFT OUTER JOIN wp_term_relationships r ON r.object_id = p.ID
LEFT OUTER JOIN wp_terms t ON t.term_id = r.term_taxonomy_id
WHERE p.post_status = 'publish'
AND p.post_type = 'post'
AND t.slug = 'your_category_slug'
Just replace your_category_slug with your category slug, of which category post u want.
I hope the following code will help you to get data posts from specific category.
<?php
global $wpdb;
$query = "SELECT *
FROM wp_posts p
LEFT OUTER JOIN wp_term_relationships r ON r.object_id = p.ID
LEFT OUTER JOIN wp_terms t ON t.term_id = r.term_taxonomy_id
WHERE p.post_status = 'publish'
AND p.post_type = 'post'
AND t.slug = 'your_category_slug'";
$results = $wpdb->get_results( $query, OBJECT );
?>

Combine 2 Queries with different number of rows

I have a query that connects to a wordpress db and returns some posts.
In order to get the image for each of these posts i run another query inside a php foreach loop
The problem is that running the second query inside the foreach loop is extremely slow and i need another way to merge these 2 queries into 1.
The first query
SELECT pm. * , p.*
FROM wp_posts p
JOIN wp_postmeta pm ON pm.post_id = p.ID
WHERE pm.meta_key = 'accommodation_location_post_id'
AND pm.meta_value
IN (
SELECT pi.ID
FROM wp_posts pi
WHERE pi.post_title LIKE '%Cyprus%')
Based on the returned post ids, i need for each of these ids
the featured image.
This query does this job, but only for 1 id only.
The second query
SELECT wp_posts.guid
FROM wp_posts
WHERE wp_posts.ID =
(Select wp_postmeta.meta_value
FROM wp_postmeta
WHERE wp_postmeta.meta_key =
'_thumbnail_id' AND wp_postmeta.post_id = 'The id of each post')
I need a query to return all the posts alongside with its images.
I really don't understand your data structure, but try this query
select
t1.*, t2.guid
from (
SELECT pm. * , p.*
FROM wp_posts p
JOIN wp_postmeta pm ON pm.post_id = p.ID
WHERE pm.meta_key = 'accommodation_location_post_id'
AND pm.meta_value IN (
SELECT pi.ID
FROM wp_posts pi
WHERE pi.post_title LIKE '%Cyprus%'
)
) t1
left join (
select
p2.guid, pm2.post_id
from
wp_posts p2
join wp_postmeta pm2 on
pm2.meta_value = p2.ID
and pm2.meta_key = '_thumbnail_id'
) t2 on t2.post_id = t1.ID

Categories