0

I'm looking for a way to hide outdated skus in WooCommerce.

The method below works. I'm wondering if I'm missing a way to accomplish this with fewer iterations.

Is it possible to exclude products from a search by an array of skus?

Can a syntax like this be made to work with skus in WooCommerce query? $query = new WP_Query( array( 'cat' => '-12,-34,-56' ) );

Is it possible to apply settings to query results without iterating through the array?

Also, I've seen two syntaxes recommended for hiding products. Which of these is least likely to be deprecated in the future?

$terms = array( 'exclude-from-search', 'exclude-from-catalog' ); // for hidden..
wp_set_post_terms( $prod_ID, $terms, 'product_visibility', false );

Change product visibility via PHP on Woocommerce 3+

Or this syntax:

// Get an instance of the product variation from a defined ID
$child_product = wc_get_product(child_id);
// Change the product visibility
$child_product->set_catalog_visibility('visible');
// Save and sync the product visibility
$child_product->save();

Update the product visibility in Woocommerce 3

This works, but I'm wondering if it's possible to get rid of either or both of the iterative processes:

function test_hide_outdated_skus(){

    $product_ids_to_keep = array();
    $skus_to_keep = array('sku_0','sku_1','sku_2','sku_3','sku_4');
    foreach ($skus_to_keep as $p) {
        if (wc_get_product_id_by_sku($p)) {
            array_push($product_ids_to_keep,wc_get_product_id_by_sku($p));
        }                                                               
    }
    $query = new WC_Product_Query(array(
    'exclude' => $product_ids_to_keep,
    'limit'     => -1,
    'tax_query' => array( array(
        'taxonomy' => 'product_visibility',
        'field'    => 'slug',
        'terms' => array('exclude-from-search', 'exclude-from-catalog' ),
        'operator'  => 'NOT IN'
    ) ),
    ) );

    $products = $query->get_products(); 

    foreach ($products as $prod) {
        $prod->set_catalog_visibility('hidden');
        $prod->save();
        } 

} 
Rip
  • 27
  • 5

1 Answers1

-1

To hide outdated SKUs in WooCommerce, you can use the pre_get_posts action hook to modify the main query and exclude products with specific SKUs.

function exclude_outdated_skus( $query ) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
        // Array of outdated SKUs
        $outdated_skus = array( 'SKU1', 'SKU2', 'SKU3' ); // Replace with your outdated SKUs

        // Exclude products with outdated SKUs
        $meta_query = $query->get( 'meta_query' );
        $meta_query[] = array(
            'key'     => '_sku',
            'value'   => $outdated_skus,
            'compare' => 'NOT IN',
        );
        $query->set( 'meta_query', $meta_query );
    }
}
add_action( 'pre_get_posts', 'exclude_outdated_skus' );

In the above code, replace $outdated_skus with an array of your outdated SKUs. The pre_get_posts action hook is used to modify the main query and exclude products with the specified SKUs.

By adding the meta_query parameter to the query, you can specify the key _sku (the SKU meta key in WooCommerce) and use the NOT IN comparison to exclude products with outdated SKUs.

This approach allows you to exclude outdated SKUs from the search query without iterating through the array or applying settings to the query results individually.

Bhautik
  • 11,125
  • 3
  • 16
  • 38
  • 2
    Welcome back to Stack Overflow. It looks like it's been a while since you've posted and may not be aware of the current policies since many of your recent *twenty* answers appear likely to have been entirely or partially written by AI (e.g., ChatGPT). Please be aware that [posting of AI-generated content is banned here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. Thanks! – NotTheDr01ds Jul 07 '23 at 10:51
  • 1
    **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. The moderation team can use your help to identify quality issues. – NotTheDr01ds Jul 07 '23 at 10:51