WC_Product_Data_Store_CPT::update()publicWC 1.0

Method to update a product in the database.

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

Хуки из метода

Возвращает

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

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

$WC_Product_Data_Store_CPT = new WC_Product_Data_Store_CPT();
$WC_Product_Data_Store_CPT->update( $product );
$product(WC_Product) (обязательный) (передается по ссылке — &)
Product object.

Код WC_Product_Data_Store_CPT::update() WC 8.7.0

public function update( &$product ) {
	$product->save_meta_data();
	$changes = $product->get_changes();

	// Only update the post when the post data changes.
	if ( array_intersect( array( 'description', 'short_description', 'name', 'parent_id', 'reviews_allowed', 'status', 'menu_order', 'date_created', 'date_modified', 'slug', 'post_password' ), array_keys( $changes ) ) ) {
		$post_data = array(
			'post_content'   => $product->get_description( 'edit' ),
			'post_excerpt'   => $product->get_short_description( 'edit' ),
			'post_title'     => $product->get_name( 'edit' ),
			'post_parent'    => $product->get_parent_id( 'edit' ),
			'comment_status' => $product->get_reviews_allowed( 'edit' ) ? 'open' : 'closed',
			'post_status'    => $product->get_status( 'edit' ) ? $product->get_status( 'edit' ) : 'publish',
			'menu_order'     => $product->get_menu_order( 'edit' ),
			'post_password'  => $product->get_post_password( 'edit' ),
			'post_name'      => $product->get_slug( 'edit' ),
			'post_type'      => 'product',
		);
		if ( $product->get_date_created( 'edit' ) ) {
			$post_data['post_date']     = gmdate( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getOffsetTimestamp() );
			$post_data['post_date_gmt'] = gmdate( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getTimestamp() );
		}
		if ( isset( $changes['date_modified'] ) && $product->get_date_modified( 'edit' ) ) {
			$post_data['post_modified']     = gmdate( 'Y-m-d H:i:s', $product->get_date_modified( 'edit' )->getOffsetTimestamp() );
			$post_data['post_modified_gmt'] = gmdate( 'Y-m-d H:i:s', $product->get_date_modified( 'edit' )->getTimestamp() );
		} else {
			$post_data['post_modified']     = current_time( 'mysql' );
			$post_data['post_modified_gmt'] = current_time( 'mysql', 1 );
		}

		/**
		 * When updating this object, to prevent infinite loops, use $wpdb
		 * to update data, since wp_update_post spawns more calls to the
		 * save_post action.
		 *
		 * This ensures hooks are fired by either WP itself (admin screen save),
		 * or an update purely from CRUD.
		 */
		if ( doing_action( 'save_post' ) ) {
			$GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $product->get_id() ) );
			clean_post_cache( $product->get_id() );
		} else {
			wp_update_post( array_merge( array( 'ID' => $product->get_id() ), $post_data ) );
		}
		$product->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.

	} else { // Only update post modified time to record this save event.
		$GLOBALS['wpdb']->update(
			$GLOBALS['wpdb']->posts,
			array(
				'post_modified'     => current_time( 'mysql' ),
				'post_modified_gmt' => current_time( 'mysql', 1 ),
			),
			array(
				'ID' => $product->get_id(),
			)
		);
		clean_post_cache( $product->get_id() );
	}

	$this->update_post_meta( $product );
	$this->update_terms( $product );
	$this->update_visibility( $product );
	$this->update_attributes( $product );
	$this->update_version_and_type( $product );
	$this->handle_updated_props( $product );
	$this->clear_caches( $product );

	wc_get_container()
		->get( DownloadPermissionsAdjuster::class )
		->maybe_schedule_adjust_download_permissions( $product );

	$product->apply_changes();

	do_action( 'woocommerce_update_product', $product->get_id(), $product );
}