Skip to content

Membership Not Being Added After Order Completion/ Automatically Disabling the "Add to Cart" Button for Users Who Already Have the Membership #209

@aliasgharv3x

Description

@aliasgharv3x
  1. Membership Not Being Added After Order Completion
    Issue:
    After an order is completed—especially when the order is manually completed by an admin—the membership was not being added to the membership plugin, even though access to protected content was granted.

Fix:

The pmprowoo_add_membership_from_order function now checks for a custom meta field (_pmprowoo_membership_added) on the order.
If this meta field exists, the function exits early to prevent duplicate membership additions.
If not, after processing the order, the membership is added and the meta field is updated to mark that the membership has been successfully added.
This ensures that regardless of how the order is completed (automatically or manually by an admin), the membership is correctly added to the user’s account.
2. Automatically Disabling the "Add to Cart" Button for Users Who Already Have the Membership
Issue:
For users who already have the corresponding membership, the product was still showing an active “Add to Cart” button, which could lead to duplicate purchases.

Fix:

The pmprowoo_is_purchasable filter was modified to first check if the user is logged in and already holds the membership level associated with the product.
If the user already has the membership, the product is marked as non-purchasable.
Additionally, a custom message (“You already have this membership.”) is displayed on the product page instead of the “Add to Cart” button, informing the user that they already own the membership.
This prevents duplicate purchases and improves the user experience.
General Improvements
Global Array Safeguards: Global arrays (e.g., membership levels, gift codes, discounts) are always ensured to be non-null, minimizing potential errors.
Early Initialization: The plugin’s modules and globals are loaded early (priority 5 on the init hook) to enhance stability.
Maintained Integration: All other functionality (like price modifications, membership discount handling, and synchronization of user meta) remains intact, ensuring seamless integration between WooCommerce and Paid Memberships Pro.
pmpro-woocommerce.php:

  1. Modified pmprowoo_is_purchasable Function
    This change checks if the logged‑in user already holds the membership level corresponding to the current product. If so, it marks the product as non‑purchasable and displays a message instead of the “Add to Cart” button.

function pmprowoo_is_purchasable( $is_purchasable, $product ) {
global $pmprowoo_product_levels;

// If already not purchasable, return early.
if ( ! $is_purchasable ) {
	return $is_purchasable;
}

// Ensure membership levels array is valid.
if ( empty( $pmprowoo_product_levels ) || ! is_array( $pmprowoo_product_levels ) ) {
	return $is_purchasable;
}

$membership_product_ids = array_keys( $pmprowoo_product_levels );
$product_id             = $product->get_id();

// Check if user is logged in and already has the membership for this product.
if ( is_user_logged_in() ) {
	$current_level = pmpro_getMembershipLevelForUser( get_current_user_id() );
	if ( ! empty( $current_level ) && in_array( $product_id, $membership_product_ids, true ) ) {
		if ( $current_level->id == $pmprowoo_product_levels[ $product_id ] ) {
			$is_purchasable = false;
			add_action( 'woocommerce_single_product_summary', 'pmprowoo_already_member_message' );
			return $is_purchasable;
		}
	}
}

// For membership products, check if the cart contains a different membership product.
if ( in_array( $product_id, $membership_product_ids, true ) ) {
	$cart_items = ( is_object( WC()->cart ) ) ? WC()->cart->get_cart_contents() : array();
	$other_membership_found = false;
	foreach ( $cart_items as $item ) {
		if ( in_array( $item['product_id'], $membership_product_ids, true ) && $item['product_id'] != $product_id ) {
			$other_membership_found = true;
			break;
		}
	}
	if ( $other_membership_found ) {
		$is_purchasable = false;
		add_action( 'woocommerce_single_product_summary', 'pmprowoo_purchase_disabled' );
	}
}

return $is_purchasable;

}

  1. New pmprowoo_already_member_message Function
    This new function displays the message “You already have this membership.” in place of the Add to Cart button when the user already holds the membership.

function pmprowoo_add_membership_from_order( $order_id ) {
global $wpdb, $pmprowoo_product_levels;

// Exit if PMPro isn't active.
if ( ! defined( "PMPRO_DIR" ) || ! function_exists( "pmpro_init" ) ) {
	return;
}

// Exit if membership levels array is empty.
if ( empty( $pmprowoo_product_levels ) || ! is_array( $pmprowoo_product_levels ) ) {
	return;
}

// Exit if membership has already been added for this order.
if ( get_post_meta( $order_id, "_pmprowoo_membership_added", true ) ) {
	return;
}

// [Existing logic to loop through order items and add membership...]
// ...

// After processing, mark this order as having membership added.
update_post_meta( $order_id, "_pmprowoo_membership_added", 1 );

}

Membership Purchase Check:
The pmprowoo_is_purchasable function now checks the logged‑in user’s membership level and displays a message if the user already has that membership.

Order Completion Handling:
The pmprowoo_add_membership_from_order function now prevents duplicate membership additions by checking/updating a meta field.

These changes resolve the two issues:

Ensuring memberships are added correctly after order completion (even for manually completed orders).
Automatically disabling the “Add to Cart” button (with a message) for users who already have the corresponding membership.
Feel free to review and integrate these code sections. Let me know if you need any further details!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions