13

I'm looking for the location of a product's prices according to the option which the user selects in the product price. For example, this WooCommerce shop has a select which prices change according to the user selection.

I want to know where are prices for different options stored in DB.

Thanks

1
  • 2
    A product is a custom post which post_type is "product". In database you have to look in posts table which post_type is product. For each post_id (product id) you can find all related data to this product in postmeta table: SELECT * FROM 'postmeta' WHERE 'post_id' = nnnn (nnnn is the number ID of one product). All the different prices for each product are store in "postmeta" table. Commented Apr 2, 2016 at 21:46

4 Answers 4

13

All the data like different prices of a product custom post type are store (for each product) in postmeta table.

To find the post id of all products you have to use this query on posts table:

SELECT *  FROM 'posts' WHERE 'post_type' = 'product'

For each product id (post_id), you can retrieve all related data with this query on postmeta table:

SELECT * FROM 'postmeta' WHERE 'post_id' = nnnn

(nnnn is the number id (post_id) of a product)

You will get the list of all product properties metakey and metavalues.
For related price meta_key(s) you have, for example:
- _regular_price
- _sale_price
- _price
- …

To get a particular value of a product meta_key, you can use the wordpress function:

get_post_meta($post_id, '$meta_key');

4
  • In Woocommerce, what is the difference between _regular_price and _price?
    – ptrcao
    Commented Jan 22, 2019 at 20:23
  • 2
    @ptrcao "_price" is the active price (that can be the regular price or the sale price if defined). "_regular_price" is the price that you enter in your product, under regular price. Commented Jan 22, 2019 at 20:26
  • Thanks - and where/how does one define the "active" price?
    – ptrcao
    Commented Jan 22, 2019 at 20:30
  • 4
    @ptrcao If the product is on sale (with a defined and active sale price), the active price "_price" is equal to the sale price (so different than the regular price). If the product is not on sale the active price "_price" is equal to the regular price. Commented Jan 22, 2019 at 20:33
4

As LoicTheAztec told, the product data lives in the posts and postmeta tables. Here is a query, I used to get the prices in my environment (WC Role Based Price installed)

SELECT
  wpp.ID,
  wppm.meta_key AS FIELD,
  wppm.meta_value AS VALUE,
  wppm.*
FROM wp_posts AS wpp
  LEFT JOIN wp_postmeta AS wppm
    ON wpp.ID = wppm.post_id
WHERE wpp.post_type = 'product'
      AND (wppm.meta_key = '_regular_price'
      OR wppm.meta_key = '_sale_price'
      OR wppm.meta_key = '_price'    
      OR wppm.meta_key = '_product_attributes')
ORDER BY wpp.ID ASC, FIELD ASC, wppm.meta_id DESC;

Perhaps this is helpful.

1

Ηere are three meta_keys in postmeta table.

[ _sale_price, _regular_price, _price ]

First array contains value and second array contains WHERE condition. You can add more conditions.

You can update values using the following code.

//update _price
$wpdb->update( 
    $wpdb->postmeta, 
    array( 'meta_value' => $default_product_price ), 
    array( 'meta_key' => '_price' )
);
//update _regular_price
$wpdb->update( 
    $wpdb->postmeta, 
    array( 'meta_value' => $default_product_price ), 
    array( 'meta_key' => '_regular_price' )
);
//update _price
$wpdb->update( 
    $wpdb->postmeta, 
    array( 'meta_value' => $default_sale_price ), 
    array( 'meta_key' => '_sale_price' )
);
1
  • Queries are incomplete. It is essential to set in the second array (WHERE clause) the post_id otherwise the same price will be set to all posts (we assume products). Commented Nov 22, 2023 at 19:10
0

This prints product title and it's price.

 SELECT distinct p.id, p.post_title,pm.meta_key, pm.meta_value 
 FROM {db_prefix}_postmeta pm 
inner join {db_prefix}_posts p on p.id= pm.post_id
WHERE pm.meta_key = '_sale_price'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.