0

So basically what I have done is programmatically changed the price such that if a level 1 customer comes in, it automatically pulls the level 1 price from the custom field put in and then trims the dollar sign. Once this is done it changes the price for the individual logged in without changing the database. However, i get the error "unable to purchase" when I try to purchase the product. I changed the product to make it purchasable and I find out the price shows as $0.00 in the cart.

Here is my code:

add_filter('woocommerce_get_price', 'custom_price_wanted', 10, 2);
function custom_price_wanted($price, $product) {
    if (!is_user_logged_in()) return $price;
    if (has_role_wanted('level_1_customers')){
        $new_price = get_post_meta( get_the_ID(), '_level_1', true );
        $new_price = trim($new_price,'$');
        $price = $new_price;
    }
    if (has_role_wanted('level_2_customers')){
        $new_price = get_post_meta( get_the_ID(), '_level_2', true );
        $new_price = trim($new_price,'$');
        $price = $new_price;
    }
    if (has_role_wanted('level_3_customers')){
        $new_price = get_post_meta( get_the_ID(), '_level_3', true );
        $new_price = trim($new_price,'$');
        $price = $new_price;
    }
    return $price;
}

function has_role_wanted($role = '',$user_id = null){
    if ( is_numeric( $user_id )){
        $user = get_user_by( 'id',$user_id );
    }
    else {
        $user = wp_get_current_user();
    }
    if ( empty( $user ) ) {
        return false;
    }
    return in_array( $role, (array) $user->roles );
}

EDIT: FOUND THE ISSUE!! The problem lies in get_post_meta( get_the_ID() ). It ends up getting the cart id as opposed to the product id which is why it shows as free.

Baneet Grover
  • 57
  • 2
  • 8
  • Have you tried to echo `$price` and see if it calculates the new price correctly? – Epodax Aug 26 '15 at 09:15
  • Hey @Epodax, on the products page it shows perfectly fine, even when I add it to cart, however when I go on to the cart page it shows as $0.00. – Baneet Grover Aug 26 '15 at 09:28

0 Answers0