• Resolved mato093

    (@mato093)


    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() { ?>

    <?php
    echo ‘

    Grange je práve zatvoreny. Objednajte si cez otváracie hodiny prosím.’;
    }

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Emre Vona

    (@emrevona)

    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

    (@mato093)

    Do you think this can handle it ? Or i must got outside from PHP ?
    I am beginner in coding.

    // 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 zatvoreny. 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

    (@emrevona)

    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.