filters - Checking for stock in accordance with product's SKU

admin2025-01-07  5

Im trying to figure out how to check for products in accordance with their SKUs. I have already figured out how to check if product is in stock. However, each product has 3 purchase buttons: one for Rent, one for Purchase New, and one for Purchase Used. The two I am concerned with is Purchase Used and Purchase New. If there are no products that are listed as 'used' in stock, ideally, if one looks for used products, they should not show up, and the button Purchase Used should be disabled, and not be allowed to allow the users to purchase. But for whatever reason, the only way a product will not be shown at all is if all 3 attributes, used, rent, and new, are all out of stock. Each product's quality is based upon their SKU number. How can I make it so that I can check against a specific product's quantity based on its SKU?

Here is my main code I used for filtering:

function unified_product_filter($filters) {
    $excluded_ids = array(6949, 6948, 6947, 6946, 6944, 6935, 6883, 5584, 6097, 6096, 5747, 5581, 5580, 5579, 5577, 5547);
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => -1,
        'post__not_in' => $excluded_ids,
        'orderby' => array(
            'menu_order' => 'ASC',
            'date' => 'DESC'
        ),
    );

    $has_filters = !empty($filters);
    if ($has_filters) {
        $args['tax_query'] = array('relation' => 'AND');
        foreach ($filters as $attribute => $value) {
            $args['tax_query'][] = array(
                'taxonomy' => 'pa_' . $attribute,
                'field' => 'slug',
                'terms' => $value,
            );
        }

        // Add condition for "used" products
        if (isset($filters['condition']) && $filters['condition'] === 'used') {
            $args['meta_query'] = array(
                array(
                    'key' => '_stock_status',
                    'value' => 'instock',
                    'compare' => '=',
                )
            );
        }
        



        // Add condition for "new" products
        if (isset($filters['condition']) && $filters['condition'] === 'new') {
            $args['meta_query'] = array(
                array(
                    'key' => '_stock_status',
                    'value' => 'instock',
                    'compare' => '=',
                )
            );
        }


    }




    

    // Query for filtered products
    $products = new WP_Query($args);

    ob_start();
    if ($products->have_posts()) {
        while ($products->have_posts()) {
            $products->the_post();
            wc_get_template_part('content', 'product');
        }
    } else {
        echo '<h1>No products found</h1>';
    }
    $filtered_html = ob_get_clean();

    wp_reset_postdata();

    // Query for unfiltered products if filters are active
    $other_html = '';
    if ($has_filters) {
        $filtered_product_ids = wp_list_pluck($products->posts, 'ID');
        $unfiltered_args = array(
            'post_type' => 'product',
            'posts_per_page' => -1,
            'post__not_in' => array_merge($excluded_ids, $filtered_product_ids),
        );

        $unfiltered_products = new WP_Query($unfiltered_args);

        ob_start();
        if ($unfiltered_products->have_posts()) {
            while ($unfiltered_products->have_posts()) {
                $unfiltered_products->the_post();
                wc_get_template_part('content', 'product');
            }
        }
        $other_html = ob_get_clean();

        wp_reset_postdata();
    }

    return array(
        'filtered_products' => $filtered_html,
        'other_products' => $other_html,
    );
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736257736a460.html

最新回复(0)