'Get data from database into custom field

In our country we have to appear the discounted product previous sales price. (cheapest sales price from the last 30 days)

I found a plugin on github, which is saving the previous product prices, and save it into woocommerce_prices_history_products database table.

I created a custom field in the available product variation section. Is it possible to get data from that database table?

Other question. The plugin saves prices, but I need a filter to appear the cheapest price from the last 30 days.

https://github.com/pogla/Woocommerce-Save-Product-Price-History

Any idea how can i do this?



Solution 1:[1]

Yes, you can use $wpdb global object to get the value from desired table. You can do something like this:

$product_id = xxx; //PUT your product id here
$serialized_data = $wpdb->get_var( "SELECT data FROM woocommerce_prices_history_products WHERE product_id=$product_id" );
$price_array = unserialize($serialized_data);

With this array you can iterate, check that saved date is less than 30 days and retrieve the cheaper price:

foreach($price_array as $date => $price) {
  //Check that $data is less than 30 days

  //Check that $price['s_p'] is the cheapest one.
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Skatox