Home > Software design >  Woocommerce - Latest modified date
Woocommerce - Latest modified date

Time:06-18

Previously I have added the PHP Snippet to make my single product page shows the latest modified date.

However, I have to rebuild the single product page with Elementor pro (Theme builder) for some reason. The code is not working anymore on the single product page.

Appreciate it if anyone could help with this. Thanks in advance.

    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 "<br>";
        echo "<br>";
        echo sprintf( '<p>' .__ ( 'Date modified: %s', 'woocommerce' ) . '</p>', $product->get_date_modified()->date( 'Y-m-d' ) );
    }
}
// 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 );

CodePudding user response:

Working with Elementor and the standard WooCommerce hooks can be a hassle because some hooks don't exist or need to be placed in the single product template. You have 2 alternative options:

  1. Make sure that this hook is there by placing the correct product widget in your template which contains this hook.

  2. Modify your code: Create a shortcode instead of a hook and place the shortcode where you want in your template:

    add_shortcode( 'my_custom_product_date', woocommerce_product_date' );

Put [my_custom_product_date]in your Elementor template.

  • Related