Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Inject into Product archive
- Plugin URI: https://www.facebook.com/groups/advanced.woocommerce/permalink/2119468894734185/
- Description: Inject a div after every fourth product in product archive.
- Author: Damien Carbery
- Author URI: https://www.damiencarbery.com
- Version: 0.1
- */
- add_action( 'wp_head', 'dcwd_override_columns_setting' );
- function dcwd_override_columns_setting() {
- wc_set_loop_prop( 'columns', 300 );
- }
- static $i = 0;
- global $i;
- static $inject_counter = 1;
- global $inject_counter;
- // This is where you write the code to put information into the new space.
- // The counter tells you which space it is.
- function inject_item( $counter ) {
- switch ( $counter ) {
- case 1:
- ?>
- <div class="informationfirst">
- <h2 class="information"> INFORMATION </h2>
- <p class="text"> Lorem ipsum dolor sit amet, consectetur adipiscing elit.
- Aliquam elementum iaculis velit, vel maximus ante finibus vitae.
- Nam venenatis ultrices tempus. Morbi ornare nisl ac vulputate elementum.</p>
- <a class="button" href="http://google.com"> Click! </a>
- </div>
- <?php
- break;
- case 2:
- ?>
- <div class="informationsecond">
- <h3 class="information"> INFORMATION SECOND GO GO</h2>
- <p class="text"> Elementum iaculis velit, vel maximus ante finibus vitae.
- Nam venenatis ultrices tempus. Morbi ornare nisl ac vulputate elementum.</p>
- <a class="button" href="http://google.com"> Click! </a>
- </div>
- <?php
- break;
- case 3:
- ?>
- <div class="informationTHIRD">
- <h4 class="information"> INFORMATION THIRD</h2>
- <p class="text"> Morbi ornare nisl ac vulputate elementum.</p>
- <a class="button" href="http://google.com"> Click! </a>
- </div>
- <?php
- break;
- }
- }
- // Add a new item to the list after every fourth product.
- add_action( 'woocommerce_shop_loop', 'inject_into_product_archive' );
- function inject_into_product_archive() {
- global $i, $inject_counter;
- // This additional counter variable is a hack. The first space was being added after the
- // 4th product but when it was after every 3rd product.
- $current_position = $i - $inject_counter;
- if ( $current_position < 0 ) $current_position = 0;
- //error_log( "Inject: $i - $inject_counter = $current_position" );
- if ( ( $current_position > 0 ) && ( $current_position % 4 == 3 ) ) {
- $i++;
- $classes = array();
- $classes[] = 'product';
- $classes = dcwd_add_post_class_in_archive( $classes, '', '' );
- echo '<li class="'. join( ' ', $classes ).'">';
- inject_item( $inject_counter );
- echo '</li>';
- $inject_counter++;
- //error_log( "Inject div: $i" );
- //$i++;
- }
- $i++;
- }
- // Manually add the 'first' and 'last' classes to the li elements to force them onto the different rows.
- add_filter( 'post_class', 'dcwd_add_post_class_in_archive', 30, 3 );
- function dcwd_add_post_class_in_archive( $classes, $class, $post_id ) {
- if ( ! is_product_category() ) {
- return $classes;
- }
- global $i;
- //error_log( "Add first/last post class: $i (post $post_id)" );
- if ( $i % 3 == 0 ) {
- $classes[] = 'last';
- //error_log( "Add LAST post class: $i (post $post_id)" );
- }
- else if ( $i % 3 == 1 ) {
- $classes[] = 'first';
- //error_log( "Add FIRST post class: $i (post $post_id)" );
- }
- return $classes;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement