WC_Meta_Box_Product_Data::save │ public static │ WC 1.0

Save meta box data.

Метод класса: WC_Meta_Box_Product_Data{}

Возвращает

null. Ничего (null).

Использование

$result = WC_Meta_Box_Product_Data::save( $post_id, $post );
$post_id(int) (обязательный)
WP post id.
$post(WP_Post) (обязательный)
Post object.

Код WC_Meta_Box_Product_Data::save() WC 11.3.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 : ProductType::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,
			'global_unique_id'          => isset( $_POST['_global_unique_id'] ) ? wc_clean( wp_unslash( $_POST['_global_unique_id'] ) ) : 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,
			'customs_commodity_code'    => isset( $_POST['_customs_commodity_code'] ) ? wp_unslash( $_POST['_customs_commodity_code'] ) : null, // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated by the customs setters.
			'customs_country_of_origin' => isset( $_POST['_customs_country_of_origin'] ) ? wp_unslash( $_POST['_customs_country_of_origin'] ) : null, // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated by the customs setters.
			'customs_description'       => isset( $_POST['_customs_description'] ) ? wp_unslash( $_POST['_customs_description'] ) : null, // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated by the customs setters.
			'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'                  => ProductType::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 ( wc_get_container()->get( CostOfGoodsSoldController::class )->feature_is_enabled() ) {
		$cogs_value = wc_clean( wp_unslash( $_POST['_cogs_value'] ?? null ) );
		$product->set_cogs_value( is_null( $cogs_value ) ? null : (float) wc_format_decimal( $cogs_value ) );
	}

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

	// Remove _product_template_id for products that were created with the new product editor.
	$product->delete_meta_data( '_product_template_id' );

	// Only sync POS visibility for product types where the "Available for POS" checkbox is rendered,
	// so unsupported types (downloadable, grouped, external) don't silently acquire the pos-hidden term.
	$pos_visibility_sync = wc_get_container()->get( POSProductVisibilitySync::class );
	if ( $product instanceof WC_Product && $pos_visibility_sync->is_product_supported( $product ) ) {
		$visible_in_pos = isset( $_POST['_visible_in_pos'] ) && 'yes' === wc_clean( wp_unslash( $_POST['_visible_in_pos'] ) );
		$pos_visibility_sync->set_product_pos_visibility( $post_id, $visible_in_pos );
	}

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

	$previous_product_type = WC_Product_Factory::get_product_type( $post_id );

	$product->save();

	if ( $product instanceof WC_Product && ProductType::EXTERNAL === $product_type && $previous_product_type !== $product_type ) {
		self::persist_external_product_stock_data( $product );
	}

	if ( $product->is_type( ProductType::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 */
}