【问题标题】:Calling Class method from other file从其他文件调用类方法
【发布时间】:2018-03-01 22:21:53
【问题描述】:

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

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

附言。正在使用的插件是 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' ] );

class-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 行的注册\模块\class-wdm-woocommerce.php'

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

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

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

非常感谢 som 的帮助,

最好的问候,Ledung

【问题讨论】:

  • 这有两个原因,一是您使用的是 include,而不是 include_once,二是您的 class_exists('WdmLdWooCommerce') 不起作用,因为这不是完整的类名,因为您缺少命名空间。
  • 非常感谢 :)
  • 如果您愿意留下答案,我可以将其标记为解决方案! :)

标签: php wordpress woocommerce


【解决方案1】:

失败有两个原因:

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

2) 你的 class_exists('WdmLdWooCommerce') 不起作用,因为这不是完整的类名,即它总是返回 false,因为 'WdmLdWooCommerce' 不存在,但 \wisdmlabs\ldgroups\WdmLdWooCommerce 应该。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多