• Resolved samuelashbr00k

    (@samuelashbr00k)


    I have written some code which displays all of the featured products alphabetically when the sorting option is set to “Sort by Featured”. I would like to be able to add a query onto the end of this so that instead of exclusively showing featured products, it would also have a loop to iterate and display non featured products afterwards. How could i make this work?

    // Create sorting option
    add_filter( 'woocommerce_catalog_orderby', 'website_catalog_orderby' );
    function website_catalog_orderby( $orderby ) {
        $orderby['featured'] = __('Sort by Featured', 'woocommerce');
    
        return $orderby;
    }
    
    // Loop through all featured products
    add_action( 'woocommerce_product_query', 'website_product_query' );
    function website_product_query( $q ) {
        if ( ! is_admin() && isset($_GET['orderby']) && 'featured' === esc_attr($_GET['orderby']) ) {
            $tax_query = $q->get('tax_query');
            $tax_query[] = array(
                'taxonomy' => 'product_visibility',
                'field'    => 'name',
                'terms'    => 'featured',
            );
            $q->set( 'tax_query', $tax_query );
            $q->set( 'orderby', 'title' );
            $q->set( 'order', 'ASC' ); // A-Z
        }
    }
    

    Many Thanks.

Viewing 1 replies (of 1 total)
  • Hi @samuelashbr00k

    Thanks for reaching out!

    I understand that using the code snippet above, you were able to display all of the featured products alphabetically when the sorting option is set to “Sort by Featured”. In addition, you would like to alter it so that instead of exclusively showing featured products, it would also have a loop to iterate and display non-featured products afterwards, correct?

    Kindly be informed that custom coding is outside our scope of support,hence, I am leaving this thread open for a bit to see if anyone can chime in to help you out.

    For questions related to development and custom coding, your best bet is to ask on any of these channels for support. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, too.

    Hope this helps!

Viewing 1 replies (of 1 total)
  • The topic ‘Loop through all featured products first & non featured products afterwards’ is closed to new replies.