WC_Meta_Box_Product_Data::save()public staticWC 1.0

Save meta box data.

Method of the class: WC_Meta_Box_Product_Data{}

Return

null. Nothing (null).

Usage

$result = WC_Meta_Box_Product_Data::save( $post_id, $post );
$post_id(int) (required)
WP post id.
$post(WP_Post) (required)
Post object.

WC_Meta_Box_Product_Data::save() code WC 8.7.0

public static function save( $post_id, $post ) {
	// phpcs:disable WordPress.Security.NonceVerification.Missing
	// Process product type first so we have the correct class to run setters.
	$product_type = empty( $_POST['product-type'] ) ? WC_Product_Factory::get_product_type( $post_id ) : sanitize_title( wp_unslash( $_POST['product-type'] ) );
	$classname    = WC_Product_Factory::get_product_classname( $post_id, $product_type ? $product_type : 'simple' );
	$product      = new $classname( $post_id );
	$attributes   = self::prepare_attributes();
	$stock        = null;

	// Handle stock changes.
	if ( isset( $_POST['_stock'] ) ) {
		if ( isset( $_POST['_original_stock'] ) && wc_stock_amount( $product->get_stock_quantity( 'edit' ) ) !== wc_stock_amount( wp_unslash( $_POST['_original_stock'] ) ) ) {
			/* translators: 1: product ID 2: quantity in stock */
			WC_Admin_Meta_Boxes::add_error( sprintf( __( 'The stock has not been updated because the value has changed since editing. Product %1$d has %2$d units in stock.', 'woocommerce' ), $product->get_id(), $product->get_stock_quantity( 'edit' ) ) );
		} else {
			$stock = wc_stock_amount( wp_unslash( $_POST['_stock'] ) );
		}
	}

	// Handle dates.
	$date_on_sale_from = '';
	$date_on_sale_to   = '';

	// Force date from to beginning of day.
	if ( isset( $_POST['_sale_price_dates_from'] ) ) {
		$date_on_sale_from = wc_clean( wp_unslash( $_POST['_sale_price_dates_from'] ) );

		if ( ! empty( $date_on_sale_from ) ) {
			$date_on_sale_from = date( 'Y-m-d 00:00:00', strtotime( $date_on_sale_from ) ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
		}
	}

	// Force date to to the end of the day.
	if ( isset( $_POST['_sale_price_dates_to'] ) ) {
		$date_on_sale_to = wc_clean( wp_unslash( $_POST['_sale_price_dates_to'] ) );

		if ( ! empty( $date_on_sale_to ) ) {
			$date_on_sale_to = date( 'Y-m-d 23:59:59', strtotime( $date_on_sale_to ) ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
		}
	}

	$errors = $product->set_props(
		array(
			'sku'                => isset( $_POST['_sku'] ) ? wc_clean( wp_unslash( $_POST['_sku'] ) ) : null,
			'purchase_note'      => isset( $_POST['_purchase_note'] ) ? wp_kses_post( wp_unslash( $_POST['_purchase_note'] ) ) : '',
			'downloadable'       => isset( $_POST['_downloadable'] ),
			'virtual'            => isset( $_POST['_virtual'] ),
			'featured'           => isset( $_POST['_featured'] ),
			'catalog_visibility' => isset( $_POST['_visibility'] ) ? wc_clean( wp_unslash( $_POST['_visibility'] ) ) : null,
			'tax_status'         => isset( $_POST['_tax_status'] ) ? wc_clean( wp_unslash( $_POST['_tax_status'] ) ) : null,
			'tax_class'          => isset( $_POST['_tax_class'] ) ? sanitize_title( wp_unslash( $_POST['_tax_class'] ) ) : null,
			'weight'             => isset( $_POST['_weight'] ) ? wc_clean( wp_unslash( $_POST['_weight'] ) ) : null,
			'length'             => isset( $_POST['_length'] ) ? wc_clean( wp_unslash( $_POST['_length'] ) ) : null,
			'width'              => isset( $_POST['_width'] ) ? wc_clean( wp_unslash( $_POST['_width'] ) ) : null,
			'height'             => isset( $_POST['_height'] ) ? wc_clean( wp_unslash( $_POST['_height'] ) ) : null,
			'shipping_class_id'  => isset( $_POST['product_shipping_class'] ) ? absint( wp_unslash( $_POST['product_shipping_class'] ) ) : null,
			'sold_individually'  => ! empty( $_POST['_sold_individually'] ),
			'upsell_ids'         => isset( $_POST['upsell_ids'] ) ? array_map( 'intval', (array) wp_unslash( $_POST['upsell_ids'] ) ) : array(),
			'cross_sell_ids'     => isset( $_POST['crosssell_ids'] ) ? array_map( 'intval', (array) wp_unslash( $_POST['crosssell_ids'] ) ) : array(),
			'regular_price'      => isset( $_POST['_regular_price'] ) ? wc_clean( wp_unslash( $_POST['_regular_price'] ) ) : null,
			'sale_price'         => isset( $_POST['_sale_price'] ) ? wc_clean( wp_unslash( $_POST['_sale_price'] ) ) : null,
			'date_on_sale_from'  => $date_on_sale_from,
			'date_on_sale_to'    => $date_on_sale_to,
			'manage_stock'       => ! empty( $_POST['_manage_stock'] ),
			'backorders'         => isset( $_POST['_backorders'] ) ? wc_clean( wp_unslash( $_POST['_backorders'] ) ) : null,
			'stock_status'       => isset( $_POST['_stock_status'] ) ? wc_clean( wp_unslash( $_POST['_stock_status'] ) ) : null,
			'stock_quantity'     => $stock,
			'low_stock_amount'   => isset( $_POST['_low_stock_amount'] ) && '' !== $_POST['_low_stock_amount'] ? wc_stock_amount( wp_unslash( $_POST['_low_stock_amount'] ) ) : '',
			'download_limit'     => isset( $_POST['_download_limit'] ) && '' !== $_POST['_download_limit'] ? absint( wp_unslash( $_POST['_download_limit'] ) ) : '',
			'download_expiry'    => isset( $_POST['_download_expiry'] ) && '' !== $_POST['_download_expiry'] ? absint( wp_unslash( $_POST['_download_expiry'] ) ) : '',
			// Those are sanitized inside prepare_downloads.
			'downloads'          => self::prepare_downloads(
				isset( $_POST['_wc_file_names'] ) ? wp_unslash( $_POST['_wc_file_names'] ) : array(), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
				isset( $_POST['_wc_file_urls'] ) ? wp_unslash( $_POST['_wc_file_urls'] ) : array(), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
				isset( $_POST['_wc_file_hashes'] ) ? wp_unslash( $_POST['_wc_file_hashes'] ) : array() // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
			),
			'product_url'        => isset( $_POST['_product_url'] ) ? esc_url_raw( wp_unslash( $_POST['_product_url'] ) ) : '',
			'button_text'        => isset( $_POST['_button_text'] ) ? wc_clean( wp_unslash( $_POST['_button_text'] ) ) : '',
			'children'           => 'grouped' === $product_type ? self::prepare_children() : null,
			'reviews_allowed'    => ! empty( $_POST['comment_status'] ) && 'open' === $_POST['comment_status'],
			'attributes'         => $attributes,
			'default_attributes' => self::prepare_set_attributes( $attributes, 'default_attribute_' ),
		)
	);

	if ( is_wp_error( $errors ) ) {
		WC_Admin_Meta_Boxes::add_error( $errors->get_error_message() );
	}

	/**
	 * Set props before save.
	 *
	 * @since 3.0.0
	 */
	do_action( 'woocommerce_admin_process_product_object', $product );

	$product->save();

	if ( $product->is_type( 'variable' ) ) {
		$original_post_title = isset( $_POST['original_post_title'] ) ? wc_clean( wp_unslash( $_POST['original_post_title'] ) ) : '';
		$post_title          = isset( $_POST['post_title'] ) ? wc_clean( wp_unslash( $_POST['post_title'] ) ) : '';

		$product->get_data_store()->sync_variation_names( $product, $original_post_title, $post_title );
	}
	/* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */
	do_action( 'woocommerce_process_product_meta_' . $product_type, $post_id );
	/* phpcs:enable WordPress.Security.NonceVerification.Missing and WooCommerce.Commenting.CommentHooks.MissingHookComment */
}