Display course batches on single course page when course layout set as other than blank

Add this code in wplms-customizer.php to show course batches on single course page below course description.
https://prntscr.com/11drafa

add_action('wplms_after_course_description',function(){  $id = get_the_ID();  $batches = wplms_get_course_batches($id);    if(empty($batches))
      return;    $init=Wplms_Batches_Init::init();    $init->show_enroll_button = 1;
      ?>
      <div class="wplms_batches_wrapper block">
        <ul>
        <?php
          foreach($batches as $batch_id){
            $init->batch_grid_style('custom',$batch_id);
          }
        ?>
        </ul>
      </div><style>.wplms_batches_wrapper ul{display:grid;grid-template-columns:repeat(auto-fit,minmax(240px,1fr));grid-gap:1rem}.wplms_batches_wrapper .batch_snapshot{display:flex;flex-direction:row;border:1px solid var(--border);padding:.5rem;position:relative;flex-wrap:wrap;align-items: flex-start}.batch_snapshot .labels{position:absolute;top:.5rem;left:.5rem;z-index:9;background:var(--primary);color:var(--primarycolor);padding:2px 5px;border-radius:2px;display:flex;flex-wrap:wrap}.batch_snapshot .labels>span{text-transform:uppercase;font-size:11px;letter-spacing:1px}.batch_snapshot .batch_image img{border-radius:3px}.batch_image{position:relative;margin:.5rem;flex:1 0 120px}.batch_admins{position:absolute;bottom:.5rem;right:.5rem}.batch_admins img{width:32px;height:32px;border-radius:50%}.batch_content{margin:.5rem;flex:3 0 120px}</style>
      <?php    ?><script>
      if(typeof localforage != 'undefined'){
        localforage.getItem('bp_login_token').then(function(token){          function sendEnrollBatchRequest(batch_id){            var xh = new XMLHttpRequest();
            xh.open('POST', ajaxurl);
            xh.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xh.onload = function() {
              if (xh.status === 200) {
                              let data = JSON.parse(xh.responseText);
                              if(typeof data == 'object' && data.status){
                                document.querySelectorAll('.enrol_in_batch').forEach(function(el){
                                  el.parentNode.removeChild(el);
                                });
                                var span = document.createElement('span');
                  span.innerHTML = "<?php _e('Enrolled','wplms-batches');?>";
                            document.querySelector('#batch'+batch_id+' .labels').appendChild(span);
                              }
              }else if (xhr.status !== 200) {
                            console.log('Something went wrong.Request failed.' + xhr.status);
                          }
            }
            xh.send(encodeURI('action=enrol_in_batch&batch_id='+batch_id+'&course_id=<?php echo $id; ?>+&token=' + token));
          }          if(token){
            var xhr = new XMLHttpRequest();
            xhr.open('POST', ajaxurl);
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.onload = function() {
                          if (xhr.status === 200) {
                              let data = JSON.parse(xhr.responseText);
                              if(typeof data == 'object' && data.status){
                                if(data.enrolled && data.enrolled.length){
                                  data.enrolled.map(function(batch_id){
                                    var span = document.createElement('span');
                      span.innerHTML = "<?php _e('Enrolled','wplms-batches');?>";
                                    document.querySelector('#batch'+batch_id+' .labels').appendChild(span);
                                  });
                                }else{                                document.querySelectorAll('.batch_content').forEach(function(el){
                                  var btn = document.createElement('a');
                      btn.innerHTML = "<?php _e('Enrol Now','wplms-batches');?>";
                      btn.setAttribute('class','enrol_in_batch button is-primary small');
                      if(data.hasOwnProperty('cart_links') && Object.keys(data.cart_links).length){
                        if(data.cart_links.hasOwnProperty(el.getAttribute('data-id')) && data.cart_links[el.getAttribute('data-id')].length){
                          btn.setAttribute('href',data.cart_links[el.getAttribute('data-id')]);
                        }                      }else{
                        btn.addEventListener('click',function(){
                          btn.innerHTML='...';
                          document.querySelectorAll('.enrol_in_batch').forEach(function(el){
                                          if(btn != el){
                                            el.style.display='none';
                                          }
                                        });                          sendEnrollBatchRequest(el.getAttribute('data-id'));
                        });
                      }                      if(el.querySelector('.enroll_now_button')){
                        el.querySelector('.enroll_now_button').remove();
                      }                                  el.appendChild(btn);
                                });                                }
                              }                          }
                          else if (xhr.status !== 200) {
                              console.log('Something went wrong.Request failed.' + xhr.status);                          }
                      };                      xhr.send(encodeURI('action=get_enrolled_batches&course_id=<?php echo $id; ?>&batches='+JSON.stringify(<?php echo json_encode($batches); ?>)+'&token=' + token) );
                    }        });
      }
    </script><?php
});

 

 

Was this article helpful?

Related Articles

Leave A Comment?