0

I wanted to show price update date in Woocommerce product single page after add to cart button. something like this: " Price updated at 5/6/2020 "

I tried to research but I did not find a good response. Thanks for helping.

https://wordpress.stackexchange.com/a/216653/189455 this is also a good reference but I could not figure it out how to use and do what he said in code.

1
  • Hi, welcome to WordPress Development. As a rule, the community considers questions about third-party plugins (such as woocommerce) off-topic so you might notice some down-votes and answers may be slow if they come at all. It is felt that plugin questions should go to the plugin author.You can find out what questions are a good fit here. – Matthew Brown aka Lord Matt Jun 6 '20 at 14:56
0

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' );
4
  • Thank you for helping me. But I exactly need to show the price update time. not the product content update time :( – Reza Jun 5 '20 at 18:03
  • You can follow another method. Try adding a custom metabox to price tab. You will input last update time there. In front end you will show that meta value. – Sabbir Hasan Jun 5 '20 at 19:45
  • can you help me with the code? i cannot really figure it out how to code it. – Reza Jun 5 '20 at 21:07
  • @Reza I've updated my answer. Please re-check. – Sabbir Hasan Jun 6 '20 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.