从其他文件调用Class方法

时间:2018-03-01 11:08:29

标签: php wordpress woocommerce

我需要帮助PHP中的插件,类和方法。

我试图从我自己的一个函数调用插件文件中的方法,但我似乎并没有让它工作。

PS。正在使用的插件是Learndash-Group-Registration而不是Woocommerce。 Woocommerce用于它的产品和订单。

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

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

WdmLdWooCommerce::wdmCourseOrderCompletedCreateGroup( $args[ 'order_id' ] );

类-WDM-woocommerce.php

    <?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,因为该名称已在C:\ wamp64 \ www \ meritmind \ site \ public \ wp-content \ plugins \ ld-group-中使用在第6行注册\ modules \ class-wdm-woocommerce.php

当我删除'include'行时,我收到此错误消息:

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

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

非常感谢som的帮助,

最好的问候,Ledung

1 个答案:

答案 0 :(得分:0)

这失败有两个原因:

1)您使用的是include而不是include_once。通常使用_once意味着如果代码之前已成功使用过(在这种情况下会使第二次定义的类停止),则代码不会重复。

2)您的class_exists('WdmLdWooCommerce')无法正常工作,因为这不是完整的班级名称,即,这总是返回false,因为WdmLdWooCommerce&#39; WdmLdWooCommerce&#39;不存在,但\wisdmlabs\ldgroups\WdmLdWooCommerce应该存在。