Advertisement
palsushobhan

product meta value shortcode

May 28th, 2021
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.59 KB | None | 0 0
  1. add_shortcode('woo_product_meta', function($atts) {
  2.     global $post;
  3.     $atts = shortcode_atts( array(
  4.         'id' => null,
  5.         'key' => '',
  6.     ), $atts, 'woo_product_meta' );
  7.  
  8.     if(empty($atts['key'])) return;
  9.  
  10.     if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
  11.         $product = wc_get_product( intval( $atts['id'] ) );
  12.     } elseif( is_product() ) {
  13.         $product = wc_get_product($post);
  14.     }
  15.    
  16.     if ( ! is_a( $product, 'WC_Product' ) ) return;
  17.  
  18.     $meta_val = get_post_meta($product->get_id(), $atts['key'], true);
  19.  
  20.     return $meta_val;
  21. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement