I have a function in which i am trying to get the product short description of the current product using the product id but i keep getting Uncaught Error: Call to a member function get_short_description() on bool in
I have the following shortcode function where I am trying to get the product short description of the current WooCommerce product using the product id:
function custom_product_description( $atts ) {
global $product;
// Shortcode attribute (or argument)
$atts = shortcode_atts( array(
'id' => ''
), $atts, 'custom_product_description' );
// If the "id" argument is not defined, we try to get the post Id
if ( ! ( ! empty($atts['id']) && $atts['id'] > 0 ) ) {
$atts['id'] = get_the_id();
}
// We check that the "id" argument is a product id
if ( get_post_type($atts['id']) === 'product' ) {
$product = wc_get_product($atts['id']);
}
$product_short_description = $product->get_short_description();
return $product_short_description;
}
but I keep getting the following error:
Uncaught Error: Call to a member function get_short_description() on bool in …
Any help is appreciated.