Mohsen Jalalian
Mohsen Jalalian

Reputation: 1120

How to use woocommerce functions in wordpress plugin

I want to use wc_get_product function in a file in a wordpress plugin but when I call this function I have Call to undefined function wc_get_product() error. WooCommerce plugin installed and actice in my wordpress

Upvotes: 1

Views: 6671

Answers (2)

Andy Tschiersch
Andy Tschiersch

Reputation: 3816

Probably you call wc functions before they are loaded. Try this:

/**
* Check if WooCommerce is active
**/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    // Put your plugin code here

    // If you want use WooCommerce functions, do that after WooCommerce is loaded
    add_action( 'woocommerce_loaded', 'my_function_with_wc_functions' );        
}

function my_function_with_wc_functions() {

    $product = wc_get_product();

}

Great stuff to read:

Upvotes: 4

mariobros
mariobros

Reputation: 899

Try this:

$product = WC_Product($product_id);
$product->wc_get_product();

Upvotes: 0

Related Questions