I cannot find the meta key for the stock quantity in Woocommerce. What would be the best way to update the stock quantity in Woocommerce using MYSQL?
it's more secure you use the PHP functions. But if you want to update using sql, you can try this:
Checking stock
SELECT * FROM olenka.wp_postmeta where meta_key = '_stock';
Checking stock status:
SELECT post_id AS id, meta_value AS stockstatus FROM wp_postmeta WHERE meta_key = '_stock_status'
Set manage stock true:
UPDATE wp_postmeta SET meta_value = 'yes' WHERE meta_key = '_manage_stock'
Set the 'instock' flag:
UPDATE wp_postmeta set meta_value = 'instock' WHERE meta_key = '_stock_status' AND meta_value = 'outofstock'
Update the stock
UPDATE wp_postmeta SET meta_value = 99 WHERE meta_key = '_stock' AND meta_value IS NULL;
OR
UPDATE wp_postmeta SET meta_value = 99 WHERE meta_key = '_stock' AND meta_value > 0;
Related
I have some duplicated rows on my Wordpress wp_postmeta table. Actually hundreds of cases where old postmeta data are listed 2 or 3 times... maybe from some data import process done in the past... So I need to remove unneeded duplicate rows from wp_postmeta table, leaving just the ones with higher meta_id number... To exemplify what the wp_postmeta table looks like:
meta_id | post_id | meta_key | meta_value
155153 | 177115 | owner_img | https://www.example.com/a.jpg
176231 | 177115 | owner_img | https://www.example.com/a.jpg
193983 | 177115 | owner_img | https://www.example.com/a.jpg
Note that these are 3 metadata for the same post on wp_post table (as it has the same post_id)... so I just need to keep the latest metadata row, and delete all other instance where metadata is duplicated for each meta_key... how can I do that?
DELETE wp_postmeta.*
FROM wp_posts
INNER JOIN wp_postmeta ON wp_postmeta.post_ID = wp_posts.ID
I was able to figure it out after many research, in case anyone out there is looking for the answer...
DELETE t1 FROM wp_postmeta t1
INNER JOIN wp_postmeta t2
WHERE t1.meta_id < t2.meta_id
AND t1.meta_key = t2.meta_key
AND t1.post_id=t2.post_id;
You can do the deletion by first filtering out the lower valued meta_id
DELETE wp_posts
FROM wp_posts
JOIN wp_posts x ON x.post_ID = wp_posts.post_ID
AND wp_posts.meta_id < x.meta_id
WHERE x.post_id=wp_posts.post_id
AND x.meta_key = wp_posts.meta_key
exercise:
create table wp_posts (meta_id integer,
post_id integer,
meta_key varchar(20),
meta_value varchar(200)
);
insert into wp_posts values(155153 , 177115 , 'owner_img','https://www.example.com/a.jpg');
insert into wp_posts values(176231 , 177115 , 'owner_img' , 'https://www.example.com/a.jpg');
insert into wp_posts values(193983 , 177115 , 'owner_img' , 'https://www.example.com/a.jpg');
commit;
-- SELECT TO CHECK/VERIFY
Select distinct wp_posts.meta_id
FROM wp_posts
JOIN wp_posts x ON x.post_ID = wp_posts.post_ID
AND wp_posts.meta_id < x.meta_id
WHERE x.post_id=wp_posts.post_id
AND x.meta_key = wp_posts.meta_key
OUTPUT
meta_id
155153
176231
Delete
Delete wp_posts
FROM wp_posts
JOIN wp_posts x ON x.post_ID = wp_posts.post_ID
AND wp_posts.meta_id < x.meta_id
WHERE x.post_id=wp_posts.post_id
AND x.meta_key = wp_posts.meta_key
;
commit;
Select * from wp_posts;
OUTPUT
meta_id post_id meta_key meta_value
193983 177115 owner_img https://www.example.com/a.jpg
I need to be able change Stock levels to "Out of stock" or stock quanity to 0 to products that hasn't been updated.
So far i made 2 separate codes to update stock and to delete not-updated products, but i need to combine them
DELETE FROM `wp_posts` WHERE `post_modified` < "2019-05-14"
UPDATE wp_postmeta SET meta_value = 'outofstock' WHERE meta_value = 'instock' AND meta_key = '_stock_status'
Incidentally, your query (incorrectly posted as a comment) can be rewritten as follows:
UPDATE wp_postmeta x
JOIN wp_posts y
ON y.id = x.post_id
SET x.meta_value = 'outofstock'
WHERE x.meta_value = 'instock'
AND x.meta_key = '_stock_status'
AND y.post_type = 'product'
AND y.post_status = 'publish'
AND y.post_modified < "2019-05-14"
I have this query:
SELECT *
FROM `wp_postmeta`
WHERE `meta_key` = '_test'
AND `post_id` IN (SELECT post_id FROM `wp_postmeta`
where meta_value = 8023)
Returns the SQL error:
Table 'wp_postmeta' is specified twice, both as a target for 'UPDATE'
and as a separate source for data
I have read other answers and attempting to add a further SELECT * FROM ( ) around the sub query but didn't help.
I assume I need some form of AS in here but can't figure out the exact code.
Can you rewrite the query in the format that won't trigger the error?
The same for this similar query:
UPDATE wp_postmeta
SET meta_value = 5.55
WHERE meta_key = '_regular_price'
AND post_id IN (
SELECT post_id
FROM wp_postmeta
WHERE meta_value = 8023
)`
You can use alias for table name
(and as suggestion in your case you can also use join instead of in )
SELECT a.*
FROM `wp_postmeta` a
inner join `wp_postmeta` b on a.`post_id = b.post_id
where a.`meta_key` = '_test'
and b.meta_value = 8023
In update you could use a join with subselect for circumvent the limits due to update actions on the same table
UPDATE wp_postmeta a
inner join (
SELECT post_id
FROM wp_postmeta
WHERE meta_value = 8023
) t on a.`post_id = t.post_id and a.`meta_key` = '_test'
SET meta_value = 5.55
SELECT * FROM wp_postmeta as wp_out
WHERE wp_out.meta_key = '_test'
AND wp_out.post_id IN
( SELECT wp_in.post_id
FROM wp_postmeta as wp_in
where wp_in.meta_value = 8023)
Although this is not a good answer, but it is effective, ha ha..
SELECT *
FROM wp_postmeta
WHERE meta_key = '_test'
AND post_id IN (SELECT GROUP_CONCAT(post_id) FROM wp_postmeta
where meta_value = 8023)
The SELECT query posted in the question is equivalent to this one:
SELECT p1.*
FROM `wp_postmeta` p1
INNER JOIN `wp_postmeta` p2 ON p1.`post_id` = p2.`post_id`
WHERE p1.`meta_key` = '_test'
AND p2.`meta_value` = 8023
In fact, if some conditions are met, the MySQL engine converts the original SELECT query into a query similar to this as an optimization.
This SELECT query can be easily changed into the desired UPDATE query:
UPDATE `wp_postmeta` p1
INNER JOIN `wp_postmeta` p2 ON p1.`post_id` = p2.`post_id`
SET p1.`meta_values` = 5.55
WHERE p1.`meta_key` = '_test'
AND p2.`meta_value` = 8023
I've a table called WP_POSTMETA with a column META_KEY and a column META_VALUE.
I'd like to multiply the META_VALUE '_PRICE' with a factor 1.5
Who can help me with the right MYSQL query? I have to multiply prices for about 8000+ items and want to update them all in one action...
Please see also
You can try like this:-
Update WP_POSTMETA
SET META_VALUE = Meta_Value*1.5
WHERE META_KEY = '_PRICE'
Assuming I understand correctly...
You want to update the Meta_value * 1.5 where the meta_key = '_PRICE'
Update WP_POSTMETA
SET META_VALUE = Meta_Value*1.5
WHERE META_KEY = '_PRICE'
To view the results before you run the above update. update..
Select MEta_value*1.5 as newVal, Meta_value, Meta_key
from WP_POSTMETA
WHERE META_KEY = '_PRICE'
Or like this with a transaction:
BEGIN;
Update WP_POSTMETA
SET META_VALUE = Meta_Value*1.5
WHERE META_KEY = '_PRICE'
Select MEta_value*1.5 as newVal, Meta_value, Meta_key
from WP_POSTMETA
WHERE META_KEY = '_PRICE'
If you're happy with the result
COMMIT;
Else do a rollback
ROLLBACK;
Actually you should do all statements that modifies production data in a transaction.
I have a table with 4 values, meta_id, post_id, meta_key and meta_value, and I want to change all meta_values found as "yes" to "si" when the meta_key is stock_available... how do I do this?. I cannot even retrieve the rows at this point...I'm trying with something like this.
SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key` AND `meta_value` = 'yes'
Could I have some help?
EDIT: I had forgotten the meta_key...
SELECT * FROM `wp_postmeta` WHERE `meta_key` = 'stock_available' AND `meta_value` = yes'
So I retrieve these... btu how do I update them?
You need to use the SQL UPDATE statement:
UPDATE wp_postmeta SET meta_value = 'si' WHERE meta_value = 'yes' AND meta_key = 'stock_available'
Before you do that, run this SELECT to make sure that you are going to be updating the correct rows:
SELECT * FROM wp_postmeta WHERE meta_value = 'yes' AND meta_key = 'stock_available'
UPDATE wp_postmeta
SET meta_value = 'si'
WHERE meta_key = 'stock_available'
AND meta_value = 'yes';