Reputation: 23
We've been using WooCommerce subscriptions fine however it has started to cause issue when viewing orders from a users "my account" section, turning on WP_DEBUG shows the following:
Uncaught Error: Call to a member function get_user_id() on bool in /wp-content/plugins/woocommerce-subscriptions/includes/wcs-user-functions.php:381
Line 381 is:
if ( $user_id === $subscription->get_user_id() ) {
$allcaps['edit_shop_subscription_status'] = true;
}
break;
Given this is an official plugin I'm not sure how an issue has developed, I also have the stack trace:
Stack trace: #0 /wp-includes/class-wp-hook.php(289): wcs_user_has_capability(Array, Array, Array) #1 /wp-includes/plugin.php(212): WP_Hook->apply_filters(Array, Array) #2 /wp-includes/class-wp-user.php(783): apply_filters('user_has_cap', Array, Array, Array, Object(WP_User)) #3 /wp-includes/capabilities.php(788): WP_User->has_cap('edit_shop_subsc...', 44013) #4 /wp-content/plugins/woocommerce-subscriptions/includes/wcs-user-functions.php(320): user_can(Object(WP_User), 'edit_shop_subsc...', 44013) #5 /wp-content/plugins/tmanager/tmanager.php(76): wcs_get_all_user_actions_for_subscription(Object(WC_O in /wp-content/plugins/woocommerce-subscriptions/includes/wcs-user-functions.php on line 381
The full block of code:
/**
* Checks if a user has a certain capability
*
* @access public
* @param array $allcaps
* @param array $caps
* @param array $args
* @return array
*/
function wcs_user_has_capability( $allcaps, $caps, $args ) {
if ( isset( $caps[0] ) ) {
switch ( $caps[0] ) {
case 'edit_shop_subscription_payment_method' :
$user_id = $args[1];
$subscription = wcs_get_subscription( $args[2] );
if ( $subscription && $user_id === $subscription->get_user_id() ) {
$allcaps['edit_shop_subscription_payment_method'] = true;
}
break;
case 'edit_shop_subscription_status' :
$user_id = $args[1];
$subscription = wcs_get_subscription( $args[2] );
if ( $user_id === $subscription->get_user_id() ) {
$allcaps['edit_shop_subscription_status'] = true;
}
break;
case 'edit_shop_subscription_line_items' :
$user_id = $args[1];
$subscription = wcs_get_subscription( $args[2] );
if ( $user_id === $subscription->get_user_id() ) {
$allcaps['edit_shop_subscription_line_items'] = true;
}
break;
case 'switch_shop_subscription' :
$user_id = $args[1];
$subscription = wcs_get_subscription( $args[2] );
if ( $user_id === $subscription->get_user_id() ) {
$allcaps['switch_shop_subscription'] = true;
}
break;
case 'subscribe_again' :
$user_id = $args[1];
$subscription = wcs_get_subscription( $args[2] );
if ( $user_id === $subscription->get_user_id() ) {
$allcaps['subscribe_again'] = true;
}
break;
case 'pay_for_order' :
$user_id = $args[1];
$order = wc_get_order( $args[2] );
if ( $order && wcs_order_contains_subscription( $order, 'any' ) ) {
if ( $user_id === $order->get_user_id() ) {
$allcaps['pay_for_order'] = true;
} else {
unset( $allcaps['pay_for_order'] );
}
}
break;
}
}
return $allcaps;
}
add_filter( 'user_has_cap', 'wcs_user_has_capability', 15, 3 );
Using print_r the $user_id gives a result but i cant get anything out of $subscription - could it be showing an error only for users who are NOT on a subscription plan? Any pointers greatly asppreciated.
Upvotes: 2
Views: 1550
Reputation: 253901
As the guilty seems to be the WC_Subscription
Object that return a boolean value, use instead:
if ( is_a( $subscription, 'WC_Subscription' ) && $user_id === $subscription->get_user_id() ) {
$allcaps['edit_shop_subscription_status'] = true;
}
It should solve the issue.
Upvotes: 1