How to use the use WooCommerce functions in a WordPress plugin?

How would I use WooCommerce hooks in my plugin? Here is what I am trying to do:

add_filter('woocommerce_edit_product_columns', 'pA_manage_posts_columns');
function pA_manage_posts_columns($columns, $post_type = 'product') {
global $woocommerce;
if ( in_array( $post_type, array( 'product') ) ) {
    $columns['offering_price'] = __( 'offering price', 'your_text_domain' ); // this offering price title 
    $columns['offering_qty'] = __( 'Qty', 'your_text_domain' ); // add the quantity title
    }
unset($columns['name']);
return $columns;

Here is how I include WooCommerce in my plugin:

Read More
$ds = DIRECTORY_SEPARATOR;
$base_dir = realpath(dirname(__FILE__)  . $ds . '..') . $ds;
$file = "{$base_dir}woocommerce{$ds}woocommerce.php"; 
include_once($file);

Still can’t get the output from

print_r($woocommerce);

Related posts

Leave a Reply

1 comment

  1. You’re mixed the hook with the callback. The original call is in this file:

    add_filter( 'manage_edit-product_columns', 'woocommerce_edit_product_columns' );
    

    Your code should be:

    add_filter( 'manage_edit-product_columns', 'pA_manage_posts_columns', 15 );
    
    function pA_manage_posts_columns( $columns ) 
    {
        global $woocommerce;
        $columns['offering_price'] = __( 'offering price', 'your_text_domain' ); // this offering price title 
        $columns['offering_qty'] = __( 'Qty', 'your_text_domain' ); // add the quantity title
        unset($columns['name']);
        return $columns;
    }
    

    Note that there’s no post_type parameter in the callback. The filter hook is already telling what the post type is: manage_edit-product_columns.

    As Obmerk Kronen has pointed out, there’s no need to include any WooCommerce file, all its functionality is available to you already.