V
V
Vladislav Kalnaus2018-07-28 07:03:41
WordPress
Vladislav Kalnaus, 2018-07-28 07:03:41

How to make custom Woocommerce product sorting?

Tell me how to implement such sorting instead of the standard ones?
5b5beb1196d02076898930.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2018-07-28
@azerphoenix

Hello!
1) number of displayed products:

/** Выводим dropdown кол-во товаров **/
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );

function woocommerce_catalog_page_ordering() {
?>

<form action="" method="POST" name="results" class="woocommerce-ordering">
<select name="woocommerce-sort-by-columns" id="woocommerce-sort-by-columns" class="sortby" onchange="this.form.submit()">
<?php

//Get products on page reload
if  (isset($_POST['woocommerce-sort-by-columns']) && (($_COOKIE['shop_pageResults'] <> $_POST['woocommerce-sort-by-columns']))) {
        $numberOfProductsPerPage = $_POST['woocommerce-sort-by-columns'];
          } else {
        $numberOfProductsPerPage = $_COOKIE['shop_pageResults'];
          }
            $shopCatalog_orderby = apply_filters('woocommerce_sortby_page', array(
                '10'        => __('10', 'woocommerce'),
                '20'        => __('20', 'woocommerce'),
                '40'        => __('40', 'woocommerce'),
                '-1'        => __('Все', 'woocommerce'),
            ));

        foreach ( $shopCatalog_orderby as $sort_id => $sort_name )
            echo '<option value="' . $sort_id . '" ' . selected( $numberOfProductsPerPage, $sort_id, true ) . ' >' . $sort_name . '</option>';
?>
</select>
</form>

<?php echo ' </span>' ?>
<?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, '/', 'example.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');

don't forget to change example.com to your domain.
And for sorting (by title, etc.) call this in the template:
<?php woocommerce_catalog_ordering() ?>
Example - test5.md7.info/shop

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question