Using woocommerce functions inside a custom shortcode
I am developing a custom plugin.
The main purpose of this plugin is to create a custom shortcode.
Inside callback function of this plugin, I use some of woocommerce functions.
But I get “call to undefined function (any woocommerce function)” error.
For sake of simplicity, I created a very basic plugin to describe this problem.
<?php
add_shortcode('custom_shortcode','custom_shortcode_callback');
function custom_shortcode_callback(){
$order_id = 81201;
$order = wc_get_product($order_id);
echo 'anything';
}
?>
I get this error:
Uncaught Error: Call to undefined function wc_get_product().
In some answers regarding this problem, the solution was to hook ‘woocommerce_init’ or ‘woocommerce_loaded’ actions. But I want to echo some result in page and just hooking to these actions, does not provide my shortcode’s intention.
Leave an answer