Skip to content

Instantly share code, notes, and snippets.

@mehrshaddarzi
Forked from florentsorel/functions.php
Created September 10, 2018 06:50
Show Gist options
  • Save mehrshaddarzi/624deb379c68510c85af082f5a5f1bec to your computer and use it in GitHub Desktop.
Save mehrshaddarzi/624deb379c68510c85af082f5a5f1bec to your computer and use it in GitHub Desktop.
WooCommerce : overirde loop/orderby.php template by button group with Bootstrap
<?php
/*
File : functions.php (in your theme)
Override "woocommerce_before_shop_loop" hook.
Add a check : (Line 41 - 43)
Avoid error if administrator choosing "rating" in administration and rating are disabled.
IF "woocommerce_enable_review_rating" option equal "no"
AND
"woocommerce_default_catalog_orderby" option equal "rating"
THEN
update "woocommerce_default_catalog_orderby" option to "date"
*/
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
function get_current_url() {
global $wp;
$old = 'http://' . $_SERVER['HTTP_HOST'] . strtok($_SERVER['REQUEST_URI'], '?');
$current_url = add_query_arg( '', '', home_url( $wp->request ) );
return $current_url;
}
function woocommerce_catalog_ordering() {
global $wp_query;
if ( 1 == $wp_query->found_posts || ! woocommerce_products_will_display() ) {
return;
}
$orderby = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
$show_default_orderby = 'menu_order' === apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
$catalog_orderby_options = apply_filters( 'woocommerce_catalog_orderby', array(
'menu_order' => __( 'Default sorting', 'woocommerce' ),
'popularity' => __( 'Sort by popularity', 'woocommerce' ),
'rating' => __( 'Sort by average rating', 'woocommerce' ),
'date' => __( 'Sort by newness', 'woocommerce' ),
'price' => __( 'Sort by price: low to high', 'woocommerce' ),
'price-desc' => __( 'Sort by price: high to low', 'woocommerce' )
) );
if ( ! $show_default_orderby ) {
unset( $catalog_orderby_options['menu_order'] );
}
if ( get_option( 'woocommerce_enable_review_rating' ) === 'no' ) {
unset( $catalog_orderby_options['rating'] );
}
if( get_option('woocommerce_enable_review_rating') == 'no' && get_option('woocommerce_default_catalog_orderby') == 'rating') {
update_option('woocommerce_default_catalog_orderby', 'date');
}
wc_get_template( 'loop/orderby.php', array( 'catalog_orderby_options' => $catalog_orderby_options, 'orderby' => $orderby, 'show_default_orderby' => $show_default_orderby ) );
}
<?php
/*
File : loop/orderby.php
Replace the default form by "btn-group"
*/
?>
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><?php echo (isset($_GET['orderby'])) ? $catalog_orderby_options[$orderby] : $catalog_orderby_options[get_option( 'woocommerce_default_catalog_orderby' )]; ?> <span class="caret"></span></button>
<ul class="dropdown-menu">
<?php foreach ( $catalog_orderby_options as $id => $name ) : ?>
<li<?php echo (isset($_GET['orderby']) && $_GET['orderby'] == $id ) ? ' class="active"' : ($id == get_option( 'woocommerce_default_catalog_orderby') && !isset($_GET['orderby'])) ? ' class="active"' : ''; ?>>
<a href="<?php echo get_current_url() . '/?orderby=' . $id ; ?>"><?php echo esc_html( $name ); ?></a>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php wc_get_template( 'loop/result-count.php' ); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment