• jonasojczyk

    (@jonasojczyk)


    Hi,

    Please help me figure out how to hide out of stock products from shop/category ONLY when using sorting/ordering option ‘sort after price: low/high’ and ‘popularity’ option.

    View post on imgur.com

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Niels Lange

    (@nielslange)

    Hello @jonasojczyk ??

    Please help me figure out how to hide out of stock products from shop/category ONLY when using sorting/ordering option ‘sort after price: low/high’ and ‘popularity’ option.

    I understood that you only want to hide the out-of-stock products, when a user orders the products using the option Sort by popularity or Sort by price: low to high, but not when selecting any of the other options.

    While this is technically possible, it’s a fairly complex development topic. I’m going to leave it open for a bit to see if anyone can chime in to help you out.

    I can also recommend <a href=”https://developer.woocommerce.com/&#8221; rel=”noopener” target=”_blank”>the Woo Developer Resources Portal</a> for resources on developing for woo.name.

    You can also visit the <a href=”https://www.facebook.com/groups/advanced.woocommerce/&#8221; rel=”noopener” target=”_blank”>WooCommerce Facebook group</a> or the #developers channel of the <a href=”https://woo.url/community-slack/&#8221; rel=”noopener” target=”_blank”>Woo Community Slack</a>. We’re lucky to have a great community of open-source developers for Woo, and many of our developers hang out there as well.

    Thread Starter jonasojczyk

    (@jonasojczyk)

    @nielslange
    Hi,
    I have asked on Facebook and Slack.

    Plugin Contributor Niels Lange

    (@nielslange)

    @jonasojczyk I hope someone there will be able to assist you on this particular issue.

    Brad Dalton

    (@wordpresssites)

    Add this PHP code to the end of your child themes functions file or custom code snippets plugin.

    add_action( 'woocommerce_product_query', 'wpsites_hide_out_of_stock_products_on_specific_sorting', 10, 2 );
    function wpsites_hide_out_of_stock_products_on_specific_sorting( $query ) {

    if ( ! is_admin() && $query->is_main_query() && is_shop() || is_product_category() ) {

    $orderby = isset( $_GET['orderby'] ) ? wc_clean( wp_unslash( $_GET['orderby'] ) ) : '';

    if ( in_array( $orderby, array( 'price', 'popularity' ) ) ) {

    $query->set( 'meta_query', array_merge( $query->get( 'meta_query' ), array(
    array(
    'key' => '_stock_status',
    'value' => 'instock',
    'compare' => '='
    )
    ) ) );
    }
    }
    }
    Thread Starter jonasojczyk

    (@jonasojczyk)

    @wordpresssites

    Hi Brad,

    Your code works great, but can you make it so that ‘Sort by price: high to low’ (?orderby=price-desc) hides out-of-stock products also? ??

    Gunleif Grube Malerier Til Salg | GALLERI FUSION

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.