Home / Admin / Disable Product delete for published products only
Duplicate Snippet

Embed Snippet on Your Site

Disable Product delete for published products only

Code Preview
php
<?php
/**
 * 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;
}

Comments

Add a Comment