Skip to content

For developers

Displays the lowest product price in the last 30 days.

Usage:

// In the product template
echo do_shortcode( '[wpify_woo_lowest_price]' );

Output: Formatted price using wc_price().

Programmatic retrieval of the lowest price

Section titled “Programmatic retrieval of the lowest price”
// Get module instance
$prices_log = wpify_woo_container()->get(
\WpifyWoo\Modules\PricesLog\PricesLogModule::class
);
// Get lowest price
$lowest_price = $prices_log->get_lowest_price( $product_id );

The module stores price history in its own table:

ColumnTypeDescription
idINTPrimary key
product_idINTProduct ID
regular_priceVARCHARRegular price (stored as text)
sale_priceVARCHARSale price (stored as text)
created_atVARCHAREntry date (format Y-m-d H:i:s)
// Get repository
$repository = wpify_woo_container()->get(
\WpifyWoo\Modules\PricesLog\PricesLogRepository::class
);
// All product entries
$logs = $repository->find_by_product_id( $product_id );
// Last entry
$last = $repository->get_last_by_product_id( $product_id );
// Lowest price in 30 days
$lowest = $repository->find_lowest_price( $product_id );

The Prices log module provides data for the Prices module (price type “Lowest price in 30 days”):

// In the Prices module:
$module = wpify_woo_container()->get(
\WpifyWoo\Modules\PricesLog\PricesLogModule::class
);
$price['value'] = $module->get_lowest_price( get_the_ID() );
add_action( 'woocommerce_single_product_summary', function() {
global $product;
if ( ! $product->is_on_sale() ) {
return;
}
$prices_log = wpify_woo_container()->get(
\WpifyWoo\Modules\PricesLog\PricesLogModule::class
);
$lowest = $prices_log->get_lowest_price( $product->get_id() );
if ( $lowest && $lowest < $product->get_regular_price() ) {
printf(
'<div class="omnibus-price">%s: %s</div>',
esc_html__( 'Lowest price in 30 days', 'my-theme' ),
wc_price( $lowest )
);
}
}, 15 );
function export_price_history( $product_id ) {
$repository = wpify_woo_container()->get(
\WpifyWoo\Modules\PricesLog\PricesLogRepository::class
);
$logs = $repository->find_by_product_id( $product_id );
$data = [];
foreach ( $logs as $log ) {
$data[] = [
'date' => $log->created_at,
'regular_price' => $log->regular_price,
'sale_price' => $log->sale_price,
];
}
return $data;
}