Advertisement
daymobrew

Inject item into product category

May 27th, 2018
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.28 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Inject into Product archive
  4. Plugin URI: https://www.facebook.com/groups/advanced.woocommerce/permalink/2119468894734185/
  5. Description: Inject a div after every fourth product in product archive.
  6. Author: Damien Carbery
  7. Author URI: https://www.damiencarbery.com
  8. Version: 0.1
  9. */
  10.  
  11. add_action( 'wp_head', 'dcwd_override_columns_setting' );
  12. function dcwd_override_columns_setting() {
  13.     wc_set_loop_prop( 'columns', 300 );
  14. }
  15.  
  16.  
  17. static $i = 0;
  18. global $i;
  19. static $inject_counter = 1;
  20. global $inject_counter;
  21.  
  22.  
  23. // This is where you write the code to put information into the new space.
  24. // The counter tells you which space it is.
  25. function inject_item( $counter ) {
  26.     switch ( $counter ) {
  27.         case 1:
  28. ?>
  29. <div class="informationfirst">
  30.     <h2 class="information"> INFORMATION </h2>
  31.         <p class="text"> Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  32.         Aliquam elementum iaculis velit, vel maximus ante finibus vitae.
  33.         Nam venenatis ultrices tempus. Morbi ornare nisl ac vulputate elementum.</p>
  34.             <a class="button" href="http://google.com"> Click! </a>
  35. </div>
  36. <?php
  37.             break;
  38.         case 2:
  39. ?>
  40. <div class="informationsecond">
  41.     <h3 class="information"> INFORMATION SECOND GO GO</h2>
  42.         <p class="text"> Elementum iaculis velit, vel maximus ante finibus vitae.
  43.         Nam venenatis ultrices tempus. Morbi ornare nisl ac vulputate elementum.</p>
  44.             <a class="button" href="http://google.com"> Click! </a>
  45. </div>
  46. <?php
  47.             break;
  48.         case 3:
  49. ?>
  50. <div class="informationTHIRD">
  51.     <h4 class="information"> INFORMATION THIRD</h2>
  52.         <p class="text"> Morbi ornare nisl ac vulputate elementum.</p>
  53.             <a class="button" href="http://google.com"> Click! </a>
  54. </div>
  55. <?php
  56.             break;
  57.     }
  58. }
  59.  
  60.  
  61. // Add a new item to the list after every fourth product.
  62. add_action( 'woocommerce_shop_loop', 'inject_into_product_archive' );
  63. function inject_into_product_archive() {
  64.     global $i, $inject_counter;
  65.    
  66.    
  67.     // This additional counter variable is a hack. The first space was being added after the
  68.     // 4th product but when it was after every 3rd product.
  69.     $current_position = $i - $inject_counter;
  70.     if ( $current_position < 0 ) $current_position = 0;
  71.     //error_log( "Inject: $i - $inject_counter = $current_position" );
  72.     if ( ( $current_position > 0 ) && ( $current_position % 4 == 3 ) ) {
  73.         $i++;
  74.         $classes = array();
  75.         $classes[] = 'product';
  76.         $classes = dcwd_add_post_class_in_archive( $classes, '', '' );
  77.         echo '<li class="'. join( ' ', $classes ).'">';
  78.         inject_item( $inject_counter );
  79.         echo '</li>';
  80.         $inject_counter++;
  81.         //error_log( "Inject div: $i" );
  82.         //$i++;
  83.     }
  84.     $i++;
  85. }
  86.  
  87.  
  88. // Manually add the 'first' and 'last' classes to the li elements to force them onto the different rows.
  89. add_filter( 'post_class', 'dcwd_add_post_class_in_archive', 30, 3 );
  90. function dcwd_add_post_class_in_archive( $classes, $class, $post_id ) {
  91.     if ( ! is_product_category() ) {
  92.         return $classes;
  93.     }
  94.     global $i;
  95.    
  96.     //error_log( "Add first/last post class: $i (post $post_id)" );
  97.     if ( $i % 3 == 0 ) {
  98.         $classes[] = 'last';
  99.         //error_log( "Add LAST post class: $i (post $post_id)" );
  100.     }
  101.     else if ( $i % 3 == 1 ) {
  102.         $classes[] = 'first';
  103.         //error_log( "Add FIRST post class: $i (post $post_id)" );
  104.     }
  105.  
  106.     return $classes;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement