Automattic\WooCommerce\Internal\DataStores

CustomMetaDataStore::add_meta()publicWC 1.0

Add new piece of meta.

Method of the class: CustomMetaDataStore{}

No Hooks.

Return

Int. meta ID

Usage

$CustomMetaDataStore = new CustomMetaDataStore();
$CustomMetaDataStore->add_meta( $object, $meta );
$object(WC_Data) (required) (passed by reference — &)
WC_Data object.
$meta(stdClass) (required)
(containing ->key and ->value).

CustomMetaDataStore::add_meta() code WC 7.1.0

public function add_meta( &$object, $meta ) {
	global $wpdb;

	$db_info = $this->get_db_info();

	$object_id  = $object->get_id();
	$meta_key   = wp_unslash( wp_slash( $meta->key ) );
	$meta_value = maybe_serialize( is_string( $meta->value ) ? wp_unslash( wp_slash( $meta->value ) ) : $meta->value );

	// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_value,WordPress.DB.SlowDBQuery.slow_db_query_meta_key
	$result = $wpdb->insert(
		$db_info['table'],
		array(
			$db_info['object_id_field'] => $object_id,
			'meta_key'                  => $meta_key,
			'meta_value'                => $meta_value,
		)
	);
	// phpcs:enable WordPress.DB.SlowDBQuery.slow_db_query_meta_value,WordPress.DB.SlowDBQuery.slow_db_query_meta_key

	return $result ? (int) $wpdb->insert_id : false;
}