Automattic\WooCommerce\Internal\Abilities\Domain

ProductDelete::executepublic staticWC 10.9.0

Delete a product.

Method of the class: ProductDelete{}

Hooks from the method

Returns

Array|\WP_Error.

Usage

$result = ProductDelete::execute( $input );
$input(array) (required)
Ability input.

Changelog

Since 10.9.0 Introduced.

ProductDelete::execute() code WC 11.0.1

public static function execute( array $input ) {
	$product = self::get_product_from_input( $input );

	if ( is_wp_error( $product ) ) {
		return $product;
	}

	if ( $product->is_type( ProductType::VARIATION ) ) {
		return new \WP_Error(
			'woocommerce_product_type_unsupported',
			__( 'Product type is not supported by this ability.', 'woocommerce' ),
			array( 'status' => 400 )
		);
	}

	$product_id = $product->get_id();
	$force      = (bool) ( $input['force'] ?? false );

	/**
	 * Filter whether a product supports trashing in WooCommerce domain abilities.
	 *
	 * @param bool        $supports_trash Whether the product supports trashing.
	 * @param \WC_Product $product        The product being considered for trashing.
	 *
	 * @since 10.9.0
	 */
	$supports_trash = apply_filters( 'woocommerce_product_object_trashable', EMPTY_TRASH_DAYS > 0, $product );

	if ( ! $force && ! $supports_trash ) {
		return new \WP_Error(
			'woocommerce_trash_not_supported',
			__( 'Trash is disabled on this site. Pass force: true to permanently delete.', 'woocommerce' ),
			array( 'status' => 501 )
		);
	}

	$deleted = $product->delete( $force );

	if (
		! $deleted
		|| ( $force && null !== get_post( $product_id ) )
		|| ( ! $force && 'trash' !== get_post_status( $product_id ) )
	) {
		return new \WP_Error(
			'woocommerce_product_delete_failed',
			__( 'Failed to delete product.', 'woocommerce' ),
			array( 'status' => 500 )
		);
	}

	return array(
		'deleted' => (bool) $deleted,
		'id'      => $product_id,
	);
}