1. Home
  2. WC Vendors Pro
  3. Code Snippets
  4. Disable Product Delete for Published products only

Disable Product Delete for Published products only

Requirements

  • WC Vendors Pro 1.7.9+

WC Vendors Pro provides a setting to disable the delete option for vendors on the Product lists page. This disables delete for all product status’, published or draft.

You can find this setting under WC Vendors > Settings > Capabilities > Products

Leave this setting unchecked for this snippet to function. This conditionally disables delete for only published (online) products. This will allow your vendors to safely delete draft products while being unable to delete published (online) products.

The second part of the snippet will disable the draft button if the product is published. This stops the vendor from working around the delete issue by making the product draft and then deleting it.

/**
 * This will disable delete only for published products, leaving drafts to be able to be deleted
 * You would not need to disable delete in the WC Vendors Settings in WP Admin. 
 */
add_filter( 'wcv_product_table_row_actions', 'wcv_allow_delete_draft', 10, 2 );
 function wcv_allow_delete_draft( $actions, $product ){ 

	// If the product is published, disable delete 
	if ( 'publish' ===  $product->get_status() ){ 
		unset( $actions['delete']  );
	}

	return $actions;
}

/**
 * Disable the draft button on the product edit page if the product is published. 
 * 
 * This will only work with WC Vendors Pro 1.7.9 and above. 
 */
add_filter( 'wcv_product_draft_button', 'wcv_disable_draft' ); 
 function wcv_disable_draft( $field ){ 
	$object_id = get_query_var( 'object_id' );
	$product = wc_get_product( $object_id ); 

	if (is_object( $product) && 'publish' === $product->get_status() ){ 
		return array();
	} 
	return $field;
}

filters

Updated on March 18, 2021

Was this article helpful?