Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从其他文件调用类方法_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 从其他文件调用类方法

Php 从其他文件调用类方法,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我需要一些关于PHP中插件、类和方法的帮助 我试图从我自己的一个函数的插件文件中调用一个方法,但我似乎无法使它工作 另外,正在使用的插件是Learndash Group Registration,而不是Woocommerce。Woocommerce用于其产品和订单 这是我尝试调用方法的文件:(file.php) class wdm woocommerce.php $args = array( 'order_id' => 1234, ); include

我需要一些关于PHP中插件、类和方法的帮助

我试图从我自己的一个函数的插件文件中调用一个方法,但我似乎无法使它工作

另外,正在使用的插件是Learndash Group Registration,而不是Woocommerce。Woocommerce用于其产品和订单

这是我尝试调用方法的文件:(file.php)

class wdm woocommerce.php

    $args = array(
      'order_id' => 1234,
    );
    include '/wp-content/plugins/ld-
group-registration/modules/class-wdm-woocommerce.php';

WdmLdWooCommerce::wdmCourseOrderCompletedCreateGroup( $args[ 'order_id' ] );
    <?php
namespace wisdmlabs\ldgroups;

defined('ABSPATH') || exit;
if (!class_exists('WdmLdWooCommerce')) {
    class WdmLdWooCommerce
    {
        public function wdmCourseOrderCompletedCreateGroup($order_id)
        {
            if ($this->wdmIsRenewalOrder($order_id)) {
                return;
            }
            $order = new \WC_Order($order_id);
            $product_id = null;
            $group_data = array();
            $items = $order->get_items();
            $group_creation_done = get_post_meta($order_id, 'wdm_successful_group_creation', true);

            if ($group_creation_done == 'done') {
                return;
            }

            if (WC_VERSION < '3.0.0') {
                foreach ($items as $item) {
                    $product_id = $item[ 'product_id' ];
                    $quantity = apply_filters('wdm_modify_total_number_of_registrations', $item[ 'qty' ], $product_id);
                    $product_type = wdmGetProductType($product_id);
                    $group_registration = isset($item[ 'Group Registration' ]) ? $item[ 'Group Registration' ] : '';
                    //$courses = maybe_unserialize(get_post_meta($product_id, '_related_course', true));
                    $courses = '';
                    if ($product_type == 'variable-subscription') {
                        $variation_id = $item['variation_id'];
                        if (!empty($variation_id)) {
                            $courses = maybe_unserialize(get_post_meta($variation_id, '_related_course', true));
                        }
                        $product_id = $variation_id;
                    } else {
                        $courses = maybe_unserialize(get_post_meta($product_id, '_related_course', true));
                    }
                    if (!empty($courses) && $group_registration != '') {
                        $uid = $order->get_user_id();
                        $user1 = new \WP_User($uid);
                        $user1->add_role('group_leader');
                        $user1->remove_role('customer');
                        $user1->remove_role('subscriber');
                        $group_data[ 'leader' ] = $uid;
                        $group_data[ 'course' ] = $courses;
                        $this->wdmCreateLearndashGroup($group_data, $order, $order_id, $quantity, $product_id, $product_type);
                        update_post_meta($order_id, 'wdm_successful_group_creation', 'done');
                    }
                }
            } else {
                foreach ($items as $key_item_id => $item) {
                    $key_item_id = $key_item_id;
                    $product_id = $item[ 'product_id' ];
                    //$quantity = $item[ 'qty' ];
                    $quantity = apply_filters('wdm_modify_total_number_of_registrations', $item[ 'qty' ], $product_id);
                    $product_type = wdmGetProductType($product_id);
                    $group_registration = isset($item[ 'Group Registration' ]) ? $item[ 'Group Registration' ] : '';
                    $courses = '';
                    if ($product_type == 'variable-subscription') {
                        $variation_id = $item['variation_id'];
                        if (!empty($variation_id)) {
                            $courses = maybe_unserialize(get_post_meta($variation_id, '_related_course', true));
                            $product_id = $variation_id;
                        }
                    } else {
                        $courses = maybe_unserialize(get_post_meta($product_id, '_related_course', true));
                    }
                    if (!empty($courses) && $group_registration != '') {
                        $uid = $order->get_user_id();
                        $user1 = new \WP_User($uid);
                        $user1->add_role('group_leader');
                        $user1->remove_role('customer');
                        $user1->remove_role('subscriber');
                        $group_data[ 'leader' ] = $uid;
                        $group_data[ 'course' ] = $courses;
                        $this->wdmCreateLearndashGroup($group_data, $order, $order_id, $quantity, $product_id, $product_type);
                        update_post_meta($order_id, 'wdm_successful_group_creation', 'done');
                    }
                }
            }
            // exit;
        }
}
?>

这不是完整的插件文件,但它包含我要调用的方法和类

当我尝试以这种方式调用该方法时,会收到错误消息:

“致命错误:无法声明wisdmlabs\ldgroups\WdmLdWooCommerce类,因为该名称已在第6行的C:\wamp64\www\meritmind\site\public\wp content\plugins\ld group registration\modules\class-wdm-woocommerce.php中使用”

当我删除“include”行时,我收到以下错误消息:

“致命错误:未捕获错误:在C:\wamp64\www\meritmind\site\public\wp content\themes\meritgo\file.php第108行找不到类“WdmLdWooCommerce”

我不明白我做错了什么。有一次它说我没有声明类,但当我使用include声明类时,它说类已经声明了

非常感谢som的帮助


致以最诚挚的问候,乐东这失败有两个原因:

1) 您正在使用include,而不是include\u一次。使用
\u一次
通常意味着,如果以前已经成功使用过代码,则不会重复该代码(在这种情况下,将停止第二次定义该类)


2) 您的
class\u exists('WdmLdWooCommerce')
不起作用,因为它不是完整的类名,也就是说,由于“WdmLdWooCommerce”不存在,它总是返回false,但是
\wisdmlabs\ldgroups\WdmLdWooCommerce
应该存在。

这有两个原因,一个是您使用include,而不是include\u一次,两个,您的
类_exists('WdmLdWooCommerce')
不起作用,因为这不是完整的类名,因为您缺少名称空间。这起作用了,非常感谢:)如果您能留下一个答案,我可以将其标记为解决方案!:)