Title: Caching code snippets
Last modified: July 8, 2023

---

# Caching code snippets

 *  Resolved [mato093](https://wordpress.org/support/users/mato093/)
 * (@mato093)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/caching-code-snippets/)
 * Hello
 * I have a problem with my code snippet who care about opening time.
 * In certain cases happen that the code not run correctly or not run at all. One
   think is interesting when i am logged in as admin everything works but when i
   logged out it looks like code is stuck or something like this but when i clear
   cache it looks good and code works correctly on user site too.
 * I am think if is possible somehow exclude this code from caching maybe this can
   help.
 * date_default_timezone_set(“Europe/Bratislava”);
   $current_day = date(‘D’);$current_time
   = date(‘H:i:s’, strtotime($current_time . ‘ + 2 hours’));
 * // Set the opening hours for each day
   $opening_hours = array(‘Mon’ => array(‘
   open’ => ’11:00:00′, ‘close’ => ’22:00:00′),‘Tue’ => array(‘open’ => ’11:00:00′,‘
   close’ => ’22:00:00′),‘Wed’ => array(‘open’ => ’11:00:00′, ‘close’ => ’22:00:
   00′),‘Thu’ => array(‘open’ => ’11:00:00′, ‘close’ => ’22:00:00′),‘Fri’ => array(‘
   open’ => ’11:00:00′, ‘close’ => ’16:00:00′),‘Sat’ => array(‘open’ => ’13:00:00′,‘
   close’ => ’22:00:00′),‘Sun’ => array(‘open’ => ’13:00:00′, ‘close’ => ’21:00:
   00′));
 * // Check if restaurant is open
   if (isset($opening_hours[$current_day]) && $current_time
   >= $opening_hours[$current_day][‘open’] && $current_time <= $opening_hours[$current_day][‘
   close’]) { // restaurant is open, so do nothing } else { // restaurant is closed,
   hide add to cart button and display message add_filter( ‘woocommerce_is_purchasable’,‘
   __return_false’); add_action( ‘woocommerce_before_add_to_cart_button’, ‘cwpai_display_closed_message’,
   25 ); } _/ // Function to display closed message /_ function cwpai_display_closed_message(){?
   ><?phpecho ‘
 * Grange je práve zatvorený. Objednajte si cez otváracie hodiny prosím.’;
   }
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcaching-code-snippets%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [Emre Vona](https://wordpress.org/support/users/emrevona/)
 * (@emrevona)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/caching-code-snippets/#post-16880502)
 * when a page is cached, the page is saved as a static html file so PHP does not
   work. you need to handle it using Ajax.
 *  Thread Starter [mato093](https://wordpress.org/support/users/mato093/)
 * (@mato093)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/caching-code-snippets/#post-16880804)
 * Do you think this can handle it ? Or i must got outside from PHP ? 
   I am beginner
   in coding.
 *     ```wp-block-code
       // Get the current day and time
       add_action('woocommerce_before_add_to_cart_button','open_time');
       function open_time(){
   
       	?>
       	<script>
       		// Get current date and time
       		let currentDate = new Date();
   
       		// Get current day and time
       		let currentDay = currentDate.getDay();
       		//let currentTime = currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds();
       		let currentTime = "20:01:00";
       		console.log(currentTime);
   
       		// Set the opening hours for each day
       		let openingHours = {
       			1: { 'open': '11:00:00', 'close': '22:00:00' }, // Monday
       			2: { 'open': '11:00:00', 'close': '22:00:00' }, // Tuesday
       			3: { 'open': '11:00:00', 'close': '22:00:00' }, // Wednesday
       			4: { 'open': '11:00:00', 'close': '22:00:00' }, // Thursday
       			5: { 'open': '11:00:00', 'close': '22:00:00' }, // Friday
       			6: { 'open': '13:00:00', 'close': '22:00:00' }, // Saturday
       			0: { 'open': '13:00:00', 'close': '21:00:00' }  // Sunday
       		};
   
       		// Get the add to cart button and the message container by their classes
       		let addToCartButtons = document.getElementsByClassName("e-atc-qty-button-holder");
       		let goToCartButtons = document.getElementsByClassName("go_to_cart");
       		let messageContainer = document.getElementById("message_container");
   
       		// Check if restaurant is open
       		if (openingHours[currentDay] && currentTime >= openingHours[currentDay]['open'] && currentTime <= openingHours[currentDay]['close']) {
       			// restaurant is open, so do nothing
       			console.log("The restaurant is open.");
       		} else {
       			// restaurant is closed
       			console.log("The restaurant is closed.");
   
       			// Hide the add to cart buttons
       			for(let i = 0; i < addToCartButtons.length; i++) {
       				addToCartButtons[i].style.display = "none";
       			}
   
       			// Hide the go to cart buttons
       			for(let i = 0; i < goToCartButtons.length; i++) {
       				goToCartButtons[i].style.display = "none";
       			}
   
       			// Create a new paragraph element with the closed message
       			let closedMessage = document.createElement("p");
       			closedMessage.className = "opening_hours_message"; // add class for styling
       			closedMessage.textContent = "Grange je práve zatvorený. Objednajte si cez otváracie hodiny prosím.";
   
       			// Append the message to the body of the document
       			document.getElementById("OpenMessage").appendChild(closedMessage); 
       		}
   
       	</script>
       	<div id="OpenMessage" style="text-align:center"></div>
       	<?php
   
       }
       ```
   
 *  Plugin Author [Emre Vona](https://wordpress.org/support/users/emrevona/)
 * (@emrevona)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/caching-code-snippets/#post-16880933)
 * You need to make Ajax request. And the Ajax request executes a PHP code. You 
   cannot execute a PHP code directly because pages are saved as a static html file.
   
   If you read something about Ajax, you will understand what I said.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Caching code snippets’ is closed to new replies.

 * ![](https://ps.w.org/wp-fastest-cache/assets/icon-256x256.png?rev=2064586)
 * [WP Fastest Cache - WordPress Cache Plugin](https://wordpress.org/plugins/wp-fastest-cache/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-fastest-cache/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-fastest-cache/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-fastest-cache/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-fastest-cache/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-fastest-cache/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Emre Vona](https://wordpress.org/support/users/emrevona/)
 * Last activity: [2 years, 11 months ago](https://wordpress.org/support/topic/caching-code-snippets/#post-16880933)
 * Status: resolved