Skip to content

Instantly share code, notes, and snippets.

@msulaimanmisri
Created July 4, 2022 07:41
Show Gist options
  • Save msulaimanmisri/35625cc330ef2cff0b82bea29b133b45 to your computer and use it in GitHub Desktop.
Save msulaimanmisri/35625cc330ef2cff0b82bea29b133b45 to your computer and use it in GitHub Desktop.
WordPress WooCommerce Random Product
<?php
/**
* @author Muhamad Sulaiman
* @link https://github.com/msulaimanmisri
*/
function sm_wc_random_products( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters('woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'random_list' == $orderby_value ) {
$args['orderby'] = 'rand';
$args['order'] = '';
$args['meta_key'] = '';
}
return $args;
}
function sm_wc_random_product_catalog( $sortby ) {
$sortby['random_list'] = 'Random';
return $sortby;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'sm_wc_random_products' );
add_filter( 'woocommerce_default_catalog_orderby_options', 'sm_wc_random_product_catalog' );
add_filter( 'woocommerce_catalog_orderby', 'sm_wc_random_product_catalog' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment