PMPro integration

Issue #1094 new
Valentina R created an issue

Asked them to add one line to their plugin to make it compatible with ours. If they don’t do it, we need to use this code snippet in order to make Appointments compatible with their plugin (line 26):

remove_filter( "woocommerce_product_get_price", "pmprowoo_get_membership_price", 10 );
remove_filter( "woocommerce_product_variation_get_price", "pmprowoo_get_membership_price", 10 );

add_filter( "woocommerce_product_get_price", "pmprowoo_get_membership_price_wca", 10, 2 );
add_filter( "woocommerce_product_variation_get_price", "pmprowoo_get_membership_price_wca", 10, 5 );

function pmprowoo_get_membership_price_wca( $price, $product ) {
    global $current_user, $pmprowoo_member_discounts, $pmprowoo_product_levels, $pmprowoo_discounts_on_subscriptions;

    // quitely exit if PMPro isn't active
    if ( ! defined( 'PMPRO_DIR' ) && ! function_exists( 'pmpro_init' ) ) {
        return $price;
    }

    // make sure $product is a product object
    if (! is_object( $product ) ) {
        $product = wc_get_product( $product );
    }

    // no product? bail
    if ( empty( $product ) ) {
        return $price;
    }

    // COMPATIIBILITY WITH APPOINTMENTS
    if ( doing_action( 'woocommerce_add_cart_item_data' ) ) return $price;

    // Get the ID for the product that we are currently getting a membership price for.
    if ( $product->get_type() === 'variation' ) {
        $product_id = $product->get_parent_id(); //for variations   
    } else {
        $product_id = $product->get_id();
    }

    $membership_product_ids = array_keys( $pmprowoo_product_levels );
    $items       = is_object( WC()->cart ) ? WC()->cart->get_cart_contents() : array(); // items in the cart

    //ignore membership products and subscriptions if we are set that way
    if ( ( ! $pmprowoo_discounts_on_subscriptions || $pmprowoo_discounts_on_subscriptions == 'No' ) && ( $product->is_type( array( 'subscription', 'variable-subscription', 'subscription_variation' ) ) || in_array( $product_id, $membership_product_ids, false ) ) ) {
        return $price;
    }

    // Get all membership level ids that will be given to the user after checkout.
    // Ignore the product that we are currently getting a membership price for.
    $cart_level_ids = array();
    foreach ( $items as $item ) {
        if ( $item['product_id'] != $product->get_id() && in_array( $item['product_id'], $membership_product_ids ) ) {
            $cart_level_ids[] = $pmprowoo_product_levels[ $item['product_id'] ];
        }
    }

    // Get the membership level ids that the user already has.
    $user_levels = pmpro_getMembershipLevelsForUser( $current_user->ID );
    $user_level_ids = empty( $user_levels ) ? array() : wp_list_pluck( $user_levels, 'id' );

    // Merge the cart levels and user levels and remove duplicates to get all levels that could discount this product.
    $discount_level_ids = array_unique( array_merge( $cart_level_ids, $user_level_ids ) );

    // Find the lowest membership price for this product.
    $lowest_price = (float) $price;
    $lowest_price_level = null; // Needed for backwards compatibility for pmprowoo_get_membership_price filter.
    foreach ( $discount_level_ids as $level_id ) {
        $level_price = get_post_meta( $product_id, '_level_' . $level_id . '_price', true );
        if ( ! empty( $level_price ) || $level_price === '0' || $level_price === '0.00' || $level_price === '0,00' ) {
            $level_price = (float) $level_price;
            if ( $level_price < $lowest_price ) {
                $lowest_price = $level_price;
                $lowest_price_level = $level_id;
            }
        }
    }

    // Find the highest membership discount for this product.
    $highest_discount = 0;
    foreach ( $discount_level_ids as $level_id ) {
        if ( ! empty( $pmprowoo_member_discounts ) && ! empty( $pmprowoo_member_discounts[ $level_id ] ) ) {
            $level_discount = (float) $pmprowoo_member_discounts[ $level_id ];
            if ( $level_discount > $highest_discount ) {
                $highest_discount = $level_discount;
            }
        }
    }
    $discount_price = $lowest_price - ( $lowest_price * $highest_discount );

    // Filter the result.
    return apply_filters( 'pmprowoo_get_membership_price', $discount_price, $lowest_price_level, $price, $product );
}

Requested here.

Comments (0)

  1. Log in to comment