Snippets

Visser Labs Showing conditional notices on the Single Product and My Account screen #3

Updated by Michael Visser

File functions.php Modified

  • Ignore whitespace
  • Hide word diff
 // Conditionally display a persistent notice on the My Account screen
 function custom_woocommerce_before_my_account() {
 
-
 	// Get all the Orders associated with this User
 	$args = array(
 		'numberposts' => -1,
Created by Michael Visser

File functions.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+
+// Conditionally display a persistent notice on the My Account screen
+function custom_woocommerce_before_my_account() {
+
+
+	// Get all the Orders associated with this User
+	$args = array(
+		'numberposts' => -1,
+		'meta_key'    => '_customer_user',
+		'meta_value'  => get_current_user_id(),
+		'post_type'   => wc_get_order_types(),
+		'post_status' => array_keys( wc_get_order_statuses() ),
+	);
+	$customer_orders = get_posts( $args );
+
+	// Bail out if there are no Orders linked to this User
+	if( empty( $customer_orders ) )
+		return;
+
+    // Bail out if less than 3 Orders have been made
+    $size = count( $customer_orders );
+    $minimum_orders = 3
+    if( < $minimum_orders )
+	    return;
+
+    // Get the User’s first name, let’s assume it is Dave
+    $customer = wp_get_current_user();
+
+    $text = sprintf( "Hello, %s. You're looking well today.", $customer->display_name );
+    wc_print_notice( $text, 'notice' );
+
+}
+add_action( 'woocommerce_before_my_account', 'custom_woocommerce_before_my_account' );
+
+?>
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.