Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC_Deposits_Product_Meta class.
* @since 1.2
*/
class WC_Deposits_Product_Meta {
/** @var object Class Instance */
private static $instance;
/**
* Get the class instance.
*/
public static function get_instance() {
return null === self::$instance ? ( self::$instance = new self ) : self::$instance;
}
/**
* Gets a piece of product meta in a version agnostic way.
*
* @param int|WC_Product $product
* @param string $meta_key
* @return mixed
*/
public static function get_meta( $product, $meta_key ) {
if ( is_object( $product ) ) {
$product_id = $product->get_id();
} else {
$product_id = $product;
}
if ( version_compare( WC_VERSION, '2.7', '<' ) ) {
return get_post_meta( $product_id, $meta_key, true );
} else {
if ( is_numeric( $product ) ) {
$product = wc_get_product( $product );
}
return $product->get_meta( $meta_key, true );
}
}
/**
* Update a piece of product meta in a version agnostic way.
*
* @param int|WC_Product $product
* @param string $meta_key
* @param mixed $meta_value
*/
public static function update_meta( $product, $meta_key, $meta_value ) {
if ( is_object( $product ) ) {
$product_id = $product->get_id();
} else {
$product_id = $product;
}
if ( version_compare( WC_VERSION, '2.7', '<' ) ) {
update_post_meta( $product_id, $meta_key, $meta_value );
} else {
if ( is_numeric( $product ) ) {
$product = wc_get_product( $product );
}
$product->update_meta_data( $meta_key, $meta_value );
$product->save_meta_data();
}
}
}
WC_Deposits_Product_Meta::get_instance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.