Automattic\WooCommerce\Api\Mutations\Products
UpdateProduct::execute
[ReturnType( Product::class )]Method of the class: UpdateProduct{}
No Hooks.
Returns
null. Nothing (null).
Usage
$UpdateProduct = new UpdateProduct(); $UpdateProduct->execute( #[Description( foo )] UpdateProductInput $input, ): object;
[Description( foo )] UpdateProductInput $input (required)
: .
- (required)
- .
UpdateProduct::execute() UpdateProduct::execute code WC 10.9.1
public function execute(
#[Description( 'The fields to update.' )]
UpdateProductInput $input,
): object {
$wc_product = wc_get_product( $input->id );
if ( ! $wc_product instanceof \WC_Product ) {
throw new ApiException( 'Product not found.', 'NOT_FOUND', status_code: 404 );
}
foreach ( array( 'name', 'slug', 'sku', 'description', 'short_description', 'manage_stock', 'stock_quantity' ) as $field ) {
if ( $input->was_provided( $field ) ) {
$wc_product->{"set_{$field}"}( $input->$field );
}
}
foreach ( array( 'regular_price', 'sale_price' ) as $field ) {
if ( $input->was_provided( $field ) ) {
$wc_product->{"set_{$field}"}( null !== $input->$field ? (string) $input->$field : '' );
}
}
// Nullable enum: only invoke the setter when the client supplied a
// non-null value. An explicit null means "ignore this field" here —
// WC_Product's set_status doesn't accept null and would fall back
// to a default, silently overwriting whatever is already on the
// product.
if ( $input->was_provided( 'status' ) && null !== $input->status ) {
$wc_product->set_status( $input->status->value );
}
if ( $input->was_provided( 'dimensions' ) ) {
foreach ( array( 'length', 'width', 'height', 'weight' ) as $field ) {
if ( $input->dimensions->was_provided( $field ) ) {
$wc_product->{"set_{$field}"}( null !== $input->dimensions->$field ? (string) $input->dimensions->$field : '' );
}
}
}
$wc_product->save();
return ProductMapper::from_wc_product( $wc_product );
}