Here is the thing. WordPress or woocommerce don’t store price update date. But as we know when you update price you need to save the product and that changes product last modified time. So you can show that date as price update date.
you can use bellow code to show that date. Read more about the action here.
add_action( 'woocommerce_after_shop_loop_item', 'after_shop_loop_item_action_callback', 20 );
function after_shop_loop_item_action_callback() {
global $product;
echo '<br><span class="date_modified">' . $product->get_date_modified()->date('F j, Y, g:i a') . '</span>';
}
To Do this using a custom field
Register the custom field in dashboard. It will show in General Tab.
/**
* Display the date picker in dashboard
* @since 1.0.0
*/
function cfwc_price_update_time_field() {
$args = array(
'id' => 'cf_price_update_time',
'label' => __( 'Price Update Date', 'cfwc' ),
'class' => 'cfwc-price-update',
'type' => 'date',
'desc_tip' => true,
'description' => __( 'Enter last price update date', 'ctwc' ),
);
woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_general_product_data', 'cfwc_price_update_time_field' );
/**
* Save the date picker data
* @since 1.0.0
*/
function cfwc_save_price_update_time_field( $post_id ) {
$product = wc_get_product( $post_id );
$title = isset( $_POST['cf_price_update_time'] ) ? $_POST['cf_price_update_time'] : '';
$product->update_meta_data( 'cf_price_update_time', sanitize_text_field( $title ) );
$product->save();
}
add_action( 'woocommerce_process_product_meta', 'cfwc_save_price_update_time_field' );
/**
* Display the date on the front end
* @since 1.0.0
*/
function cfwc_display_price_update_time() {
global $post;
// Check for the custom field value
$product = wc_get_product( $post->ID );
$title = $product->get_meta( 'cf_price_update_time' );
$data = date_format(date_create($title),"d/m/Y");
if( $data ) {
printf('<span class="cfwc-custom-field-wrapper"> Price updated at %s</span>',
esc_html( $data )
);
}
}
add_action( 'woocommerce_after_add_to_cart_button', 'cfwc_display_price_update_time' );