pagination - Woocommerce products per page dropdown

I'm using the following code taken from here-?showmodaldialog=1#comment-1554to display a drop down selector so the

I'm using the following code taken from here-

/?showmodaldialog=1#comment-1554

to display a drop down selector so the user can select how many products to view per page. It all works except the pagination. Selecting next page returns a page not found 404 error. I think it something to do with the cookies. Can anyone help? Thanks

// 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'),
                '2'    => __('2 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, '/', 'beadsnwire.lukeseall.co.uk/', 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 );

I'm using the following code taken from here-

http://designloud/how-to-add-products-per-page-dropdown-to-woocommerce/?showmodaldialog=1#comment-1554

to display a drop down selector so the user can select how many products to view per page. It all works except the pagination. Selecting next page returns a page not found 404 error. I think it something to do with the cookies. Can anyone help? Thanks

// 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'),
                '2'    => __('2 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, '/', 'beadsnwire.lukeseall.co.uk/', 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 );
Share Improve this question asked Apr 22, 2013 at 10:34 Luke SeallLuke Seall 3303 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

This does not have anything to do with cookies, this is what happends: 1. Visitor goes to page 4/6 wich shows products 40-50 (10 per page) 2. When visitor changes products per page from 10 to 30, he will still be at page 4, when there are only 2 pages (60 products / 30 = 2 pages)

This creates the 404.

To resolve this you can also add this above the form:

global $wp_query;
$cat = $wp_query->get_queried_object();
$action = ""; // default to current page
if( isset( $cat->term_id ) ) 
    $action = get_term_link( (int)$cat->term_id, "product_cat" );

You should add this just BEFORE the form, and set the action

<form action="<?php echo $action; ?>" method="POST" name="results">

(Hope its flawless, did just type it here)

Hope this helps!

Edit: I did make a plugin to add the 'Product per page' dropdown, this is not yet resolved in it, but I will put it on my ToDo list. (http://wordpress/plugins/woocommerce-products-per-page/)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745228413a4617569.html

相关推荐

  • pagination - Woocommerce products per page dropdown

    I'm using the following code taken from here-?showmodaldialog=1#comment-1554to display a drop down selector so the

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信