• Resolved jetcastillo

    (@jetcastillo)


    hi, im developer by a client and he wants to develop by stock products
    hes using this plugings:
    *woocommerce
    *WOOF – WooCommerce Products Filter
    and a bunch more
    im using this piece of code to try to implement this but it dosent return me anything

    add_filter( 'woocommerce_catalog_orderby', 'misha_add_custom_sorting_options' );
     
    function misha_add_custom_sorting_options( $options ){
     
        // $options['title'] = 'Sort alphabetically';
        $options['availability'] = 'Ordenar por Cantidad';
     
        return $options;
     
    }
    
    add_filter( 'woocommerce_get_catalog_ordering_args', 'misha_custom_product_sorting' );
     
    function misha_custom_product_sorting( $args ) {
     
        // Sort alphabetically
        if ( isset( $_GET['orderby'] ) && 'title' === $_GET['orderby'] ) {
            $args['orderby'] = 'title';
            $args['order'] = 'asc';
        }
     
        // Show products in stock first
        if( isset( $_GET['orderby'] ) && 'availability' === $_GET['orderby'] ) {
            $args['meta_key'] = '_stock';
            $args['orderby'] = array( 'meta_value' => 'DESC' );
    
        }
     
        return $args;
     
    }

    also this one

    add_filter( 'woocommerce_catalog_orderby', 'misha_add_custom_sorting_options' );
     
    function misha_add_custom_sorting_options( $options ){
     
        // $options['title'] = 'Sort alphabetically';
        $options['availability'] = 'Ordenar por Cantidad';
     
        return $options;
     
    }
    
    add_filter( 'woocommerce_get_catalog_ordering_args', 'misha_custom_product_sorting' );
     
    function misha_custom_product_sorting( $args ) {
     
        // Sort alphabetically
        if ( isset( $_GET['orderby'] ) && 'title' === $_GET['orderby'] ) {
            $args['orderby'] = 'title';
            $args['order'] = 'asc';
        }
     
        // Show products in stock first
        if( isset( $_GET['orderby'] ) && 'availability' === $_GET['orderby'] ) {
            echo "entre a orden";
            $args['orderby'] = 'meta_value';
            $args['order'] = 'DESC';
            $args['meta_key'] = '_stock';
    
        }
     
        return $args;
     
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi there ??

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers.

    If you look at the $options array in the woocommerce_catalog_orderby filter, you will see the default options are the following:
    Array ( [menu_order] => Default sorting [popularity] => Sort by popularity [rating] => Sort by average rating [date] => Sort by latest [price] => Sort by price: low to high [price-desc] => Sort by price: high to low )

    The code you have adds a new option called “availability” where the default sorting dropdown will show “Ordenar por Cantidad” as an option. Do you even see that as an option in the dropdown? If not, another plugin like WOOF may be overriding it.

    I copied your first example code and it works fine on a default install with no other plugins other than WooCommerce and sorts by _stock in descending order.

    I am not too familiar with WOOF other than that I’ve removed and replaced it on multiple sites with better plugins like FacetWP or custom ElasticSearch integrations. From what I recall, it adds its own param to the URL like ?swoof and most likely also overriding the filters you are trying to use. Your code should add a ?orderby=availability param to the URL.

    If you do see your custom option in the dropdown, then check the URL to see if the orderby=availability param is showing after selecting it. If it’s not, then WOOF or another plugin is probably filtering it again and removing it. Those plugins may have their own filters to accomplish what you’re after.

    Thread Starter jetcastillo

    (@jetcastillo)

    hello @mtg169 yes it appear in my dropdown order option, also when i click on it , it change the URL “?orderby=availability&paged=1” but at the point that i select the option it trow me a white page with “Page not found”.
    so how do u say to me its a plugin thats its removing it or idk , if u can help me a little bit more i can give u more info of the plugins in the site

    @jetcastillo What is the full URL when you select from the dropdown? On a default install, it should be something like this:
    https://yourdomain.net/shop/?orderby=availability

    If you get a 404, then it sounds like the /shop page doesn’t exist, it’s not set under WooCommerce > Settings > Products > Shop Page, or something else is modifying the URL and default behavior.

    If that checks out, then preferably on a staging location, you can disable all other plugins except WooCommerce, and then enable one by one while testing to see which plugin is causing the problem.

    Plugin Support B C. a11n

    (@battouly)

    @jetcastillo, we have a detailed guide about conflict testing here:

    https://docs.woocommerce.com/document/how-to-test-for-conflicts/

    I hope that helps.

    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Since it’s been a while since we last heard back from you, I’m going to mark this thread resolved.

    Hopefully, you were able to find a solution to your problem! If you have further questions, please feel free to open a new topic.

    Cheers! ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘sort by stock’ is closed to new replies.