Reputation: 63
I need some help with plugins, classes, and methods in PHP.
I am trying to call a method from a plugin file from one of my own functions but I doesn't seem to make it work.
PS. The plugin that is being used is Learndash-Group-Registration and not Woocommerce. Woocommerce is used for it's products and orders.
This is the file where I try to call the method: (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;
}
}
?>
This is not the complete Plugin file but it contains the method I want to call and the Class.
When I try to call the method this way I get the error message:
'Fatal error: Cannot declare class wisdmlabs\ldgroups\WdmLdWooCommerce, because the name is already in use in C:\wamp64\www\meritmind\site\public\wp-content\plugins\ld-group-registration\modules\class-wdm-woocommerce.php on line 6'
And when I remove the 'include' line I recieve this error message:
'Fatal error: Uncaught Error: Class 'WdmLdWooCommerce' not found in C:\wamp64\www\meritmind\site\public\wp-content\themes\meritgo\file.php on line 108'
I do not understand what I am doing wrong. At one point it says I have not declared the class but when I declare the Class using include it says the Class is already declared.
Would really appreciate som help,
Best regards, Ledung
Upvotes: 0
Views: 226
Reputation: 4187
This fails for 2 reasons:
1) you're using include, rather than include_once. Using _once
in general means the code isn't repeated if it's already been successfully used previously (which in this case would have stopped the class being defined a second time).
2) your class_exists('WdmLdWooCommerce')
doesn't work because that's not the full class name, i.e. this is always returning false as 'WdmLdWooCommerce' doesn't exist, but \wisdmlabs\ldgroups\WdmLdWooCommerce
should.
Upvotes: 1