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.



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.

aminjabari 1 year 2023-01-24T12:35:53-05:00 0 Answers 0 views 0

Leave an answer