1

I try to call a (public static) function of a plugin in my functions.php, but that doesn't work. I tried this code:

include_once( ABSPATH .  '/wp-content/plugins/atum-multi-inventory/classes/Models/Inventory.php' );

add_action('woocommerce_before_add_to_cart_button', 'getMainInventory', 99 );

function getMainInventory ()    {
    global $product;
    $proid = $product->id;
    if(function_exists('get_product_main_inventory')){
        $test = get_product_main_inventory( $proid );
        echo $test;
    } 
    else {
        echo "failed";
    }   
}

But I always get "failed" back although the plugin is activated. My product is a simple product.

Here is the function I try to call:

/EDIT: Added the namespace and use statements below:

namespace AtumMultiInventory\Models;

defined( 'ABSPATH' ) || die;

use Atum\Components\AtumCache;
use Atum\Components\AtumOrders\AtumOrderPostType;
use Atum\Inc\Globals;
use Atum\Inc\Helpers as AtumHelpers;
use AtumMultiInventory\Legacy\InventoryLegacyTrait;
use AtumMultiInventory\Inc\Helpers;

class Inventory {


        /**
     * Get the Main Inventory for the specified product
     *
     * @since 1.0.0
     *
     * @param int $product_id Must be original translation.
     *
     * @return MainInventory
     */

        public static function get_product_main_inventory( $product_id ) {

        $cache_key      = AtumCache::get_cache_key( 'product_main_inventory', $product_id );
        $main_inventory = AtumCache::get_cache( $cache_key, ATUM_MULTINV_TEXT_DOMAIN, FALSE, $has_cache );

        if ( ! $has_cache ) {

            global $wpdb;

            $product_id = apply_filters( 'atum/multi_inventory/product_id', $product_id );

            // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
            $query = $wpdb->prepare( "
                SELECT id 
                FROM $wpdb->prefix" . self::INVENTORIES_TABLE . '
                WHERE `product_id` = %d AND `is_main` = 1     
            ', $product_id );
            // phpcs:enable

            $main_inventory_id = absint( $wpdb->get_var( $query ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
            $main_inventory    = Helpers::get_inventory( $main_inventory_id, $product_id, TRUE );

            AtumCache::set_cache( $cache_key, $main_inventory, ATUM_MULTINV_TEXT_DOMAIN );

        }

        return $main_inventory;

    }
}

Can anybody help me please?

Chrostip Schaejn
  • 2,347
  • 1
  • 6
  • 13

2 Answers2

1

You should use like this:

add_action('woocommerce_before_add_to_cart_button', 'getMainInventory', 99 );

function getMainInventory ()    {
    global $product;
    use AtumMultiInventory\Models\Inventory;
    $proid = $product->id;
    include_once( ABSPATH . '/wp-content/plugins/atum-multi-inventory/classes/Models/Inventory.php' );
    if(method_exists(Inventory::class, 'get_product_main_inventory' )){
        $test = Inventory::get_product_main_inventory( $proid );
        echo $test;
    } 
    else {
        echo "failed";
    }   
}

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can).

Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42
  • Hi, thanks for sharing, but that doesnt works neither. I get "failed", so the check if the function exists fails. But why? – Chrostip Schaejn Feb 05 '20 at 08:45
  • Mhm it still doesn't work. I also commented the if clause, but then I get a white page. Can it be that I need an initation as in my initial post? `include_once( ABSPATH . '/wp-content/plugins/atum-multi-inventory/classes/Models/Inventory.php' );` – Chrostip Schaejn Feb 05 '20 at 08:58
  • Is plugin activate? – Dmitry Leiko Feb 05 '20 at 09:39
  • Yes, it is active and I get a debug info "Function create_function() is deprecated in Path/to/functions.php on line XY". – Chrostip Schaejn Feb 05 '20 at 09:44
  • Hi Dmitry, thank you so far - unfortunately it doesn't work yet. I also contacted the support of the plugin and he/she made the hint to add the namespace before the class name. I edited my entry post with namespace and use statements Would it be possible to help me again? I have no idea where to insert the namespace. – Chrostip Schaejn Feb 05 '20 at 10:57
0

If it's a public static function, you need to call it using the class name first.

Try this:

$test = Inventory::get_product_main_inventory($proid);
Chemaclass
  • 1,933
  • 19
  • 24