Skip to content

Instantly share code, notes, and snippets.

@Preciousomonze
Created October 8, 2020 13:49
Show Gist options
  • Save Preciousomonze/288683ee42ed4424854b7a8d02afe02a to your computer and use it in GitHub Desktop.
Save Preciousomonze/288683ee42ed4424854b7a8d02afe02a to your computer and use it in GitHub Desktop.
Alter WooCommerce Product Catalog visibility via hook. Based on this thread here: https://woocommercecommunity.slack.com/archives/C1KAZ91E3/p1601981478254000
<?php
/**
* Change catalog visibility.
*
* No need to run ::save(), cause this hook is run before $product->save()
*
* Hooked to woocommerce_admin_process_product_object
*
* @param WC_Product $product The product object.
*/
function pekky_save_product_data( $product ) {
$product->set_catalog_visibility( 'hidden' );
}
add_filter( 'woocommerce_admin_process_product_object', 'pekky_save_product_data' );
@wsoyka
Copy link

wsoyka commented Oct 13, 2020

got it working thanks to this, thank you! (couldn't edit visibility on product status change any other way, changes to visibility never got persisted)
im using it like this:

add_action("<newstatus>_product", "on_product_<newstatus>", 10, 2);
function on_product_<newstatus>($id, $post){
	$product = wc_get_product($id);
	add_post_meta( $id, "my_visibility", "hidden", true);
}
add_filter( 'woocommerce_admin_process_product_object', 'pekky_save_product_data' );
function pekky_save_product_data( $product ) {
	$id = $product->get_id();
	$visib = get_post_meta($id, "my_visibility", true);
	if($visib != null){
		$product->set_catalog_visibility($visib);
		delete_post_meta($id, "my_visibility");
	}
}

@Preciousomonze
Copy link
Author

Glad it worked! πŸš€
Don't forget to have a nice day and try out pancakes πŸ’ͺ🏼πŸ₯ž
Over and out! πŸ„πŸ½β€β™‚οΈ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment