Last active
November 18, 2023 14:34
-
-
Save hamidrezayazdani/692e4ed155c69651bd39a4b1a75661a3 to your computer and use it in GitHub Desktop.
Change WooCommerce order by condition on the edit product screen.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Change views on the edit product screen. | |
* | |
* @param array $views Array of views. | |
* @return array | |
*/ | |
function ywp_product_views( $views ) { | |
global $wp_query; | |
// Add sorting link. | |
if ( current_user_can( 'edit_others_products' ) ) { | |
$class = ( isset( $wp_query->query['orderby'] ) && 'id' === $wp_query->query['orderby'] ) ? 'current' : ''; | |
$query_string = remove_query_arg( array( 'orderby', 'order' ) ); | |
$query_string = add_query_arg( 'orderby', rawurlencode( 'id' ), $query_string ); | |
$query_string = add_query_arg( 'order', rawurlencode( 'ASC' ), $query_string ); | |
$views['byorder'] = '<a href="' . esc_url( $query_string ) . '" class="' . esc_attr( $class ) . '">' . __( 'Sorting', 'woocommerce' ) . '</a>'; | |
} | |
return $views; | |
} | |
add_filter( 'views_edit-product', 'ywp_product_views', 9999 ); | |
// The code goes to your active theme/child theme functions.php file | |
// NOTE: ASC = Ascending, DESC = descending |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment