Hello @fmunozn,
Sorry for the delay in responding due to different timezone, I always tried best to answer as soon as possible.
As you know that the “Product Code for WooCommerce” is an add-on the Product Code can not be fetched via a woocommerce inbuilt product functions. Also, there is no function like get_product_code() which you are using. So obviously it will show an error.
Product Code data are saved as a meta key and value to the “postmeta” table of WordPress.
To get it displayed for your custom code you can use the following snippet.
function product_code_shortcode() {
global $product;
if( $product ) {
$product_id = $product->get_id();
// Check if product is variable product type
if( $product->is_type( 'variable' ) ){
//get all variation list
$variations = $product->get_available_variations();
if( $variations ) {
foreach( $variations as $variable_product ) {
$value = get_post_meta( $variable_product['variation_id'], "_product_code_variant", true );
$code = !$value ? __( 'N/A', 'product-code-for-woocommerce' ) : $value;
echo 'CODE: ' . $code . '<br/>';
}
}
} else {
$value = get_post_meta( $product_id, "_product_code", true );
$code = !$value ? __( 'N/A', 'product-code-for-woocommerce' ) : $value;
echo 'CODE: ' . $code;
}
}
}
add_shortcode('codenew', 'product_code_shortcode');
Hope my answer helps you. Let me know further.
Thanks
Abdullah