WC_Data_Store_WP::update_or_delete_post_meta( WC_Data $object, string $meta_key, mixed $meta_value )

Update meta data in, or delete it from, the database.


Description Description

Avoids storing meta when it’s either an empty string or empty array. Other empty values such as numeric 0 and null should still be stored. Data-stores can force meta to exist using must_exist_meta_keys.

Note: WordPress get_metadata function returns an empty string when meta data does not exist.


Parameters Parameters

$object

(Required) The WP_Data object (WC_Coupon for coupons, etc).

$meta_key

(Required) Meta key to update.

$meta_value

(Required) Value to save.


Top ↑

Return Return

(bool) True if updated/deleted.


Top ↑

Source Source

File: includes/data-stores/class-wc-data-store-wp.php

	protected function update_or_delete_post_meta( $object, $meta_key, $meta_value ) {
		if ( in_array( $meta_value, array( array(), '' ), true ) && ! in_array( $meta_key, $this->must_exist_meta_keys, true ) ) {
			$updated = delete_post_meta( $object->get_id(), $meta_key );
		} else {
			$updated = update_post_meta( $object->get_id(), $meta_key, $meta_value );
		}

		return (bool) $updated;
	}

Top ↑

Changelog Changelog

Changelog
Version Description
3.6.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.