Automattic\WooCommerce\Internal\Utilities

COTMigrationUtil::get_post_or_object_metapublicWC 1.0

Gets value of a meta key from WC_Data object if passed, otherwise from the post object. This helper function support backward compatibility for meta box functions, when moving from posts based store to custom tables.

Method of the class: COTMigrationUtil{}

No Hooks.

Returns

Array|Mixed|String. Value of the meta key.

Usage

$COTMigrationUtil = new COTMigrationUtil();
$COTMigrationUtil->get_post_or_object_meta( ?WP_Post $post, ?\WC_Data $data, $key, $single );
?WP_Post $post(required)
.
?\WC_Data $data(required)
.
$key(string) (required)
Key to fetch metadata for.
$single(true|false) (required)
Whether metadata is single.

COTMigrationUtil::get_post_or_object_meta() code WC 10.8.1

public function get_post_or_object_meta( ?WP_Post $post, ?\WC_Data $data, string $key, bool $single ) {
	if ( isset( $data ) ) {
		if ( method_exists( $data, "get$key" ) ) {
			return $data->{"get$key"}();
		}
		return $data->get_meta( $key, $single );
	} else {
		return isset( $post->ID ) ? get_post_meta( $post->ID, $key, $single ) : false;
	}
}