Skip to content

Instantly share code, notes, and snippets.

@bekarice
Last active April 8, 2024 17:56
Show Gist options
  • Select an option

  • Save bekarice/1883b7e678ec89cc8f4d to your computer and use it in GitHub Desktop.

Select an option

Save bekarice/1883b7e678ec89cc8f4d to your computer and use it in GitHub Desktop.

Revisions

  1. bekarice revised this gist May 30, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wc-sku-sorting.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    <?php
    <?php // ONLY COPY THIS LINE IF NEEDED!

    /**
    * Adds the ability to sort products in the shop based on the SKU
  2. bekarice revised this gist May 30, 2019. 1 changed file with 15 additions and 4 deletions.
    19 changes: 15 additions & 4 deletions wc-sku-sorting.php
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,19 @@
    <?php

    /**
    * Adds the ability to sort products in the shop based on the SKU
    * Can be combined with tips here to display the SKU on the shop page: https://www.skyverge.com/blog/add-information-to-woocommerce-shop-page/
    *
    * @param array $args the sorting args
    * @return array updated args
    */

    function sv_add_sku_sorting( $args ) {

    $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

    if ( 'sku' == $orderby_value ) {
    $args['orderby'] = 'meta_value';
    $args['order'] = 'asc';
    // ^ lists SKUs alphabetically 0-9, a-z; change to desc for reverse alphabetical
    $args['order'] = 'asc'; // lists SKUs alphabetically 0-9, a-z; change to desc for reverse alphabetical
    $args['meta_key'] = '_sku';
    }

    @@ -19,9 +22,17 @@ function sv_add_sku_sorting( $args ) {
    add_filter( 'woocommerce_get_catalog_ordering_args', 'sv_add_sku_sorting' );


    /**
    * Add the option to the orderby dropdown.
    *
    * @param array $sortby the sortby options
    * @return array updated sortby
    */
    function sv_sku_sorting_orderby( $sortby ) {
    $sortby['sku'] = 'Sort by SKU';

    // Change text above as desired; this shows in the sorting dropdown
    $sortby['sku'] = __( 'Sort by SKU', 'textdomain' );

    return $sortby;
    }
    add_filter( 'woocommerce_catalog_orderby', 'sv_sku_sorting_orderby' );
  3. bekarice revised this gist Sep 27, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion wc-sku-sorting.php
    Original file line number Diff line number Diff line change
    @@ -24,4 +24,5 @@ function sv_sku_sorting_orderby( $sortby ) {
    // Change text above as desired; this shows in the sorting dropdown
    return $sortby;
    }
    add_filter( 'woocommerce_catalog_orderby', 'sv_sku_sorting_orderby' );
    add_filter( 'woocommerce_catalog_orderby', 'sv_sku_sorting_orderby' );
    add_filter( 'woocommerce_default_catalog_orderby_options', 'sv_sku_sorting_orderby' );
  4. bekarice created this gist Mar 12, 2015.
    27 changes: 27 additions & 0 deletions wc-sku-sorting.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    /**
    * Adds the ability to sort products in the shop based on the SKU
    * Can be combined with tips here to display the SKU on the shop page: https://www.skyverge.com/blog/add-information-to-woocommerce-shop-page/
    */

    function sv_add_sku_sorting( $args ) {

    $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

    if ( 'sku' == $orderby_value ) {
    $args['orderby'] = 'meta_value';
    $args['order'] = 'asc';
    // ^ lists SKUs alphabetically 0-9, a-z; change to desc for reverse alphabetical
    $args['meta_key'] = '_sku';
    }

    return $args;
    }
    add_filter( 'woocommerce_get_catalog_ordering_args', 'sv_add_sku_sorting' );


    function sv_sku_sorting_orderby( $sortby ) {
    $sortby['sku'] = 'Sort by SKU';
    // Change text above as desired; this shows in the sorting dropdown
    return $sortby;
    }
    add_filter( 'woocommerce_catalog_orderby', 'sv_sku_sorting_orderby' );