WC_Data_Store_WP::update_or_delete_post_meta()
Update meta data in, or delete it from, the database.
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.
Method of the class: WC_Data_Store_WP{}
No Hooks.
Return
true|false
. True if updated/deleted.
Usage
// protected - for code of main (parent) or child class $result = $this->update_or_delete_post_meta( $object, $meta_key, $meta_value );
- $object(WC_Data) (required)
- The WP_Data object (WC_Coupon for coupons, etc).
- $meta_key(string) (required)
- Meta key to update.
- $meta_value(mixed) (required)
- Value to save.
Changelog
Since 3.6.0 | Introduced. |
Since 3.6.0 | Added to prevent empty meta being stored unless required. |
WC_Data_Store_WP::update_or_delete_post_meta() WC Data Store WP::update or delete post meta code WC 9.3.3
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; }