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.

I use this shortcode inside a WordPress page editor like this:

[custom_shortcode]
  • This topic was modified 1 hour, 39 minutes ago by aminjabari.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer