WooCommerce – 如何隐藏过时的 SKU

huangapple go评论105阅读模式
英文:

WooCommerce – how to hide outdated skus

问题

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 );

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();

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();
} 

}

英文:

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 );

https://stackoverflow.com/questions/50592683/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();

https://stackoverflow.com/questions/50633551/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();
		} 

} 

答案1

得分: -1

要隐藏WooCommerce中过时的SKU,您可以使用pre_get_posts操作钩子来修改主查询并排除具有特定SKU的产品。

function exclude_outdated_skus( $query ) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
        // 过时的SKU数组
        $outdated_skus = array( 'SKU1', 'SKU2', 'SKU3' ); // 用您的过时SKU替换这里

        // 排除具有过时SKU的产品
        $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' );

在上面的代码中,将$outdated_skus替换为您的过时SKU数组。pre_get_posts操作钩子用于修改主查询并排除具有指定SKU的产品。

通过将meta_query参数添加到查询中,您可以指定键_sku(WooCommerce中的SKU元键)并使用NOT IN比较来排除具有过时SKU的产品。

这种方法允许您在不逐个遍历数组或单独应用设置到查询结果的情况下,从搜索查询中排除过时的SKU。

英文:

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.

huangapple
  • 本文由 发表于 2023年7月6日 22:42:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76630014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定