• Resolved artemis21

    (@artemis21)


    Hello,

    I’m adding a sort by featured in the products search results but I have a problem.

    This is my code:

    add_filter( ‘woocommerce_catalog_orderby’, ‘hugo_boss_catalog_orderby’ );
    sport_catalog_orderby( $orderby ) {
    // Add a new sorting option called “Featured” to the dropdown
    $orderby[‘featured’] = __(‘Featured’, ‘woocommerce’);

    // Return the modified sorting options
    return $orderby;

    }

    add_action( ‘woocommerce_product_query’, ‘sport_product_query’ );
    function sport_product_query( $q ) {
    // Check if not in admin panel and if the orderby parameter is set to “featured”
    if ( ! is_admin() && isset($_GET[‘orderby’]) && ‘featured’ === esc_attr($_GET[‘orderby’]) ) {
    // Get the existing tax queries
    $tax_query = $q->get(‘tax_query’);

    // Add a new tax query to filter by product visibility term “featured”
    $tax_query[] = array(
    ‘taxonomy’ => ‘product_visibility’,
    ‘field’ => ‘name’,
    ‘terms’ => ‘featured’,
    ‘operator’ => ‘IN’, // Include featured products
    );

    // Merge all tax queries with ‘relation’ set to ‘OR’
    $tax_query[‘relation’] = ‘OR’;

    // Add another tax query to exclude products that are marked as “featured”
    $tax_query[] = array(
    ‘taxonomy’ => ‘product_visibility’,
    ‘field’ => ‘name’,
    ‘terms’ => ‘featured’,
    ‘operator’ => ‘NOT IN’, // Exclude featured products
    );

    // Set the modified tax query
    $q->set( ‘tax_query’, $tax_query );

    // Set the order as DESC (or ASC if needed)
    $q->set( ‘order’, ‘DESC’ );
    }

    }

    unfortunately it only returns ONLY featured products, I tried adding logics to show both featured and non featured but it shows either unsorted products or no products at all.

    Can you help me fix this function so that it shows featured first and non featured after?

    Thank you

    • This topic was modified 1 year ago by artemis21.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hey there, @artemis21! Thanks for contacting us.

    While we can’t provide support for code customization as per our support policy, we do our best to offer advice and direct you to appropriate resources.

    You can 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 too.

    I’m going to leave it open for a bit to see if anyone is able to chime in and help you out further.

    Have a wonderful day!

    Thread Starter artemis21

    (@artemis21)

    Hi @carolm29

    I tried using the Slack link but it says it’s expired.

    Thank you

    hey, @artemis21!

    I just checked and I was able to access it. Have you tried different browsers to check if it works? Let me share it again:

    https://woo.com/community-slack/

    Have a wonderful day!

    anastas10s

    (@anastas10s)

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – we’ll be here if and/or when you are ready to continue.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add Sort by Featured’ is closed to new replies.