2

How to disable product when the the timer runs out below is my phtml code is it possible please let me know

here my code am calling in phtml file

 <?php
    $myBlock = \Magento\Framework\App\ObjectManager::getInstance()->get('Mageplaza\HelloWorld\Block\AttributeCollection');
     
    // print current product data
        $currentProduct = $myBlock->getCurrentProduct();
        echo $currentProduct->getStartTime() . '<br />';
        echo $currentProduct->getEndTime() . '<br />';  
        ?>
    
    
     <!DOCTYPE HTML>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    p {
      text-align: center;
      font-size: 60px;
      margin-top: 0px;
    }
    </style>
    </head>
    <body>
    
    <p id="demo"></p>
    
    <script>
    // Set the date we're counting down to
    var countDownDate = new Date("<?php echo $currentProduct->getEndTime(); ?>").getTime();
    
    // Update the count down every 1 second
    var x = setInterval(function() {
    
      // Get today's date and time
      var now = new Date("<?php echo $currentProduct->getStartTime(); ?>").getTime();
        
      // Find the distance between now and the count down date
      var distance = countDownDate - now;
        
      // Time calculations for days, hours, minutes and seconds
      var days = Math.floor(distance / (1000 * 60 * 60 * 24));
      var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
      var seconds = Math.floor((distance % (1000 * 60)) / 1000);
        
      // Output the result in an element with id="demo"
      document.getElementById("demo").innerHTML = days + "d " + hours + "h "
      + minutes + "m " + seconds + "s ";
        
      // If the count down is over, write some text 
      if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo").innerHTML = "EXPIRED";
      }
    }, 1000);
    </script>
    
    </body>
    </html>

2 Answers 2

1

Replace blow section in your code :-

// If the count down is over, write some text 
      if (distance < 0) {
        clearInterval(x);
        jQuery("#demo").hide();
        jQuery("#demo").after( "<p>EXPIRED</p>" );
      }
5
  • what exactly will thsi code does chirag?\
    – Pramod
    Aug 21, 2020 at 10:03
  • You need to find above section in your code and replace with my code. it will hide "<p id="demo"></p>" and add new p tag with your desired text. I think this is you want to achieve in your task. Aug 21, 2020 at 10:05
  • no i want to hide the current product when timer runs out
    – Pramod
    Aug 21, 2020 at 10:06
  • and ur trying to hide <p> tag here
    – Pramod
    Aug 21, 2020 at 10:07
  • then replace "#demo" with your your div id in above code. You need to inspect the section you want to hide and replace in place of "#demo" in above code. Aug 21, 2020 at 10:08
0

You need to set current running time in var now. Replace this below code :

Replace this line : var now = new Date().getTime();

<?php
    $myBlock = \Magento\Framework\App\ObjectManager::getInstance()->get('Mageplaza\HelloWorld\Block\AttributeCollection');
     
    // print current product data
        $currentProduct = $myBlock->getCurrentProduct();
        echo $currentProduct->getStartTime() . '<br />';
        echo $currentProduct->getEndTime() . '<br />';  
        ?>
    
    
     <!DOCTYPE HTML>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    p {
      text-align: center;
      font-size: 60px;
      margin-top: 0px;
    }
    </style>
    </head>
    <body>
    
    <p id="demo"></p>
    
    <script>
    // Set the date we're counting down to
    var countDownDate = new Date("<?php echo $currentProduct->getEndTime(); ?>").getTime();
    
    // Update the count down every 1 second
    var x = setInterval(function() {
    
      // Get today's date and time
      var now = new Date().getTime();
        
      // Find the distance between now and the count down date
      var distance = countDownDate - now;
        
      // Time calculations for days, hours, minutes and seconds
      var days = Math.floor(distance / (1000 * 60 * 60 * 24));
      var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
      var seconds = Math.floor((distance % (1000 * 60)) / 1000);
        
      // Output the result in an element with id="demo"
      document.getElementById("demo").innerHTML = days + "d " + hours + "h "
      + minutes + "m " + seconds + "s ";
        
      // If the count down is over, write some text 
      if (distance < 0) {
        clearInterval(x);
        jQuery('#product-addtocart-button').attr('disabled', 'disabled'); 
        document.getElementById("demo").innerHTML = "EXPIRED";
      }
    }, 1000);
    </script>
    
    </body>
    </html>
5
  • rohan check the updated question
    – Pramod
    Aug 21, 2020 at 9:29
  • i want to disable product when timer runs out
    – Pramod
    Aug 21, 2020 at 9:29
  • It's quite simple. You can add this line before Expire code line : jQuery('#product-addtocart-button').attr('disabled', 'disabled'); Aug 21, 2020 at 9:40
  • Check my updated answer. Aug 21, 2020 at 9:40
  • this will disable add to cart but i want to hide product
    – Pramod
    Aug 21, 2020 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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