3
votes

I'm looking for solution to display products current date on the shop page.

I have tried with this, but it shows nothing at all

add_action( 'woocommerce_single_product_summary','bloomer_echo_product_date',25 );
 
function bloomer_echo_product_date() {
if ( is_product() ) {
   echo get_the_date('', '<span class="date_published">Updated on: ', '</span>', false);
   }
}

Any help is appreciated.

1

1 Answers

2
votes

The following code shows the date the product was created and modified on the single product page and archive/Shop page

Note: the time/date display can be adjusted based on DateTime::format PHP function.

Currently used:

  • Y - A full numeric representation of a year, 4 digits
  • m - Numeric representation of a month, with leading zeros
  • d - Day of the month, 2 digits with leading zeros
  • H - 24-hour format of an hour with leading zeros
  • i - Minutes with leading zeros
  • s - Seconds with leading zeros

So you get:

function woocommerce_product_date() {
    // Get the global product object
    global $product;

    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Output date created & date modified
        echo sprintf( '<p>' . __( 'Date created: %s', 'woocommerce' ) . '</p>', $product->get_date_created()->date( 'Y-m-d H:i:s' ) );
        echo sprintf( '<p>' . __( 'Date modified: %s', 'woocommerce' ) . '</p>', $product->get_date_modified()->date( 'Y-m-d H:i:s' ) );
    }
}
// Display on single product page
add_action( 'woocommerce_single_product_summary', 'woocommerce_product_date', 10, 0 );
// Display on archive/shop page
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_product_date', 10, 0 );