WC_Product_Variation_Data_Store_CPT::update_post_metapublicWC 3.0.0

Helper method that updates all the post meta for a product based on its settings in the WC_Product class.

Method of the class: WC_Product_Variation_Data_Store_CPT{}

Returns

null. Nothing (null).

Usage

$WC_Product_Variation_Data_Store_CPT = new WC_Product_Variation_Data_Store_CPT();
$WC_Product_Variation_Data_Store_CPT->update_post_meta( $product, $force );
$product(WC_Product) (required) (passed by reference — &)
Product object.
$force(true|false)
Force update. Used during create.
Default: false

Changelog

Since 3.0.0 Introduced.

WC_Product_Variation_Data_Store_CPT::update_post_meta() code WC 9.9.5

public function update_post_meta( &$product, $force = false ) {
	$meta_key_to_props = array(
		'_variation_description' => 'description',
	);

	$props_to_update = $force ? $meta_key_to_props : $this->get_props_to_update( $product, $meta_key_to_props );

	foreach ( $props_to_update as $meta_key => $prop ) {
		$value   = $product->{"get_$prop"}( 'edit' );
		$updated = update_post_meta( $product->get_id(), $meta_key, $value );
		if ( $updated ) {
			$this->updated_props[] = $prop;
		}
	}

	if ( $this->cogs_feature_is_enabled() ) {
		$cogs_value_is_additive = $product->get_cogs_value_is_additive();

		/**
		 * Filter to customize the "Cost of Goods Sold value is additive" flag that gets saved for a given variable product,
		 * or to suppress the saving of the flag (so that custom storage can be used) if null is returned.
		 * Note that returning null will suppress any database access (for either saving the flag or deleting it).
		 *
		 * @since 9.7.0
		 *
		 * @param bool|null $cogs_value_is_additive The flag to be written to the database. If null is returned nothing will be written or deleted.
		 * @param WC_Product $product The product for which the flag is being saved.
		 */
		$cogs_value_is_additive = apply_filters( 'woocommerce_save_product_cogs_is_additive_flag', $cogs_value_is_additive, $product );

		if ( ! is_null( $cogs_value_is_additive ) ) {
			$updated = $this->update_or_delete_post_meta( $product, '_cogs_value_is_additive', $cogs_value_is_additive ? 'yes' : '' );
			if ( $updated ) {
				$this->updated_props[] = 'cogs_value_is_additive';
			}
		}
	}

	parent::update_post_meta( $product, $force );
}