Skip to content

Instantly share code, notes, and snippets.

@justinshreve
Created January 20, 2017 16:16
Show Gist options
  • Save justinshreve/92483e5e8a3346fc78e1803cc9ad401e to your computer and use it in GitHub Desktop.
Save justinshreve/92483e5e8a3346fc78e1803cc9ad401e to your computer and use it in GitHub Desktop.
<?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