Okay
  Public Ticket #160636
Woocommerce Functions Conflicting with Theme
Closed

Comments

  • Lew started the conversation
    Hello Nectar, I doubt this is anything you guys are itching to deal with, but any insight would be much appreciated: I'm trying to use a function to add a "items per page" dropdown. ---> looks like this---> // Lets create the function to house our form function woocommerce_catalog_page_ordering() { ?>
  •  1,069
    ThemeNectar replied

    Hey Lew, it appears your response got cut off because you pasted code directly into the editor things broke as a result. Please paste your code into

    a code snippet box

    next time so I can read :)

  • Lew replied
    // Lets create the function to house our form
    function woocommerce_catalog_page_ordering() {
    ?>
    <form action="" method="POST" name="results">
    <select name="woocommerce-sort-by-columns" id="woocommerce-sort-by-columns" class="sortby" onchange="this.form.submit()">
    <?php
    
    		//  This is where you can change the amounts per page that the user will use  feel free to change the numbers and text as you want, in my case we had 4 products per row so I chose to have multiples of four for the user to select.
    			$shopCatalog_orderby = apply_filters('woocommerce_sortby_page', array(
    			    ''       => __('Results per page', 'woocommerce'),
    				'24' 	=> __('24 per page', 'woocommerce'),
    				'36' 		=> __('36 per page', 'woocommerce'),
    				'48' 		=> __('48 per page', 'woocommerce'),
    				'64' 		=> __('64 per page', 'woocommerce'),
    			));
    
    			foreach ( $shopCatalog_orderby as $sort_id => $sort_name )
    				echo '<option value="' . $sort_id . '" ' . selected( $_SESSION['sortby'], $sort_id, false ) . ' >' . $sort_name . '</option>';
    		?>
    </select>
    
    </form>
    <?php
    
    } 
    
    // now we set our cookie if we need to
    function dl_sort_by_page($count) {
      if (isset($_COOKIE['shop_pageResults'])) { // if normal page load with cookie
         $count = $_COOKIE['shop_pageResults'];
      }
      if (isset($_POST['woocommerce-sort-by-columns'])) { //if form submitted
        setcookie('shop_pageResults', $_POST['woocommerce-sort-by-columns'], time()+1209600, '/', 'your-web-address.com', false); //this will fail if any part of page has been output- hope this works!
        $count = $_POST['woocommerce-sort-by-columns'];
      }
      // else normal page load and no cookie
      return $count;
    }
    
    add_filter('loop_shop_per_page','dl_sort_by_page');
    add_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_page_ordering', 20 );

    Sorry about that. Above is the code. It seems that there is some .js or cookie conflict between this function and salient, as it works on the tweenty13 theme in wordpress. If you guys could point me in a hint of the right direction that would be huge, as I've seen a lot of desire for this kind of functionality inside the woocommerce plugin, which they have yet to proprietarily address.

    Best,

    Lew

  •  1,069
    ThemeNectar replied

    Hey again!

    I believe it's not working because Salient already is already setting a filter for loop_shop_per_page. Just delete this out of the functions.php

    //chnge how many products are displayed per page	
    add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 12;' ), 20 );

    Cheers :)